以下是一个订货管理数据库,其中有仓库表、职工表、订购单表、供货商表。
仓库表:
仓库号 |
城市 |
面积 |
wh1 |
北京 |
370 |
wh2 |
上海 |
500 |
wh3 |
广州 |
200 |
wh4 |
武汉 |
400 |
职工表:
仓库号 |
职工号 |
工资 |
wh2 |
e1 |
1220 |
wh1 |
e3 |
1210 |
wh2 |
e4 |
1250 |
wh3 |
e6 |
1230 |
wh1 |
e7 |
1250 |
订购单表:
职工号 |
供应商号 |
订购单号 |
订购日期 |
e3 |
s7 |
or67 |
2001-6-23 |
e1 |
s4 |
or73 |
2001-7-28 |
e7 |
s4 |
or76 |
2001-5-25 |
e6 |
null |
or77 |
- - |
e3 |
s4 |
or79 |
2001-6-13 |
e1 |
null |
or80 |
- - |
e3 |
null |
or90 |
- - |
e3 |
s3 |
or91 |
2001-7-13 |
供应商表:
供应商号 |
供应商名 |
地址 |
s3 |
振华电子厂 |
西安 |
s4 |
华通电子公司 |
北京 |
s6 |
607厂 |
郑州 |
s7 |
爱华电子厂 |
北京 |
答案仅供参考:
create database 订货管理数据库
create table 仓库表(
仓库号 varchar(50) not null,城市 varchar(50) not null,面积 int not null,primary key(仓库号))
create table 职工表(
仓库号 varchar(50) not null,职工号 varchar(50) not null,工资 money not null,
primary key(职工号),foreign key(仓库号) references 仓库表(仓库号))
create table 订购单表(
职工号 varchar(50) not null,供应商号 varchar(50) ,订购单号 varchar not null,订购日期 datetime,
foreign key(职工号) references 职工表(职工号),foreign key(供应商号) references 供应商表(供应商号))
create table 供应商表(
供应商号 varchar(50) not null,供应商名 varchar(50) not null,地址 varchar(50) not null,primary key(供应商号))
insert into 仓库表(仓库号,城市,面积)
values(‘wh1‘,‘北京‘,370),
(‘wh2‘,‘上海‘,500),
(‘wh3‘,‘广州‘,200),
(‘wh4‘,‘武汉‘,400)
insert into 职工表(仓库号,职工号,工资)
values(‘wh2‘,‘e1‘,1220),
(‘wh1‘,‘e3‘,1210),
(‘wh2‘,‘e4‘,1250),
(‘wh3‘,‘e6‘,1230),
(‘wh1‘,‘e7‘,1250)
--修改变量范围
alter table 订购单表 alter column 订购单号 varchar(50)
insert into 订购单表(职工号,供应商号,订购单号,订购日期)
values(‘e3‘,‘s7‘,‘or67‘,‘2001-6-23‘),
(‘e1‘,‘s4‘,‘or73‘,‘2001-7-28‘),
(‘e7‘,‘s4‘,‘or76‘,‘2001-5-25‘),
(‘e6‘,null,‘or77‘,null),
(‘e3‘,‘s4‘,‘or79‘,‘2001-6-13‘),
(‘e1‘,null,‘or80‘, null ),
(‘e3‘,null,‘or90‘,null),
(‘e3‘,‘s3‘,‘or91‘,‘2001-7-23‘)
insert into 供应商表(供应商号,供应商名,地址)
values(‘s3‘,‘振华电子厂‘,‘西安‘),
(‘s4‘,‘华通电子公司‘,‘北京‘),
(‘s6‘,‘607厂‘,‘郑州‘),
(‘s7‘,‘爱华电子厂‘,‘北京‘)
--1
select 工资 from 职工表
--2
select * from 仓库表
--3
select 职工号 from 职工表 where 工资>1230
--4
select 仓库号 from 职工表 where 工资>1210
--5
select 职工号 from 职工表 where (仓库号=‘wh1‘ or 仓库号= ‘wh2‘) and 工资<1250
--6
select 职工号,城市 from 职工表,仓库表 where 工资>1230 and 职工表.仓库号=仓库表.仓库号
--7
select 职工号,城市 from 职工表,仓库表 where 职工表.仓库号=仓库表.仓库号 and 仓库表.面积>400
--8
select 城市 from 仓库表 where 仓库号 in (select 仓库号 from 职工表 where 工资=1250)
--9
select * from 仓库表 where 仓库号 not in(select 仓库号 from 职工表 where 工资<=1210) and 仓库号 in(select 仓库号 from 职工表)
--10
select 职工号 from 职工表 where 工资=(select 工资 from 职工表 where 职工号=‘e4‘)
--11
select * from 职工表 where 工资 between 1220 and 1240
--12
select * from 供应商表 where 供应商名 like ‘%公司‘
--13
select * from 供应商表 where 地址!=‘北京‘
--14
select * from 职工表 order by 工资 asc
--15
select * from 职工表 order by 仓库号 asc,工资 desc
--16
select COUNT(distinct 地址) as 地址数 from 供应商表
--17
select SUM(工资) as 工资总数 from 职工表
--18
select sum(工资) from 职工表 where 仓库号 in(select 仓库号 from 仓库表 where 城市=‘北京‘ or 城市=‘上海‘)
--19
select AVG(面积) as mavg from 仓库表 where 仓库号 not in(select 仓库号 from 职工表 where 工资<=1210) and 仓库号 in(select 仓库号 from 职工表)
--20
select MAX(工资) from 职工表 where 仓库号=‘wh2‘
--21
select 仓库号,AVG(工资) from 职工表 group by 仓库号
--22
select 仓库号,AVG(工资) from 职工表 group by 仓库号 having COUNT(*)>=2
--23
select * from 订购单表 where 供应商号 is null
--24
select * from 订购单表
left join 供应商表 on 订购单表.供应商号 = 供应商表.供应商号 where 订购单表.供应商号 is not null
--25
select distinct 供应商名 from 供应商表
--26
alter table 订购单表 add 总金额 money
--27
--28
select * from 仓库表 where 仓库号 not in (select 仓库号 from 职工表 )
--29
select * from 仓库表 where 仓库号 in (select 仓库号 from 职工表 group by 仓库号 having COUNT(职工号) >=1)
--30
select 仓库号 from 职工表 where 工资>any(select 工资 from 职工表 where 仓库号=‘wh1‘)
--31
select 仓库号 from 职工表 where 工资>=all(select 工资 from 职工表 where 仓库号=‘wh1‘)
原文:http://www.cnblogs.com/likaixuan/p/4360217.html