创建客户信息表
create table tblorder(
orderid int identity(1,1) primary key,
orderdate date not null,
custname varchar(20) not null,
custtel varchar(20),
custaddr varchar(20)
);
insert into tblorder values('2011-05-01','张晓峰','13966777766','大连');
insert into tblorder values('2010-12-01','李梓旭','13888999988',null);
insert into tblorder values('2010-12-08','张忠南',null,'大连');
insert into tblorder values('2011-03-01','赵明明','13380809090','锦州');
insert into tblorder values('2011-03-05','李刚','13966666666','沈阳');
创建客户订单表
create table tblitem(
itemid int identity(1,1) primary key,
orderid int foreign key references tblorder(orderid) on delete cascade on update cascade,
product varchar(20) not null,
quantity int not null,
price numeric(10,2) not null
);
insert into tblitem values(1,'硬盘',10,200);
insert into tblitem values(1,'内存',10,200);
insert into tblitem values(2,'显示器',2,800);
insert into tblitem values(3,'内存',100,100);
0、查询客户信息
select * from tblorder;
1、查询所有订单信息。
select * from tblitem;
2、查询所有客户姓张的订单信息。
select * from tblorder where custname like '张%';
3、 查询所有客户不姓张的订单信息。
select * from tblorder where custname not like '张%';
4、 查询客户电话为空的订单信息;
select * from tblorder where custtel is null;
5、查询所有客户地址不为空的订单信息;
select * from tblorder where custaddr is not null;
6、查询客户地址不是大连的订单信息。
select * from tblorder where custaddr !='大连';
7.、查询客户地址是沈阳或锦州的订单信息。
select * from tblorder where custaddr in ('沈阳','锦州');
8、查询订单日期介于2010-01-01至2011-05-01的订单信息。
select * from tblorder where orderdate between '2010-01-01' and '2011-05-01';
9、查询订单日期不是介于2010-01-01至2011-05-01的订单信息。
select * from tblorder where orderdate not between '2010-01-01' and '2011-05-01';
10、查询订单明细表,显示所有购买内存的订单项目信息。
select * from tblitem where product ='内存';
11、查询订单明细表,按购买数量从大到小显示所有购买内存的订单项目信息。
select * from tblitem order by quantity desc;
12、查询订单明细表,显示购买数量大于且产品单价大于的订单项目信息。
select * from tblitem where price > 50.0 and quantity > 50;
13、查询订单明细表,查询每种商品的最大单价,要求数据商品名和最大单价。
select product,MAX(price) maxprice from tblitem group by product;
14、查询订单明细表,查询订货总量大于个的商品名称和订货量
select product,quantity from tblitem where quantity>50;
15 、查询订单表,按客户地址统计不同地区的客户数量。
select custaddr,COUNT(orderid) from tblorder group by custaddr;