您好,欢迎访问三七文档
当前位置:首页 > IT计算机/网络 > 数据结构与算法 > 数据库实验-第二章课后题-SPJ表-SQL数据查询
针对数据库系统概论第四版第二章SQL实现--数据库系统原理表以及数据createtableS(SNOvarchar(8)notnullprimarykey,SNAMEnvarchar(20)unique,STATUSint,CITYnvarchar(20));createtableP(PNOvarchar(8)notnullprimarykey,PNAMEnvarchar(20),COLORnvarchar(10),WEIGHTint);createtableJ(JNOvarchar(8)notnullprimarykey,JNAMEnvarchar(30),CITYnvarchar(20));createtableSPJ(SNOvarchar(8),PNOvarchar(8),JNOvarchar(8),QTYint,primarykey(SNO,PNO,JNO),foreignkey(SNO)referencesS(SNO),foreignkey(PNO)referencesP(PNO),foreignkey(JNO)referencesJ(JNO));deletefroms;deletefromJ;deletefromP;deletefromSPJ;--S表:供应商表;insertintosvalues('S1','精益',20,'天津');insertintosvalues('S2','盛锡',10,'北京');insertintosvalues('S3','东方红',30,'北京');insertintosvalues('S4','丰泰盛',20,'天津');insertintosvalues('S5','为民',30,'上海');--P表:零件表:insertintoPvalues('P1','螺母','红',12);insertintoPvalues('P2','螺丝','绿',17);insertintoPvalues('P3','螺丝刀','蓝',14);insertintoPvalues('P4','螺丝刀','红',14);insertintoPvalues('P5','凸轮','蓝',40);insertintoPvalues('P6','齿轮','红',30);--J表:工程项目表:insertintoJvalues('J1','三建','北京');insertintoJvalues('J2','一汽','长春');insertintoJvalues('J3','弹簧厂','天津');insertintoJvalues('J4','造船厂','天津');insertintoJvalues('J5','机车厂','唐山');insertintoJvalues('J6','无线电厂','常州');insertintoJvalues('J7','半导体厂','南京');--SPJ表:供应情况表:insertintoSPJvalues('S1','P1','J1',200);insertintoSPJvalues('S1','P1','J3',100);insertintoSPJvalues('S1','P1','J4',700);insertintoSPJvalues('S1','P2','J2',100);insertintoSPJvalues('S2','P3','J1',400);insertintoSPJvalues('S2','P3','J2',200);insertintoSPJvalues('S2','P3','J4',500);insertintoSPJvalues('S2','P3','J5',400);insertintoSPJvalues('S2','P5','J1',400);insertintoSPJvalues('S2','P5','J2',100);insertintoSPJvalues('S3','P1','J1',200);insertintoSPJvalues('S3','P3','J1',200);insertintoSPJvalues('S4','P5','J1',100);insertintoSPJvalues('S4','P6','J3',300);insertintoSPJvalues('S4','P6','J4',200);insertintoSPJvalues('S5','P2','J4',100);insertintoSPJvalues('S5','P3','J1',200);insertintoSPJvalues('S5','P6','J2',200);insertintoSPJvalues('S5','P6','J4',500);insertintoSPJvalues('S1','P1','J2',5000);--①、求供应工程J1零件的供应商的号码SNO:selectdistinctSNO'供应商号码'fromSPJwhereJNO='J1';--②、求供应工程J1零件P1的供应商号码:SNO:selectdistinctSNO'供应商号码'fromSPJwhereJNO='J1'andPNO='P1';--③、求供应工程J1零件为红色的供应商号码SNO:selectdistinctSNO'供应商号码'fromSPJwhereJNO='J1'andexists(select*fromPwhereSPJ.PNO=P.PNOandp.COLOR='红');--④、求没有使用天津供应商生产的红色零件的工程号JNO:--selectdistinctSPJ.SNOfromS,P,SPJwhereS.SNO=SPJ.SNOandP.PNO=SPJ.PNOandP.COLOR='红'andS.CITY='天津';selectdistinctJNO'工程号'fromSPJwhereSNOnotin(selectdistinctSPJ.SNOfromS,P,SPJwhereS.SNO=SPJ.SNOandP.PNO=SPJ.PNOandP.COLOR='红'andS.CITY='天津');--⑤、求至少用了供应商S1供应的所有零件的工程号JNO:--也就是说:不存在一个JNO(工程号),它S1供应的零件它没有使用。--或者说:不存在一个JNO(工程号),不存在某个S1供应的零件,在此工程的零件列表中不存在。select*fromSPJ;--供应商S1供应的所有零件为:selectdistinctPNOfromSPJwhereSNO='S1';selectdistinctJNOfromSPJwherePNO='P1'andJNOin(selectJNOfromSPJwherePNO='P2');--第一个notexists对应的是不存在,第二个notexists对应的是供应商S1供应的零件它没有使用;selectdistinctJNOfromSPJwherenotexists(select*fromSPJSPJ1whereSNO='S1'andnotexists(select*fromSPJSPJ2whereSPJ2.PNO=SPJ1.PNOandSPJ2.JNO=SPJ.JNO));--第三章:第5题:--(1)找出所有供应商的姓名和所在城市;selectSNAME,CITYfromS;--(2)找出所有零件的名称、颜色、重量;selectPNAME,COLOR,WEIGHTfromP;--(3)找出使用供应商S1供应的所有零件的工程号码;selectdistinctJNOfromSPJwherenotexists(select*fromSPJSPJ1whereSNO='S1'andnotexists(select*fromSPJSPJ2whereSPJ2.PNO=SPJ1.PNOandSPJ2.JNO=SPJ.JNO));--(4)找出工程项目J2使用的各种零件的名称和重量;selectP.PNAME,P.WEIGHTfromSPJ,PwhereJNO='J2'andP.PNO=SPJ.PNO;--(5)找出上海厂商供应的所有零件号码;selectdistinctPNOfromSPJ,SwhereS.CITY='上海'andSPJ.SNO=S.SNO--(6)找出使用上海产的零件的工程名称;selectdistinctJ.JNAMEfromSPJ,JwhereJ.JNO=SPJ.JNOandSPJ.SNOin(selectSNOfromSwhereCITY='上海');--(7)找出没有使用天津产的零件的工程号码;selectdistinctJNOfromSPJwhereexists(select*fromSwhereS.SNO=SPJ.SNOandS.CITY!='天津');--(8)把全部红色零件的颜色改为蓝色;--select*fromP;updatePsetCOLOR='蓝'whereCOLOR='红';--(9)由S5供给J4的零件P6改为由S3供应,请做出必要的修改。--select*fromSPJwhereSNO='S3'andPNO='P6'andJNO='J4';updateSPJsetSNO='S3'whereSNO='S5'andPNO='P6'andJNO='J4';--(10)从供应商关系中删除S2的记录,并从供应情况关系中删除响应的记录;deletefromSPJwhereSNO='S2';deletefromSwhereSNO='S2';--(11)请将(S2,J6,P4,200)插入供应情况关系表中。insertintoSPJvalues('S2','P4','J6',200);--select*fromSPJ;
本文标题:数据库实验-第二章课后题-SPJ表-SQL数据查询
链接地址:https://www.777doc.com/doc-4593465 .html