第三章(SQL查询)作业参考答案(P71

合集下载

大三 sql 课后习题答案

大三 sql 课后习题答案

第二章3.上机练习题02 程序代码如下:CREATE DATABASE STUDENT1ON PRIMARY(NAME= STUDENT1_data,FILENAME='E:\DATA\STUDENT1.mdf', SIZE=3,MAXSIZE=unlimited,FILEGROWTH=15%)LOG ON(NAME= STUDENT1_log,FILENAME='E:\DATA\STUDENT1.ldf', SIZE=2,MAXSIZE=30,FILEGROWTH=2)03 程序代码如下:create database studentson primary(name=students1,filename='E:\DATA\students1.mdf', size=5,maxsize=75,filegrowth=10%),(name= students12,filename='E:\DATA\students2.ndf', size=10,maxsize=75,filegrowth=1)log on(name=studentslog1,filename='E:\DATA\studentslog1.ldf', size=5,maxsize=30,filegrowth=1),(name=studentslog2,filename='E:\DATA\studentslog2.ldf', size=5,maxsize=30,filegrowth=1)第三章:3 上机练习题01 程序代码如下:-- 创建表book的Transact-SQL语句:USE test01GOCREATE TABLE book(book_id nchar(6)NOT NULL,book_name nchar(30)NULL,price numeric(10, 2)NULL,CONSTRAINT PK_book PRIMARY KEY CLUSTERED(book_id ASC))ON PRIMARY-- 创建表uthor的Transact-SQL语句:CREATE TABLE dbo.author(anthor_name nchar(4)NOT NULL,book_id nchar(6)NOT NULL,address nchar(30)NOT NULL)ON [PRIMARY]-- 设置book中的book_id为主键,author表中的book_id为外键ALTER TABLE dbo.author WITH CHECKADD CONSTRAINT FK_ book_author FOREIGN KEY(book_id) REFERENCES dbo.book (book_id)02 程序代码如下:--利用Transact-SQL语句创建表booksales的代码。

数据库第三章部分习题答案

数据库第三章部分习题答案

数据库第三章部分习题答案3.2 对于教学数据库的三个基本表 S(S#,SNAME,AGE,SEX) SC(S#,C#,GRADE)C(C#,CNAME,TEACHER)试用SQL的查询语句表达下列查询:3.2.1检索年龄小于17岁的女学生的学号和姓名 select s#,sname from Swhere age<17 and sex=F;3.2.2检索男生所学课程的课程号和课程名 select c#,cname from Cwhere c# in (select distinct c#from SCwhere s# in (select s# from S where sex=M))3.2.3检索男生所学课程的任课老师的工号和姓名select t#,tname from Twhere t# in(select distinct t# from Cwhere c# in(select distinct c# from SCwhere s# in(select s#from Swhere sex=1)));3.2.4检索至少选修两门课程的学生的学号select s#from SC group by s#having count(c#)>=2;3.2.5检索至少有学号为S2和S4所学的课程和课程名 select c#,cname from Cwhere c# in((select c#from sc where s#='S2') intersect(select c# from sc where s#='S4') );3.2.6检索‘WANG’同学不学的课程号select c# from c except(select distinct c#from scwhere s# =(select s# from s where sname='WANG'));3.2.7检索全部学生都选修的课程号和课程名select c#,cnamefrom cwhere not exists(select s#from swhere c.c# not in (select c# from sc where sc.s#=s.s# ));3.2.8检索选修课程包含'LIU'老师所授课程的全部课程的学生的学号和姓名 select s#,sname from swhere not exists((select c#from cwhere t#=(select t# from twhere tname='LIU')) except(select c# from sc wheresc.s#=s.s#) );3.4 设有两个基本表R(A,B,C)和S(A,B,C),试用SQL查询语句表达下列关系代数表达式:① R∪S ② R∩S ③ R-S ④ R×S ⑤πA,B(R) πB,C(S)⑥ π1,6(σ3=4(R×S)⑦π1,2,3(R ⑧ R÷πC(S) S) 3=3 解:① (SELECT * FROM R) UNION(SELECT * FROM S);② (SELECT * FROM R) INTERSECT(SELECT * FROM S);③ (SELECT * FROM R) MINUS(SELECT * FROM S);④ SELECT *FROM R, S;⑤ SELECT R.A, R.B, S.CFROM R, SWHERE R.B=S.B;⑥ SELECT R.A, S.CFROM R, SWHERE R.C=S.A;⑦ SELECT R.* (R.*表示R中全部属性)FROM R, SWHERE R.C=S.C;⑧ R÷πC(S)的元组表达式如下:{ t |(?u)(?v)(?w)(R(u)∧ S(v)∧ R(w)∧ w[1]=u[1] ∧w[2]=u[2] ∧ w[3]=v[3] ∧ t[1]=u[1] ∧ t[2]=u[2])}据此,可写出SELECT语句: SELECT A, BFROM R RXWHERE NOT EXISTS( SELECT * FROM SWHERE NOT EXISTS( SELECT * FROM R RYWHERE RY.A=RX.A AND RY.B=RX.B AND RY.C=S.C));3.6 试叙述SQL语言的关系代数特点和元组演算特点。

第三章 关系数据库标准语言SQL语言

第三章 关系数据库标准语言SQL语言

第三章关系数据库标准语言SQL语言一、选择题1. 在SQL语言中授权的操作是通过________________语句实现的。

C A.CREATEB.REVOKEC.GRANTD.INSERT2. 假定学生关系是S(S#,SNAME,SEX,AGE),课程关系是C(C#,CNAME,TEACHER),学生选课关系是SC(S#,C#,GRADE)。

要查找选修“COMPUTER”课程的“女”学生姓名,将涉及到关系____。

DA.S B.SC,C C.S,SC D.S,C,SC3. 在 MS SQL Server中建立了表 Student(no,name,sex,birthday),no为表的主码,其他属性的默认值为 null。

表中信息如图所示:能够正确执行的插入操作是________。

A No Name Sex Birthday 101 张丽丽女 1967/05/07 102 李芳女 1970/04/14103 王朋男 1982/10/27 A.INSERT INTO student (no,sex) VALUES(102,′男′) B.INSERT INTO student (name,sex) VALUES(′王中′,′男′)D.INSERT INTO student VALUES(106,′王中′,′男′,′1984/03/08′) C.INSERT INTO stude nt VALUES(102,′男′,′王中′,′1984/03/08′) 4. SQL语言中,删除一个表的命令是________。

B A. DELETE B. DROP C. CLEAR D. REMORE 5. 为数据表创建索引的目的是________ AA.提高查询的检索性能B.创建唯一索引C.创建主键D.归类6. 在SQL语言中,条件“RETWEEN 20 AND 30”表示年龄在20到30之间,且________A. 包括20岁和30岁B. 不包括20岁和30岁C. 包括20岁不包括30岁D. 不包括20岁包括30岁7. 为了使索引键的值在基本表中唯一,在建立索引语句中应使用保留字________ AA. UNIQUEB. COUNTC. DISDINCTD. UNION 8. 下面关于SQL语言的说法中,哪一种说法是错误的? ________ AA. 一个SQL数据库就是一个基本表B. SQL语言支持数据库的三级模式结构C. 一个基本表可以跨多个存储文件存放,一个存储文件可以存放一个或多个基本表D. SQL的一个表可以是一个基本表,也可以是一个视图二、简答题1. 什么是基本表?什么是视图?两者的区别和联系是什么?【解答】基本表是本身独立存在的表,在SQL中一个关系就对应一个表。

第3章部分习题与解答

第3章部分习题与解答

第三章习题4 .针对上题中建立的四个表试用SQL语言完成第2章习题5中的查询。

答案:(1)求供应工程J1零件的供应商号码SNO;SELECT SNO FROM SPJ WHERE JNO=‘J1’;(2)求供应工程J1零件P1的供应商号码SNO;SELECT SNO FROM SPJ WHERE JNO=‘J1’AND PNO=‘P1’;(3)求供应工程J1零件为红色的供应商号码SNO;SELECT SNO /*这是嵌套查询*/ FROM SPJ WHERE JNO=‘J1’AND PNO IN /*找出红色的零件的零件号码PNO */ (SELECT PNO FROM P /*从P表中找*/ WHERE COLOR=‘红’);或SELECT SNO FROM SPJ,P /*这是两表连接查询*/ WHERE JNO=‘J1’/*这是复合条件连接查询*/ AND SPJ.PNO=P.PNO AND COLOR=‘红’;(4)求没有使用天津供应商生产的红色零件的工程号JNO;*解析:第一种解法是使用多重嵌套查询,第二种方法的子查询是一个多表连接。

注意:从J表入手,以包含那些尚未使用任何零件的工程号。

SELECT JNO FROM JWHERE NOT EXISTS(SELECT * FROM SPJWHERE SPJ.JNO=J.JNO AND SNO IN /*天津供应商的SNO*/(SELECT SNO FROM SWHERE CITY=‘天津’) AND PNO IN /*红色零件的PNO*/(SELECT PNO FROM P WHERE COLOR=‘红’));或SELECT JNO FROM JWHERE NOT EXISTS(SELECT * FROM SPJ, S, PWHERE SPJ.JNO=J.JNO AND SPJ.SNO=S.SNO ANDSPJ.PNO=P.PNO AND S.CITY=‘天津’AND P. COLOR=‘红’);//注:本例中父查询和子查询均引用了Student表,可以像自身连接那样用别名将父查询中的Student表与子查询中的Student表区分开://(5)求至少用了供应商S1所供应的全部零件的工程号JNO (类似于P113例44)。

3-SQL语句练习题参考答案

3-SQL语句练习题参考答案

标准SQL语言一、选择题1、下面关于SQL标准的叙述中,不正确的是(B )。

A.SQL语言是集数据定义、数据操纵、数据控制功能为一体的语言。

B.SQL语言是一种高度过程化的语言。

C.SQL标准规定数据库是按三级模式结构构建。

D.SQL语言是关系型数据库的标准语言。

E.SQL语言是面向集合的语言。

2、SQL语言中,修改基本表结构的语句是( B )。

A.UPDATE B.ALTER C.DROP D.CREATE3、SQL语言中,删除基本表结构的语句是( C )。

A.DELETE B.ALTER C.DROP D.CREATE4、下面关于“视图”的叙述中,不正确的是( C )。

A.视图是一种“虚表”,它的数据被存放在基本表中。

B.视图提供了逻辑数据独立性。

C.不能通过视图来更新数据库中的数据。

D.视图能提供对数据的安全保护。

5、下面关于SELECT语句的叙述中,不正确的是(C)。

A.SELECT产生的结果是一个集合。

B.HA VING子句必须与GROUP BY子句一起使用。

C.可以省略FROM子句。

D.可以省略WHERE子句。

二、填空题1、在使用INSERT语句向一个表中插入元组时,“值列表”中值的个数、(顺序)、类型必须与“列名表”保持一致。

2、在向一个表中插入元组时,对于未指定默认值且(不能取空值)的字段必须赋值。

3、向表中插入元组时,主键的值不能取(NULL )值。

4、在使用DELETE语句时,如果不指定(where 条件)就会将整个表的数据删除。

5、视图是从一个或几个基本表或(视图)导出的表,它与基本表不同,是一个虚表。

三、判断题1、SQL语言是面向集合操作的语言。

√2、可以通过视图来查询数据,但不能通过视图来更新数据库中的数据。

×3、在SQL Server数据库系统中,向表中插入元组时,系统自动为具有标识属性的列赋值。

√4、在SQL Server数据库系统中,向表中插入元组时,对取值类型为timestamp(时间戳)的列不能赋值,系统自动赋值。

第三章SQL语言习题

第三章SQL语言习题

第三章SQL语⾔习题第三章SQL语⾔⼀、选择题:1、SQL语⾔是的语⾔,易学习。

A.过程化B.⾮过程化C.格式化D.导航式2、SQL语⾔是语⾔。

A.层次数据库B.⽹络数据库C.关系数据库D.⾮数据库3、SQL语⾔具有的功能。

A.关系规范化,数据操纵,数据控制B.数据定义,数据操纵,数据控制C.数据定义,关系规范化,数据控制D.数据定义,关系规范化,数据操纵4、SQL语⾔具有两种使⽤⽅式,分别称为交互式SQL和。

A.提⽰式SQL B.多⽤户SQLC.嵌⼊式SQL D.解释式SQL5、SQL语⾔中,实现数据检索的语句是。

A.SELECT B.INSERTC.UPDATE D.DELETE6、下列SQL语句中,修改表结构的是。

A.ALTER B.CREATEC.UPDATE D.DELETE7、SQL中,与“NOT IN”等价的操作符是。

A.=SOME B.<>SOMEC.=ALL D.<>ALL8、假设有三个基本表:学⽣表S、课程表C、学⽣选课表SC,它们的结构如下:S(S#,SN,SEX,AGE,DEPT)C(C#,CN)SC(S#,C#,GRADE)检索所有⽐“王华”年龄⼤的学⽣姓名、年龄和性别。

正确的SQL语句是。

A.SELECT SN,AGE,SEX FROM SWHERE AGE>(SELECT AGE FROM SWHERE SN=”王华”)B.SELECT SN,AGE,SEXFROM SWHERE SN=”王华”C.SELECT SN,AGE,SEXFROM SWHERE AGE>(SELECT AGEWHERE SN=”王华”)D.SELECT SN,AGE,SEXFROM SWHERE AGE>王华.AGE9、检索选修课程”C2”的学⽣中成绩最⾼的学⽣的学号。

正确的SELECT语句是。

A.SELECT S#FROM SCWHERE C#=”C2”AND GRADE>=(SELECT GRADE FROM SCWHERE C#=“C2”)B.SELECT S#FROM SCWHERE C#=”C2”AND GRADE IN(SELECT GRADE FROM SCWHERE C#=“C2”)C.SELECT S#FROM SCWHERE C#=”C2”AND GRADE NOT IN(SELECT GRADE FROM SCWHERE C#=“C2”)D.SELECT S#FROM SCWHERE C#=”C2”AND GRADE>=ALL(SELECT GRADE FROM SCWHERE C#=“C2”)10、检索学⽣姓名及其所选修课程的课程号和成绩。

SQL课后作业10题参考答案

假设每个职工可以在多个公司工作,检索每个职工的兼职公司数目和工资总数,显示E#,MUM,SUM_SALARY分别表示工号、公司数目和工资总数。

分析:涉及到表为works表;按照职工来统计公司的数目和工资总额select E#,count(c#) NUM,sum(salary) SUM_SALARY from works group by E#检索”联华公司”中低于本公司平均公司的职工工号和姓名.(10题2小题)(1)查找联华公司职工的平均工资。

方法一:连接查询实现select avg(salary) from works,comp where works.c#=comp.c# and cname='联华公司'方法二:嵌套查询实现Select avg(salary) from works Where c#=(select c# from comp Where cname='联华公司')------------------------------------5799.5(2)查找联华公司工资低于(1)的职工工号和姓名。

方法一:连接查询实现外层查询select emp.e#,enamefrom emp,works,compwhere emp.e#=works.e#and works.c#=comp.c#and cname='联华公司'and salary<(Select avg(salary) from worksWhere c#=(select c# from compWhere cname='联华公司'))方法二:嵌套查询实现外层查询(自己思考)3、检索工资高于其所在公司职工平均工资的所有职工的工号和姓名。

(1)先查找各公司的平均工资select c#,avg(salary) avg_salfrom worksgroup by c#(2) 检索所有职工的工号、姓名、公司,工资select emp.e#,ename,works.c#,salary,from emp,workswhere emp.e#=works.e#(3) 检索所有职工的工号、姓名、公司,工资,所在公司和平均工资select emp.e#,ename,works.c#,salary,a.c#,a.avg_salfrom emp,works,(select c#,avg(salary) avg_salfrom works group by c#) awhere emp.e#=works.e#and works.c#=a.c#and salary>avg_sal4、检索职工人数最多的公司的编号和名称(10题的4小题).(1)按照公司来统计公司的人数(涉及到works)select c#,count(e#) from worksgroup by c#(2) 从(1)的结果中查找人数最多的公司的人数select max(cont) from (select c#,count(e#) cont from worksgroup by c#) a(3) 根据(2)查询的结果,从(1)的结果中查找人数最多的公司的编号.select c# from (select c#,count(e#) cont from works group by c#) awhere cont=(select max(cont) from (select c#,count(e#) contfrom worksgroup by c#) a)(4)已知公司编号,在COMP表中查找公司编号和名称select c#,cname from compwhere c# in(select c# from (select c#,count(e#) cont from works group by c#) a where cont=(select max(cont) from (select c#,count(e#) contfrom worksgroup by c#) a))5、检索工资总额最小的公司的编号和名称。

SQL数据库第三章所有例题

3.3Simple Select StatementsEXAMPLE 3.3.1find the aid values and names of agents that are based in New York.EXAMPLE 3.3.3Retrieve all pid values of parts for which orders are placed.EXAMPLE 3.3.4retrieve all customer-agent name pairs, (cname, aname), where the customer places an order through the agent.EXAMPLE 3.3.5Retrieve a “table" based on the orders table, with columns ordno, cid, aid, pid, and profit, where profit is calculated from quantity and price of the product sold by subtracting 60% for wholesale cost, the discount for the customer, and the percent commission for the agent.EXAMPLE 3.3.6all pairs of customers based in the same city.EXAMPLE 3.3.7find pid values of products that have been ordered by at least two customers.EXAMPLE 3.3.8Get cid values of customers who order a product for which an order isalso placed by agent a06.3.4SubqueriesEXAMPLE 3.4.1Get cid values of customers who place orders with agents in Duluth or Dallas.EXAMPLE 3.4.2to retrieve all information concerning agents based in Duluth or Dallas (very close to the Subquery in the previous example).EXAMPLE 3.4.3to determine the names and discounts of all customers who place orders through agents in Duluth or Dallas.EXAMPLE 3.4.4to find the names of customers who order product p05.EXAMPLE 3.4.5Get the names of customers who order product p07 from agent a03.EXAMPLE 3.4.6to retrieve ordno values for all orders placed by customers in Duluth through agents in New York.EXAMPLE 3.4.7find aid values of agents with a minimum percent commission.EXAMPLE 3.4.8find all customers who have the same discount as that of any of the customers in Dallas or Boston.EXAMPLE 3.4.9Get cid values of customers with discnt smaller than those of any customers who live in Duluth.EXAMPLE 3.4.10Retrieve all customer names where the customer places an order through agent a05.EXAMPLE 3.4.11Get cid values of customers who order both products p01 and p07.EXAMPLE 3.4.12Retrieve all customer names where the customer does not place an order through agent a05.EXAMPLE 3.4.13retrieving all customer names where the customer does not place an order through agent a05, but using the two equivalent NOT IN and <>ALL predicates in place of NOT EXISTS.EXAMPLE 3.4.14Find cid values of customers who do not place any order through agent a03.EXAMPLE 3.4.15Retrieve the city names containing customers who order product p01.3.5UNION Operators and FOR ALL Conditions EXAMPLE 3.5.1to create a list of cities where either a customer or an agent, or both, is based.EXAMPLE 3.5.2Get the cid values of customers who place orders with all agents based inNew York.EXAMPLE 3.5.3Get the aid values of agents in New York or Duluth who place orders for all products costing more than a dollar.EXAMPLE 3.5.4Find aid values of agents who place orders for product p01 as well as for all products costing more than a dollar.EXAMPLE 3.5.5find cl d values for customers with the following property: if customer c006 orders a particular product, so does the customer under consideration.EXAMPLE 3.5.6Find pid values of products supplied to all customers in Duluth.3.6 Some Advanced SQL SyntaxEXAMPLE 3.6.1Request cid values of customers who order both products p01 andp07.EXAMPLE 3.6.2Retrieve all customer names where the customer does not place an order through agent a05.EXAMPLE 3.6.3Retrieve all customer names where the customer places at least two orders for the same product.EXAMPLE 3.6.4to retrieve all customers who purchased at least one product costing less than $0.50.3.7 Set Functions in SQL EXAMPLE 3.7.1determine the total dollar amount of all orders.EXAMPLE 3.7.2To determine the total quantity of product p03 that has been ordered.EXAMPLE 3.7.4Get the number of cities where customers are based.EXAMPLE 3.7.5List the cid values of alt customers who have a discount less than the maximum discount.EXAMPLE 3.7.6Find products ordered by at least two customers.EXAMPLE 3.7.7Add a row with specified values for columns cid, cname, and city (c007, Windix, Dallas, null)to the customers table.insertinto customers(cid,cname,city)values ('c007','Windix','Dallas')EXAMPLE 3.7.9After inserting the row (c007, Windix, Dallas, null) to the customers table in Example 3.7.7, assume that we wish to find the average discount of all customers.select avg(discnt)from customers3.8 Groups of Rows in SQLEXAMPLE 3.8.1to calculate the total product quantity ordered of each individual product by each individual agent.EXAMPLE 3.8.2Print out the agent name and agent identification number, and the product name and product identification number, together with the total quantity each agent supplies of that product to customers c002 and c003.EXAMPLE 3.8.3Print out all product and agent IDs and the total quantity ordered of the product by the agent, when this quantity exceeds 1000.EXAMPLE 3.8.4Provide pid values of all products purchased by at least two customers.EXAMPLE 3.8.5find the average, over all agents, of the maximum dollar sales made by each agent.3.9 A Complete Description of SQL Select EXAMPLE 3.9.1List all customers, agents, and the dollar sales for pairs of customers andagents, and order the result from largest to smallest sales totals. Retain only those pairs for which the dollar amount is at least equal to 900.00.EXAMPLE 3.9.2listed the cid values of all customers with a discount less than the maximum discount.EXAMPLE 3.9.3Retrieve the maximum discount of all customers.EXAMPLE 3.9.4Retrieve all data about customers whose cname begins with the letter “A”.EXAMPLE 3.9.5Retrieve cid values of customers whose cname does not have a third letter equal to “%”.EXAMPLE 3.9.6Retrieve cid values of customers whose cname begins “Tip_” and has an arbitrary number of characters following.EXAMPLE 3.9.7Retrieve cid values of customers whose cname starts with the sequence “ab\”.3.10 Insert, Update, and Delete Statements EXAMPLE 3.10.1Add a row with specified values to the orders table, setting the qty and dollars columns null.insert into orders (ordno, month, cid, aid, pid)values (1107, ‘aug’, ‘c006’, ‘a04’, ‘p01’)EXAMPLE 3.10.2Create a new table called swcusts of Southwestern customers, and insert into it all customers from Dallas and Austin.CREATE TABLE [dbo].[swcusts]([cid] [char](4) NOT NULL,[cname] [char](10) NULL,[city] [char](13) NULL,[discnt] [real] NULL,PRIMARY KEY([cid]))insert into swcustsselect * from customerswhere city in (‘Dallas’, ‘Austin’)EXAMPLE 3.10.3Give all agents in New York a 10% raise in the percent commission they earn on an order.update agents set percent = 1.1 * percent where city = ‘New York’EXAMPLE 3.10.4Give all customers who have total orders of more than $1000 a 10% increase in the discnt.Update customers set discnt =1.1* discntwhere cid in(selectcid from orders group by cid having sum(dollars) > 1000) EXAMPLE 3.10.5Update the discnt values in rows of the swcusts table created in Example 3.10.2 with more up-to-date di sent values from the customers table. (题目好像打错了) with more up-to-date di sent values?Update swcusts set discnt=(select discnt from customers where cid=swcust.cid)EXAMPLE 3.10.6Delete all agents in New York.delete from agents where city = ‘New York’EXAMPLE 3.10.7Delete all agents who have total orders of less than $600.delete from agents where aid in (select aid from ordersgroup by aid having sum(dollars)<600)3.11 The Power of the Select Statement EXAMPLE 3.11.2Retrieve the names of customers who order products costing $0.50.EXAMPLE 3.11.4find the average, over all agents, of the total dollar sales by agent.。

数据库答案 第三章习题参考答案

7
7. 找出没有使用天津产的零件的工程项目代码。 找出没有使用天津产的零件的工程项目代码。 Select jno from j where not exists (Select * from spj where spj.jno=j.jno and sno in (Select sno from s where city=‘天津’) city=‘天津了供应商 所供应的全部零件的工程号 求至少用了供应商S1所供应的全部零件的工程号 求至少用了供应商 所供应的全部零件的工程号JNO。 。 即查找:不存在这样的零件y,供应商S1供应了 供应了y,而工程x 即查找:不存在这样的零件 ,供应商 供应了 ,而工程 为选用y。 为选用 。 Select distinct jno From spj z Where not exists (select * from spj x where sno=‘S1’ and not exists (select * from spj y where y.pno=x.pno and y.jno=z.jno));
习题三
Select sno from spj Where jno=‘J1’;
第 4题
1.求供应工程 零件的供应商号码 求供应工程J1零件的供应商号码 求供应工程 零件的供应商号码SNO。 。
2.求供应工程 零件 的供应商号码 求供应工程J1零件 的供应商号码SNO。 求供应工程 零件P1的供应商号码 。 Select sno from spj Where jno=‘J1’ and pno=‘P1’;
1
3.求供应工程 零件为红色的供应商号码。 求供应工程J1零件为红色的供应商号码 求供应工程 零件为红色的供应商号码。 Select sno from spj, p Where spj.pno=p.pno and jno=‘J1’ and color=‘红 color=‘红’; 或: Select sno from spj Where jno =‘J1’ and pno in (Select pno from p where color=‘红’ ); color=‘红

SQL作业参考答案

1、设有以下三个关系:学生关系:student(sno,sname,dep,age,sex)选课关系:sc(sno,cno,grade)课程关系:course(cno,cname,score)1)建数据库,在数据库中建立以上三个关系。

要求:学生关系中:sno为主键,sname不允许为空,age默认值18,sex取值范围’男’、’女’、默认为’男’。

课程关系中:cno为主键,cname不为空,在cname上建立唯一索引。

选课关系中:sno、cno组合为主键,grade允许为空,sno为外键(参照学生关系的sno,级联删除、级联修改),cno为外键(参照课程关系的cno,限制删除、限制修改)。

2)试用SQL的查询语句表示下列查询。

(1)检索学分score等于4的所有课程的课程号和课程名。

select cno,cnamefrom coursewhere score=4;(2)检索年龄在20和23岁之间的学生的学号与姓名。

select sno,snamefrom studentwhere age between 20 and 23 ;(3)检索Wang同学不学的课程的课程号。

select cnofrom coursewhere cno not in(select cnofrom sc,studentwhere sc.sno =student.snoand student.sname='Wang');(4)检索所有姓李的学生情况。

select *from studentwhere sname like '李%';(5)检索所有学生情况及其选课情况。

(可以用f1键查看左外联接)select student.*,sc.*from student left outer join scon student.sno = sc.sno;(6)检索所有选修了课程的学生情况和选课情况。

select student.*,sc.*from student , scwhere student.sno = sc.sno;(7)检索至少选修两门课程的学生学号和姓名。

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

第三章(SQL查询)作业参考答案:(P71,P72)
2.设有学生选取修课程数据库:
S(S#, SNAME, AGE, SEX, DEPARTMENT, ADDRESS, BIRTHPLACE)
SC(S#, C#, GRADE)
C(C#, CNAME, TEACHER)
(1) 李老师所教的课程号、课程名称;
SELECT C#, CNAME;
FROM C;
WHERE TEACHER=”李”
(2) 年龄大于23岁的女学生的学号和姓名;
SELECT S#, SNAME;
FROM S;
WHERE AGE>23 AND SEX=”女”
(3) “李小波”所选修的全部课程名称;
SELECT CNAME;
FROM S, SC;
WHERE S.S#=SC.S# AND SNAME=’李小波’
(4) 所有成绩都在80分以上的学生姓名及所在系;
SELECT SNAME, DEPARTMENT;
FROM S, SC;
WHERE S.S#=SC.S# AND SC.S# ;
not in ( SELECT S#;
FROM SC;
WHERE GRADE<80 )
(5) 没有选修“操作系统”课的学生的姓名;
SELECT SNAME ;
FROM S ;
WHERE S# NOT IN
(SELECT S# ;
FROM SC, C ;
WHERE SC.C#=C.C# AND CNAME=’操作系统’ )
(6) 与“李小波”同乡的男生姓名及所在系;
SELECT SNAME, DEPARTMENT ;
FROM S ;
WHERE SEX=’男’ AND BIRTHPLACE= ;
(SELECT BIRTHPLACE ;
FROM S ;
WHERE SNAME=’李小波’)
或:
SELECT X. SNAME, X. DEPARTMENT ;
FROM S X , S Y ;
WHERE X. SEX=’男’ AND ;
X. BIRTHPLACE=Y. BIRTHPLACE ;
AND Y. SNAME=’李小波’)
(7) 英语成绩比数学成绩好的学生;
SELECT * ;
FROM S ;
WHERE S# IN ;
(SELECT S# ;
FROM SC X , SC Y ;
WHERE X . S#=Y . S# AND ;
X . GRADE>Y . GRADE ;
AND X . C# = (SELECT C# ;
FROM SC ;
WHERE CNAME=’英语’) ;
AND Y . C# = (SELECT C# ;
FROM SC ;
WHERE CNAME=’数学’) ;
)
(8) 选修同一门课程时,女生比男生成绩好的学生名单;
SELECT X1.SNAME;
FROM S X1, SC Y1;
WHERE X1.S#=Y1.S# AND;
X1.SEX=”女” AND;
Y1.GREAD>ALL;
(SELECT Y2.GREAD;
FROM S X2, SC Y2;
WHERE X2.S#=Y2.S# AND;
X2.SEX=”男” AND;
Y2.C#=Y1.C# )
(9) 至少选修两门以上课程的学生姓名、性别;
SELECT SNAME, SEX;
FROM S, SC ;
WHERE S.S#=SC.S# ;
GROUP BY SC.S# ;
HAVING COUNT(*)>=2
(10) 选修了李老师所讲课程的学生人数;
SELECT COUNT(*) AS 选修李老师课的人数 ;
FROM S ;
WHERE S# IN ;
(SELECT DIST S# ;
FROM S, SC ;
WHERE S.C#=SC.C# AND;
TEACHER=”李”)
(11) 没有选修李老师所讲课程的学生;
SELECT SNAME AS 未选修李老师课的学生 ;
FROM S ;
WHERE S# NOT IN ;
(SELECT DIST S# ;
FROM S, SC ;
WHERE S.C#=SC.C# AND;
TEACHER=”李”)
(12) “操作系统”课程得最高分的学生姓名、性别、所在系;
SELECT SNAME, SEX, DEAPARTMENT ;
FROM S, SC, C;
WHERE S.S#=SC.S# AND C.C#=SC.C# ;
AND CNAME=”操作系统” ;
AND GREAD= ;
(SELECT MAX(GREAD) ;
FROM SC, C;
WHERE SC.C#=C.C# ;
AND CNAME=”操作系统” )

3.在第二章的定货数据库中,用SQL语言查询下列问题:
定货数据库中的两个关系:
定单(定单号,货号,定货单位,售价,定购量,送货地点)
商品(货号,品名,库存量,仓库地点,进价)
(1) 阳光公司所定货物的名称及送货地点;
SELECT 品名, 送货地点 ;
FROM 定单, 商品 ;
WHERE 定单.货号=商品.货号 ;
AND 定货单位=”阳光公司”
(2) 售价低于进货单价的货物;
SELECT 货号, 品名 ;
FROM 定单, 商品 ;
WHERE 定单.货号=商品.货号 ;
AND 售价<进价
(3) 没有接到定单的货物;
SELECT 货号, 品名 ;
FROM 商品 ;
WHERE 货号 NOT IN ;
(SELECT 货号 ;
FROM 定单 )
(4) 库存中单价最高的货物;
SELECT 货号, 品名 ;
FROM 商品 ;
WHERE 进价= ;
(SELECT MAX(进价) ;
FROM 商品 )
(5) 发了两张定单以上的公司名称;
SELECT 定货单位 ;
FROM 商品 ;
GROUP BY 定货单位;
HAVING COUNT(*)>2
(6) 定货量超过2000件的单位;
SELECT 定货单位 ;
FROM 定单 ;
GROUP BY 定货单位;
HAVING SUM(定购量)>2000

相关文档
最新文档