目录
一、建表并插入数据
二、查询语句练习
一、建表并插入数据
- 一、建立如下 𝑆 表(命名格式“姓名拼音_三位学号_week7s”,如LBJ_023_week7s),并插入数据
create table LYL_116_week7s(SNO varchar(3),SNAME varchar(10),
CITY varchar(8),primary key(SNO));
insert into LYL_116_week7s values('S1','精益','天津')
insert into LYL_116_week7s values('S2','盛锡','北京')
insert into LYL_116_week7s values('S3','东方红','北京')
insert into LYL_116_week7s values('S4','丰盛泰','天津')
insert into LYL_116_week7s values('S5','为民','上海')
- 二、建立如下 𝑃 表(命名格式“姓名拼音_三位学号_week7p”,如LBJ_023_week7p),并插入数据
create table LYL_116_week7p(PNO varchar(3),PNAME
varchar(10),COLOR varchar(2),WEIGHT int,primary key(PNO))
insert into LYL_116_week7p values('P1','螺母','红',12)
insert into LYL_116_week7p values('P2','螺栓','绿',17)
insert into LYL_116_week7p values('P3','螺丝刀','蓝',14)
insert into LYL_116_week7p values('P4','螺丝刀','红',14)
insert into LYL_116_week7p values('P5','凸轮','蓝',40)
insert into LYL_116_week7p values('P6','齿轮','红',30)
- 三、建立如下 𝐽 表(命名格式“姓名拼音_三位学号_week7j”,如LBJ_023_week7j),并插入数据
create table LYL_116_week7j(JNO varchar(3),JNAME
varchar(10),JCITY varchar(8),primary key(JNO))
insert into LYL_116_week7j values('J1','三建','北京')
insert into LYL_116_week7j values('J2','一气','长春')
insert into LYL_116_week7j values('J3','弹簧厂','天津')
insert into LYL_116_week7j values('J4','造船厂','天津')
insert into LYL_116_week7j values('J5','机车厂','唐山')
insert into LYL_116_week7j values('J6','无线电厂','常州')
insert into LYL_116_week7j values('J7','半导体厂','南京')
- 四、建立如下 𝑆𝑃𝐽 表(命名格式“姓名拼音_三位学号_week7spj”,如LBJ_023_week7spj),并插入数据(注意建表时指定主键约束和外键约束)
create table LYL_116_week7spj(SNO varchar(3),PNO varchar(3),
JNO varchar(3),QTY int foreign key(SNO) references LYL_116_week7s(SNO),
foreign key(PNO) references LYL_116_week7p(PNO),
foreign key(JNO) references LYL_116_week7j(JNO))
insert into LYL_116_week7spj values('S1','P1','J1',200)
insert into LYL_116_week7spj values('S1','P1','J3',100)
insert into LYL_116_week7spj values('S1','P1','J4',700)
insert into LYL_116_week7spj values('S1','P2','J2',100)
insert into LYL_116_week7spj values('S2','P3','J1',400)
insert into LYL_116_week7spj values('S2','P3','J2',200)
insert into LYL_116_week7spj values('S2','P3','J4',500)
insert into LYL_116_week7spj values('S2','P3','J5',400)
insert into LYL_116_week7spj values('S2','P5','J1',400)
insert into LYL_116_week7spj values('S2','P5','J2',100)
insert into LYL_116_week7spj values('S3','P1','J1',200)
insert into LYL_116_week7spj values('S3','P3','J1',200)
insert into LYL_116_week7spj values('S4','P5','J1',100)
insert into LYL_116_week7spj values('S4','P6','J3',300)
insert into LYL_116_week7spj values('S4','P6','J4',200)
insert into LYL_116_week7spj values('S5','P2','J4',100)
insert into LYL_116_week7spj values('S5','P3','J1',200)
insert into LYL_116_week7spj values('S5','P6','J2',200)
insert into LYL_116_week7spj values('S5','P6','J4',500)
二、查询语句练习
- 1)找出所有供应商的名称和所在城市
select s.SNAME'供应商名称',s.CITY'所在城市'
from LYL_116_week7s s
- 2)找出所有零件的名称、颜色、重量
select p.PNAME'零件名称',p.COLOR'零件颜色',
p.WEIGHT'零件重量' from LYL_116_week7p p
- 3)找出使用供应商 𝑆1 所供应零件的工程号码
select spj.JNO'S1所供应零件的工程号码'
from LYL_116_week7spj spj where spj.SNO='S1'
- 4)找出工程项目 𝐽2 使用的各种零件的名称及其数量
select p.PNAME,spj.QTY from
LYL_116_week7spj spj,LYL_116_week7p p
where spj.JNO = 'J2' and spj.PNO = p.PNO;
- 5)找出上海厂商供应的所有零件号码
select spj.PNO'上海厂商供应的零件号码' from
LYL_116_week7s s,LYL_116_week7spj spj
where s.CITY = '上海' and spj.SNO = s.SNO
- 6)找出使用上海产的零件的工程名称
select j.JNAME from
LYL_116_week7s s,LYL_116_week7spj spj,LYL_116_week7j j
where s.CITY='上海' and s.SNO=spj.SNO and spj.JNO=j.JNO
- 7)找出没有使用天津产的零件的工程号码
select spj2.JNO from LYL_116_week7spj spj2
where spj2.JNO not in(
select spj1.JNO from LYL_116_week7spj spj1
where spj1.SNO in
(select s1.SNO from LYL_116_week7s s1 where s1.CITY='天津'))
- 8)把全部红色零件的颜色改成蓝色
update LYL_116_week7p set COLOR = '蓝' where COLOR = '红'
select * from LYL_116_week7p
- 9)由 𝑆5 供给 𝐽4 的零件 𝑃6 改为由 𝑆3 供应
update LYL_116_week7spj set SNO = 'S3'
where SNO = 'S5' and PNO = 'P6' and JNO = 'J4'
select * from LYL_116_week7spj spj
where spj.SNO='S5' or spj.SNO='S3'
- 10)删除所有绿色零件的供应情况
delete from LYL_116_week7spj where PNO in
(select p.PNO from LYL_116_week7p p
where p.COLOR = '绿')