真给我整无语了,数据库实验怎么这么多,要考试还要写实验。写写写,写他娘的什么东西。
实验内容
实现查询
ps:实验要求用两种方法写的我除了union都是用的两种方法写,union查了好久都查不到,或许有人知道union的平替嘛请教一下大家
- 创建两张表 palette_a 和 palette_b(结构相同,但表名不同,color 为颜色)
CREATE TABLE palette_a
(id INT PRIMARY KEY,
color VARCHAR2(100) NOT NULL);
CREATE TABLE palette_b
(id INT PRIMARY KEY,
color VARCHAR2(100) NOT NULL);
创建表:
-
为表 palette_a 添加样例数据:{(1, ‘Red’), (2, ‘Green’), (3, ‘Blue’), (4, ‘Purple’)}。
Insert into palette_a(id,color) values(1, ‘Red’), (2, ‘Green’), (3, ‘Blue’), (4, ‘Purple’);
-
为表 palette_b 添加样例数据:{(1, ‘Green’), (2, ‘Red’), (3, ‘Cyan’), (4, ‘Brown’)}。
Insert into palette_b(id,color) values(1, ‘Green’), (2, ‘Red’), (3, ‘Cyan’), (4, ‘Brown’);
-
查询两张表中相同颜色的所有信息。
SELECT * FROM palette_b inner JOIN palette_a ON palette_a.color= palette_b.color;
-
查询 palette_a 表中颜色不出现在 palette_b 表中的两张表的 id 和颜色(用左外连接)。
SELECT * FROM palette_a LEFT OUTER JOIN palette_b ON palette_a.color= palette_b.color;
-
查询 palette_b 表中颜色不出现在 palette_a 表中的两张表的 id 和颜色(用右外连接)。
SELECT * FROM palette_b RIGHT OUTER JOIN palette_a ON palette_a.color= palette_b.color;
7.查询(5)或(6)两种情况的信息(用(全)外连接)。
SELECT * FROM palette_a FULL OUTER JOIN palette_b ON palette_a.color= palette_b.color;
8查询产品表 products 中的 product_id, product_name, list_price 信息,要求产品定价 list_price 大于 其平均定价 list_price。
Select product_id,product_name,list_price from products where list_price>(select avg(list_price) from products);
9.查询产品表 products 中最便宜产品的 product_id, product_name, list_price。
Select product_id,product_name,list_price from products where list_price=(select min(list_price) from products);
10.查询没有一个订单的顾客姓名。 实现要求:NOT IN(必须)+其它查询方法(如果找到)
Select name from customers where customer_id not in (select customer_id from orders);
其他方法:
Select name from customers where customer_id !=all (select customer_id from orders);
11.查询产品表 products 中产品的 product_id, product_name, list_price,要求产品定价 list_price 大于 其同类产品(可由 category_id 表达)的平均定价。 实现要求:相关子查询(必须)+基于派生表的查询(如果找到)
相关子查询
Select product_id,product_name,list_price from products p1 where list_price>(select avg(list_price) from products p2 where p1.category_id=p2.category_id);
基于派生表的:
Select product_id,product_name,list_price from products p1,(select category_id,avg(list_price) avg_price from products group by category_id) p2 where p1.category_id=p2.category_id and p1.list_price>p2.avg_price;
12.查询有订单 order 的所有顾客 customer 姓名(查询涉及 customers 表和 orders 表)。 实现要求:使用 EXISTS(必须)+其它查询方法(如果找到)
Select name from customers where exists (select customer_id from orders where orders.customer_id=customers.customer_id);
13.执行以下两条语句,观察有何不同,能否得出某些初步结论?
In 不对Null进行处理
Customer_id为NULL的
显示全部信息
Select null from customers 返回true
14.找出所有没有订单的顾客姓名(查询涉及 customers 表和 orders 表)。 实现要求:使用 NOT EXISTS(必须)+其它查询方法(如果找到)
Select name from customers where not exists (select *from orders where customers.customer_id=orders.customer_id);
Select name from customers where customer_id not in (select customer_id from orders);
15.查询产品表 products 中的产品名 product_name 和定价 list_price,要求其定价高于产品种类 1 中 的任何产品定价。 实现要求:ANY(必须)+其它查询方法(如果找到)
Select product_name,list_price from products where list_price>any(select list_price from products p where p.category_id=1 );
Select product_name,list_price from products where list_price>(select min(list_price) from products p where p.category_id=1);
16.查询产品表 products 中的产品名 product_name 和定价 list_price,要求其定价高于产品种类 1 中 的所有定价。
Select product_name,list_price from products where list_price>all(select list_price from products where products.category_id=1);
17.查询产品表 products 中的产品名 product_name 和定价 list_price,要求其定价低于产品种类的所 有平均定价。
实现要求:ALL(必须)+其它查询方法(如果找到)
Select product_name,list_price from products where list_price<all(select avg(list_price) from products);
Select product_name,list_price from products where list_price<(select max(list_price)from products);
18.查询 contacts 表和 employees 表中的所有 last_name,并以 last_name 升序显示。 实现要求:去重+UNION(必须)+其它查询方法(如果找到)
Select last_name from contacts union Select last_name from employees;
19.查询 contacts 表和 employees 表中的所有 last_name,并以 last_name 升序显示。 实现要求:保留重复+UNION ALL(必须)+其它查询方法(如果找到)
Select last_name from contacts union all Select last_name from employees order by last_name asc;
20.查询同时出现在 contacts 表和 employees 表中的所有 last_name。 实现要求:INTERSECT(必须)+其它查询方法(如果找到)
Select last_name from contacts intersect Select last_name from employees;
Select contacts.last_name from contacts,employees where contacts.last_name=employees.last_name group by contacts.last_name,employees.last_name having count(*)=1;
21.查询在产品表 products 中而不在库存表 inventories 中的产品号 product_id。 实现要求:MINUS/EXCEPT(必须)+其它查询方法(如果找到)
Select product_id from products minus select product_id from inventories;
Select product_id from products except select product_id from inventories;
Select product_id from products where products.product_id not in (select product_id from inventories);