问题涉及到的题目见《数据库系统概论》不在赘述
3.
(1)
(S)
select A
from S
where A=10
(2)
(S)
select A ,B
from S
(3)
S
T
select A,B,S.C,S.D,E,F
from S,T
where S.C=T.C and S.D =T.D
--自然连接是去掉重复的列属性,在连接选择所有的相同的属性
(4)
S T
select A,B S.C ,S.D ,T.C,T.D,E,F
from S,T
where S.C= T.C
(5)S T
select *
from S,T
where A<E
(6) x T
select S.C,S.D,E,F
from S,T
4
create table TEST.S(
Sno CHAR(9)PRIMARY KEY,
Sname CHAR(9) ,
Staus smallint,
city char(9)
)
insert into TEST.S
values('s1','精益',20,'天津')
insert into TEST.S
values('s2','盛锡',10,'北京')
insert into TEST.S
values('s3','东方红',30,'北京')
insert into TEST.S
values('s4','丰盛泰',20,'天津')
insert into TEST.S
values('s5','为民',30,'上海')
--s表
create table TEST.P(
Pno CHAR(9)PRIMARY KEY,
Pname CHAR(9) ,
Color char(9),
Weight smallint
)
insert into TEST.P
values('p1','螺母','红',12)
insert into TEST.P
values('p2','螺栓','绿',17)
insert into TEST.P
values('p3','螺丝刀','蓝',14)
insert into TEST.P
values('p4','螺丝刀','红','14')
insert into TEST.P
values('p5','凸轮','蓝',40)
insert into TEST.P
values('p6','齿轮','红',30)
--p表
create table TEST.J(
Jno CHAR(9)PRIMARY KEY,
Jname CHAR(9) ,
city CHAR(9),
)
insert into TEST.J
values('j1','三建','北京')
insert into TEST.J
values('j2','一汽','长春')
insert into TEST.J
values('j3','弹簧厂','天津')
insert into TEST.J
values('j4','造船厂','天津')
insert into TEST.J
values('j5','机车厂','唐山')
insert into TEST.J
values('j6','无线电','常州')
insert into TEST.J
values('j7','半导体','南京')
--J表
create table TEST.SPJ(
Sno CHAR(9),
Pno CHAR(9),
Jno char (9),
Qty smallint,
PRIMARY KEY (Sno,Pno,Jno),
FOREIGN KEY (Sno) REFERENCES TEST.S(Sno),
FOREIGN KEY (Pno) REFERENCES TEST.P(Pno),
FOREIGN KEY (Jno) REFERENCES TEST.J(Jno),
);
insert into TEST.SPJ
values('s1','p1','j1',200)
insert into TEST.SPJ
values('s1','p1','j3',100)
insert into TEST.SPJ values('s1','p1','j4',700)
insert into TEST.SPJ values('s1','p2','j2',100)
insert into TEST.SPJ values('s2','p3','j1',400)
insert into TEST.SPJ values('s2','p3','j2',200)
insert into TEST.SPJ values('s2','p3','j4',500)
insert into TEST.SPJ values('s2','p3','j5',400)
insert into TEST.SPJ values('s2','p5','j1',400)
insert into TEST.SPJ values('s2','p5','j2',100)
insert into TEST.SPJ values('s3','p1','j1',200)
insert into TEST.SPJ values('s3','p3','j1',200)
insert into TEST.SPJ values('s4','p5','j1',100)
insert into TEST.SPJ values('s4','p6','j3',300)
insert into TEST.SPJ values('s4','p6','j4',200)
insert into TEST.SPJ values('s5','p2','j4',100)
insert into TEST.SPJ values('s5','p3','j1',200)
insert into TEST.SPJ values('s5','p6','j2',200)
insert into TEST.SPJ values('s5','p6','j4',500)
--我真的是谁写的这个书
4.1
选择Sno
select distinct Sno
from TEST.SPJ
where Jno='j1'
4.2选择Sno
select distinct Sno
from TEST.SPJ
where Jno='j1' and Pno='p1'
4.3选择为j1供应红色零件的 Sno
select Sno
from TEST.SPJ
where Jno='j1' and Pno in(
select Pno
from TEST.P
where Color='红'
)
4.4
select SPJ.Jno
from TEST.SPJ
except
select SPJ.Jno
from TEST.SPJ
where exists
(
select Sno
from (
select *
from TEST.P join TEST.S on (city='天津' and Color='红')
)
as s_p
where SPJ.Sno=s_p.Sno and SPJ.Pno =s_p.Pno
)
(5)
select distinct Jno
from TEST.SPJ s1
where not exists
(
select *
from TEST.SPJ s2
where s2.Sno='s1' and not exists(
select *
from TEST.SPJ s3
where s3.Jno=s1.Jno and s2.Pno=s3.Pno
)
)
5
(1)供货商姓名所在地
select Sname ,city
from TEST.S
(2)
select Color ,Weight,Pname
from TEST.P
(3)
select Jno
from TEST.SPJ
where Sno='s1'
(4)
select Pno,Qty
from TEST.SPJ
where Jno='j2'
(5)
select distinct Pno
from TEST.SPJ
where Sno in(
select Sno
from TEST.S
where city='上海'
)
(6)
select distinct Jno
from TEST.SPJ s1
where s1.Pno in(
select distinct Pno
from TEST.SPJ
where Sno in(
select Sno
from TEST.S
where city='上海'
)
)
--第二种方法 使用exists
Select distinct Jno
from TEST.SPJ s1
where exists(
select Pno
from (
select Pno
from TEST.SPj s2
where Sno in (
select Sno
from TEST.S
where city='上海'
)
)
as spj_s
where exists(
select Jno
from TEST.Spj s3
where s3.Jno=s1.Jno and spj_s.Pno=s3.Pno
)
)
--感觉还是不用好
(7)
select distinct Jno
from TEST.Spj
except
select distinct Jno
from TEST.Spj
where Pno in (
select Pno
from TEST.Spj
where Sno in(
select Sno
from TEST.S
where city='天津'
)
)
---使用exists
select Jno
from TEST.Spj s1
where not exists(
select Pno
from(
select Pno
from TEST.Spj s2
where Sno in(
select Sno
from TEST.S
where city='天津'
)
)
as spj_s
where exists(
select *
from TEST.Spj s3
where s3.Jno=s1.Jno and S3.Pno=spj_s.Pno
)
)
(8)
update TEST.P
set Color='蓝'
where Color='红'
---看反了
(9)
update TEST.Spj
set Sno='s3'
where Sno='s5' and Pno='p6' and Jno='j2'
(10)
delete
from TEST.Spj
where Sno ='s2'
delete
from TEST.S
where Sno='s2'
(11)
insert into TEST.S
values('s2','盛',10,'北京')
insert into TEST.Spj
values('s2','p4','j6',200)
总结
一些语句还需要看之间写的博客才能想起来怎么用,看了同学的博客才想起来第七个还可以用not in。