第三章作业讲评 文洁 2012/4/10
完成情况 已交70份作业,9人未交作业: 王干、揭宇如、王庆一、李广耀、王嘉良、丁海韬、刘一鸣、邓捷 、王灏
第3题 创建四张表S, P, J, SPJ. CREATE TABLE S ( SNO varchar(2) PRIMARY KEY, SNAME varchar(10), STATUS smallint, CITY varchar(20) ) Primary key, not null, unique; CITY varchar(2); SPJ表的外码SNO参照S表的 SNO属性 CREATE TABLE SPJ ( … PRIMARY KEY(SNO, PNO, JNO), FOREIGN KEY (SNO) REFERENCES S(SNO) … )
第4题 (4) 求没有使用天津供应商生产的红色零件的工程号JNO SELECT JNO FROM P, S, SPJ WHERE S.SNO = SPJ.SNO AND P.PNO = SPJ.PNO AND S.CITY != ‘天津’ AND P.COLOR != ‘红’ SELECT JNO FROM J WHERE JNO NOT IN (SELECT JNO FROM SPJ, P, S WHERE S.SNO = SPJ.SNO AND P.PNO = SPJ.PNO AND S.CITY = ‘天津’ AND P.COLOR = ‘红’) J.JNO P.PNO P.COLOR S.SNO S.CITY … J1 红 天津 蓝 上海
第4题 (5) 求至少用了供应商S1供应的全部零件的工程号JNO SELECT DISTINCT JNO 1. SELECT JNO FROM SPJ WHERE SNO = ‘S1’ 2. SELECT JNO WHERE PNO=‘P1’ AND JNO IN (SELECT JNO WHERE PNO=‘P2’) SELECT DISTINCT JNO FROM SPJ X WHERE NOT EXISTS (SELECT * FROM SPJ Y WHERE SNO = ‘S1’ AND NOT EXISTS FROM SPJ Z WHERE Z.PNO=Y.PNO AND Z.JNO=X.JNO)
第5题 (1) 找出所有供应商的姓名和所在城市 SELECT S.SNAME, S.CITY FROM S SELECT S.SNO, SNAME FROM S, SPJ WHERE S.SNO = SPJ.SNO SELECT X.SNO, Y.JNO FROM SPJ X, SPJ Y
第5题 (6) 找出使用上海产的零件的工程名称 SELECT DISTINCT SNAME FROM SPJ, S, J WHERE SPJ.SNO = S.SNO AND SPJ.JNO = J.JNO AND S.CITY=‘上海’
第5题 (7) 找出没有使用天津产的零件的工程号码 SELECT JNO FROM J WHERE JNO NOT IN FROM SPJ, S WHERE S.SNO = SPJ.SNO AND S.CITY = ‘天津’ )
第5题 (10) 从供应商关系中删除S2的记录,并从供应情况关系中删除相应的 记录 SPJ S DELETE FROM SPJ WHERE SNO=‘S2’ DELETE FROM S SNO S1 S2 SNO S1 S2 S3
第5题 (11) 请将(S2, J6, P4, 200)插入SPJ表 INSERT INTO SPJ (SNO, JNO, PNO, QTY) VALUES (‘S2’, ‘J6’, ‘P4’, 200) 表名后面添加列名,默认为所有列且按照建表时的顺序排列各属性 字符类型的值需要加单引号(如CHAR, VARCHAR),数值类型不加(INT, DOUBLE)
补充题2.21 求伦敦供应商提供的零件的信息 SELECT * SELECT DISTINCT P.* FROM S WHERE S.CITY=‘伦敦’ SELECT DISTINCT P.* FROM S, SPJ, P WHERE SPJ.SNO=S.SNO AND SPJ.PNO=P.PNO AND S.CITY=‘伦敦’
补充题2.26 求由同一个供应商供应的零件号的对 SELECT DISTINCT FIRST.P#, SECOND.P# FROM SPJ FIRST, SPJ SECOND WHERE FIRST.S#=SECOND.S# AND FIRST.P#>SECOND.P# 例如:供应商S1供应了三种零件,P1,P2,P3。那么他的零件号的对就是(P1,P2),(P1,P3)和(P2,P3)。
注意事项 注意去重 Group by 的使用 SELECT P#, J#, SUM(QTY) FROM SPJ GROUP BY P#, J#
作业之星 陈一瑶、张微、仇浩波、徐晨灿、陈佳威、丁笛童、赵可君、胡婧 璇、王萌、常铭珊