可用以下语句快速创建SPJ数据库:
--以下为创建SPJ的语句 create database spj go use spj go create table S ( Sno char(4) primary key, Sname char(20) unique, Status smallint, City char(10) ); create table P ( Pno char(4) primary key, Pname char(20), Color char(10), Weight smallint, ); create table J ( Jno char(4) primary key, Jname char(20), City char(10) ); create table SPJ ( Sno char(4), Pno char(4), Jno char(4), Qty smallint, PRIMARY KEY (Sno,Pno,Jno), 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','东方红',30,'北京'); insert into S values('S4','丰泰盛',20,'天津'); insert into S values('S5','为民',30,'上海'); insert into P values('P1','螺母','红',12); insert into P values('P2','螺栓','绿',12); insert into P values('P3','螺丝刀','蓝',12); insert into P values('P4','螺丝刀','红',12); insert into P values('P5','凸轮','蓝',12); insert into P values('P6','齿轮','红',12); 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','J3',500); insert into SPJ values('S2','P3','J4',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要求这样的工程x,使( y)p→q为真。即,对于所有的零件y,满足逻辑蕴涵p→q:p表示谓词"供应商S1供应了零件y";q表示谓词"工程 x选用了零件y"。即,只要"供应商S1供应了零件y"为真,则"工程x选用了零件y" 为真。 逻辑蕴涵可以转换为等价形式:﹁ y(p∧﹁q) 它所表达的语义为:不存在这样的零件y,供应商S1供应了y,而工程x没有选用y。
SELECT DISTINCT JNO FROM SPJ AS SPJ1 WHERE NOT EXISTS (SELECT 1 FROM SPJ AS SPJ3 WHERE SNO='S1' AND NOT EXISTS (SELECT 1 FROM SPJ AS SPJ2 WHERE SPJ2.PNO=SPJ3.PNO AND SPJ2.JNO=SPJ1.JNO));