创建表(以其中一个user表举例):
create table [dbo].[user](
uid char(6) not null,
zip char(8),
address varchar(255),
name varchar(20),
primary key(uid),
);
插入数据(以其中custmor举例):
insert into customer values('0002','kitty','1999-03-20','1'),('0003','petter','1999-05-28','0');
创建视图:
create view order_detail as select ob.oid,ob.uid,u.name,p.pname,p.price,od.quantity
from order_basic ob,[dbo].[user] u,product p,order_details od
where u.uid=ob.uid and ob.oid=od.oid and od.pid=p.pid
查询:
1.
select * from customer c
where c.sex=0 and c.birth is NULL;
2.
select oid,COUNT(pid) type from order_details group by oid;
3.
select * from product p where p.price <= (select AVG(price) from product);
4.
select p.pid,p.pname from product p,order_details od
where p.pid = od.pid
group by p.pid,p.pname
having COUNT(p.pid) =(select COUNT(uid) from [dbo].[user]);
5.
select pid,pname from product
where pid not in(select pid from order_details);