select *
from S
where A='10';
select A,B
from S;
select A,B,S.C,S.D,E,F
from S,T
where S.C=T.C and S.D=T.D;
select *
from S,T
where S.C=T.C;
select *
from S,T
where S.A<T.E;
select S.C,S.D,T.*
from S,T;
1.先把表建立出来然后把数据插入进去,在进行后面SQL语句操作。
create table S
(sno char(6) unique,
sname char(15),
status int,
city char(10)
);
create table P
(pno char(6) unique,
pname char(10),
color char(3),
weight int
);
create table J
(jno char(6) unique,
jname char(20),
city char(20)
);
create table SPJ
(sno char(6),
pno char(6),
jno char(6),
qty int,
foreign key(sno) references S(sno),
foreign key(pno) references P(pno),
foreign key(jno) references J(jno)
);
INSERT INTO S VALUES ('S1','精益',20,'天津');
INSERT INTO S VALUES ('S2','盛锡',10,'北京');
INSERT INTO S VALUES ('S3','东方红',20,'北京');
INSERT INTO S VALUES ('S4','丰泰盛',20,'天津');
INSERT INTO S VALUES ('S5','为民',20,'上海');
INSERT INTO P VALUES ('P1','螺母','红',12);
INSERT INTO P VALUES ('P2','螺栓','绿',17);
INSERT INTO P VALUES ('P3','螺丝刀','蓝',14);
INSERT INTO P VALUES ('P4','螺丝刀','红',14);
INSERT INTO P VALUES ('P5','凸轮','蓝',40);
INSERT INTO P VALUES ('P6','齿轮','红',30);
INSERT INTO J VALUES ('J1','三建','北京');
INSERT INTO J VALUES ('J2','一汽','长春');
INSERT INTO J VALUES ('J3','弹簧厂','天津');
INSERT INTO J VALUES ('J4','造船厂','天津');
INSERT INTO J VALUES ('J5','机车厂','唐山');
INSERT INTO J VALUES ('J6','无线电厂','常州');
INSERT INTO J VALUES ('J7','半导体厂','南京');
INSERT INTO SPJ VALUES ('S1','P1','J1',200);
INSERT INTO SPJ VALUES ('S1','P1','J3',100);
INSERT INTO SPJ VALUES ('S1','P1','J4',700);
INSERT INTO SPJ VALUES ('S1','P2','J2',100);
INSERT INTO SPJ VALUES ('S2','P3','J1',400);
INSERT INTO SPJ VALUES ('S2','P3','J2',200);
INSERT INTO SPJ VALUES ('S2','P3','J4',500);
INSERT INTO SPJ VALUES ('S2','P3','J5',400);
INSERT INTO SPJ VALUES ('S2','P5','J1',400);
INSERT INTO SPJ VALUES ('S2','P5','J2',100);
INSERT INTO SPJ VALUES ('S3','P1','J1',200);
INSERT INTO SPJ VALUES ('S3','P3','J1',200);
INSERT INTO SPJ VALUES ('S4','P5','J1',100);
INSERT INTO SPJ VALUES ('S4','P6','J3',300);
INSERT INTO SPJ VALUES ('S4','P6','J4',200);
INSERT INTO SPJ VALUES ('S5','P2','J4',100);
INSERT INTO SPJ VALUES ('S5','P3','J1',200);
INSERT INTO SPJ VALUES ('S5','P6','J2',200);
INSERT INTO SPJ VALUES ('S5','P6','J4',500);
select * from S;
select * from P;
select * from J;
select * from SPJ;
enmmm,数据好多,最后select查看一下,应该没什么问题。这里需要注意的是先把S,P,J表插入数据后,再对SPJ表插入数据,因为SPJ中有外码。
select sno
from SPJ
where jno='J1';
select sno
from SPJ
where jno='J1' and pno='P1';
select sno
from SPJ,P
where SPJ.pno=P.pno and color='红' and jno='J1';
select jno
from SPJ
where jno not in(
select jno
from SPJ,P,S
where S.city='天津' and color='红' and S.sno=SPJ.sno and P.pno=SPJ.pno
);
select pno
from SPJ
where sno='S1';
select jno
from SPJ
where pno='P1' and jno in(
select jno
from SPJ
where pno='P2'
);
--或者可以用连接查询
select pno
from SPJ
where sno='S1';
select x.jno
from SPJ x,SPJ y
where x.jno=y.jno and x.pno='P1' and y.pno='P2';
select sname,city
from S;
select pno,color,weight
from P;
select jno
from SPJ
where sno='S1';
select pname,qty
from SPJ,P
where jno='J2' and P.pno=SPJ.pno;
select pno
from SPJ,S
where city='上海' and S.sno=SPJ.sno;
select jname
from SPJ,S,J
where S.city='上海' and S.sno=SPJ.sno and J.jno=SPJ.jno;
select jno
from SPJ
where jno not in(
select jno
from SPJ,S
where S.sno=SPJ.sno and S.city='天津'
);
update P
set color='红'
where color='蓝';
update SPJ
set sno='S3'
where sno='S5' and jno='J4' and pno='P6';
delete
from S
where sno='S2';
delete
from SPJ
where sno='S2';
insert
into SPJ
values('S2','P4','J6',200);
1.按照题意,先建立出基于三建项目的视图。
create view v1_SPJ
as select sno,pno,qty
from SPJ
where jno in(
select jno
from J
where jname='三建'
);
select pno,qty
from v1_SPJ;
--视图消解
select pno,qty
from SPJ,J
where jname='三建' and SPJ.jno=J.jno;
enmmm,下面的 select 语句应该为视图消解后的基于基本表的 select 语句。
通过实验可以看出,验证一下视图消解,enmm,没毛病。。。
select *
from v1_SPJ
where sno='S1';
select sno,pno,qty
from SPJ,J
where SPJ.jno=J.jno and jname='三建' and sno='S1';
enmmm,同理,下面为视图消解后的基于基本表的查询,查询出来的一样。
总结:最近课有点小多,作业有的都写不过来啦,坚持一下,再坚持一下吧!!!