数据库第三章作业及SQL上机实验标准答案

合集下载

大三sql课后习题答案

大三sql课后习题答案

第二章3.上机练习题02 程序代码如下:CREATE DATABASE STUDENT1 ON PRIMARY(NAME= STUDENT1_data, FILENAME='E:\DATA\',SIZE=3,MAXSIZE=unlimited, FILEGROWTH=15%)LOG ON(NAME= STUDENT1_log, FILENAME='E:\DATA\',SIZE=2,MAXSIZE=30,FILEGROWTH=2)03 程序代码如下:create database students on primary(name=students1,filename='E:\DATA\',size=5,maxsize=75,filegrowth=10%),(name= students12, filename='E:\DATA\',size=10,maxsize=75,filegrowth=1)log on(name=studentslog1, filename='E:\DATA\',size=5,maxsize=30,filegrowth=1),(name=studentslog2, filename='E:\DATA\',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(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 WITH CHECKADD CONSTRAINT FK_ book_author FOREIGN KEY(book_id) REFERENCES(book_id)02 程序代码如下:--利用Transact-SQL语句创建表booksales的代码。

大三 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的代码。

数据库第三章所有例题参考答案

数据库第三章所有例题参考答案

11级信管,保密,图档上机考试题目与参考答案3.3Simple Select Statements1.EXAMPLE 3.3.1find the aid values and names of agents that are based in New York. select aid, aname from agents where city=’New York’;2.EXAMPLE3.3.3Retrieve all pid values of parts for which orders are placed.select distinct pid from orders;3.EXAMPLE 3.3.4retrieve all customer-agent name pairs, (cname, aname), where the customer places an order through the agent.select distinct ame,agents.anamefrom customers,orders,agentswhere customers.cid=orders.cid and orders.aid=agents.aid;4.EXAMPLE 3.3.6all pairs of customers based in the same city.select c1.cid, c2.cidfrom customers c1, customers c2where c1.city = c2.city and c1.cid < c2.cid;5.EXAMPLE 3.3.7find pid values of products that have been ordered by at least twocustomers.select distinct x1.pidfrom orders x1, orders x2where x1.pid = x2.pid and x1.cid < x2.cid;6.EXAMPLE 3.3.8Get cid values of customers who order a product for which an order is also placed by agent a06.select distinct y.cidfrom orders x, orders ywhere y.pid = x,pid and x.aid = ‘a06’;3.4Subqueries7.EXAMPLE 3.4.1Get cid values of customers who place orders with agents in Duluth or Dallas.select distinct cid from orderswhere aid in (select aid from agentswhere city= ‘Duluth’ or city = ‘Dallas’)8.EXAMPLE 3.4.2to retrieve all information concerning agents based in Duluth or Dallas (very close to the Subquery in the previous example).select * from agentswhere city in (‘Duluth’, ‘Dallas’ );or select *from agentswhere city = ‘Duluth’ or city = ‘Dallas’;9.EXAMPLE 3.4.3to determine the names and discounts of all customers who place orders through agents in Duluth or Dallas.select distinct cname, discnt from customerswhere cid in (select cid from orders where aid in(select aid from agents where city in (‘Duluth’, ‘Dallas’ ))); 10.EXAMPLE 3.4.4to find the names of customers who order product p05.select distinct cname from customers, orderswhere customers.cid = orders.cid and orders.pid = ‘p05’or select disti nct cname from customers where ‘p05’ in(select pid from orders where cid = customers.cid);11.EXAMPLE 3.4.5Get the names of customers who order product p07 from agent a03. select distinct cname from customerswhere cid in (select cid from orders where pid = ‘p07’ and aid = ‘a03’) 12.EXAMPLE 3.4.6to retrieve ordno values for all orders placed by customers in Duluth through agents in New York.select ordno from orders x where exists(select * from customers c, agents awhere c.cid = x.cid and a.aid = x.aid and c.city = ‘Duluth’ anda.city=‘New York’);13.EXAMPLE 3.4.7find aid values of agents with a minimum percent commission.select aid from agents where percent = (select min(percent) from agents);14.EXAMPLE 3.4.8find all customers who have the same discount as that of any of the customers in Dallas or Boston.select cid, cname from customerswhere discnt = some (select discnt from customerswhere city = ‘Dallas’ or city = ‘Boston’);15.EXAMPLE 3.4.9Get cid values of customers with discnt smaller than those of any customers who live in Duluth.select cid from customerswhere discnt <all (select discnt from customerswhere city = ‘Duluth’);16.EXAMPLE 3.4.10Retrieve all customer names where the customer places an order through agent a05.select distinct ame from customers cwhere exists (select * from orders xwhere c.cid = x.cid and x.aid = ‘a05’);or select distinct ame from customers c, orders xwhere c.cid = x.cid and x.ai d = ‘a05’ ;17.EXAMPLE 3.4.11Get cid values of customers who order both products p01 and p07. select distinct cid from orders xwhere pid = ‘p01’ and exsits (select * from orderswhere cid = x.cid and pid = ‘p07’);orselect distinct x.cid from orders x, orders ywhere x.pid = ‘p01’ and x.cid = y.cid and y.pid = ‘p07’;18.EXAMPLE 3.4.12Retrieve all customer names where the customer does not place an order through agent a05.select distinct ame from customers cwhere not exists (select * from orders xwhere c.cid = x.cid and x.aid = ‘a05’);19.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 <>ALLpredicates in place of NOT EXISTS.select distinct ame from customers cwhere c.cid not in (select cid from orders where aid = ‘a05’);or select ame from customers cwhere c.cid <>all (select cid from orders where aid = ‘a05’);20.EXAMPLE 3.4.14Find cid values of customers who do not place any order through agent a03.select distinct cid from orders xwhere not exists (select * from orderswhere cid = x.cid and aid = ‘a03’);orselect cid from customers cwhere not exists (select * from orderswhere cid = c.cid and aid = ‘a03’);21.EXAMPLE 3.4.15Retrieve the city names containing customers who order product p01. select distinct city from customers where cid in(select cid from orders where pid = ‘p01’);or select distinct city from customers where cid =some(select cid from orders where pid = ‘p01’);or select distinct city from customers c where exsits(select * from orders where cid = c.cid and pid = ‘p01’);or select distinct city from customers c, orders xwhere x.cid = c.cid and x.pid = ‘p01’;or select distinct city from customers c where ‘p01’ in(select pid from orders where cid = c.cid);3.5UNION Operators and FOR ALL Conditions 22.EXAMPLE 3.5.1to create a list of cities where either a customer or an agent, or both, is based.select city from customersunion select city from agents;23.EXAMPLE 3.5.2Get the cid values of customers who place orders with all agents based in New York.select c.cid from customers cwhere not exsits(select * from agents awhere a.city = ‘New York’ and not exsits(select * from orders xwhere x.cid = c.cid and x.aid = a.aid));24.EXAMPLE 3.5.3Get the aid values of agents in New York or Duluth who place orders forall products costing more than a dollar.select aid from agents awhere (a.city = ‘New York’ or a.city = ‘Duluth’)and not exsits(select p.pid from products pwhere p.price > 1.00 and not exsits(select * from orders xwhere x.pid = p.pid and x.aid = a.aid));25.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.select a.aid from agents a where a.aid in(select aid from orders where pid = ‘p01’)and not exsits (select p.pid from products pwhere p.price > 1.00 and not exsits (select * from orders xwhere x.pid = p.pid and x.aid = a.aid));or select distinct y.aid from orders ywhere y.pid = ‘p01’ and not exsits(select p.pid from products pwhere p.price > 1.00 and not exsits(select * from orders xwhere x.pid = p.pid and x.aid = y.aid));26.EXAMPLE 3.5.6Find pid values of products supplied to all customers in Duluth.select pid from products pwhere not exsits(select c.cid from customers cwhere c.city = ‘Duluth’and not exists(select * from orders xwhere x.pid = p.pid and x.cid = c.cid));3.7 Set Functions in SQL27.EXAMPLE 3.7.1determine the total dollar amount of all orders.select sum(dollars) as totaldollars from orders28.EXAMPLE 3.7.2To determine the total quantity of product p03 that has been ordered. select sum(qty) as TOTAL from orders where pid=’p03’29.EXAMPLE 3.7.4Get the number of cities where customers are based.select count(distinct city) from customers30.EXAMPLE 3.7.5List the cid values of alt customers who have a discount less than the maximum discount.select cid from customerswhere discnt < (select max(discnt) from customers)31.EXAMPLE 3.7.6Find products ordered by at least two customers.select p.pid from products pwhere 2 <=(select count(distinct cid) from orders where pid=p.pid)图档的学生的上机考查的考题到此为止___________________________________________________________ ___________________________________________________________ 信管,保密的学生上机考查还包括下面的题目32.EXAMPLE 3.7.7Add a row with specified values for columns cid, cname, and city (c007, Windix, Dallas, null)to the customers table.insert into customers(cid, cname, city)values (‘c007’, ‘Windix’, ‘Dallas’)33.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 SQL34.EXAMPLE 3.8.1to calculate the total product quantity ordered of each individual product by each individual agent.select pid, aid, sum(qty) as TOTAL from ordersgroup by pid, aid35.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.select aname, a.aid, pname, p.pid, sum(qty)from orders x, products p, agents awhere x.pid = p.pid and x.aid = a.aid and x.cid in (‘c002’, ‘c003’)group by a.aid, a.aname, p.pid, p.pname36.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.select pid, aid, sum(qty) as TOTAL from ordersgroup by pid, aidhaving sum(qty) > 100037.EXAMPLE 3.8.4Provide pid values of all products purchased by at least two customers. select distinct pid from ordersgroup by pidhaving count(distinct cid) >= 23.9 A Complete Description of SQL Select38.EXAMPLE 3.9.1List all customers, agents, and the dollar sales for pairs of customers and agents, 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. select ame, c.cid, a.aname, a.aid, sum(dollars) as casalesfrom customers c, orders o, agents awhere c.cid = o.cid, and a.aid = o.aidgroup by ame, c.cid, a.aname, a.aidhaving sum(o.dollars) >= 900.00order by casales desc39.EXAMPLE 3.9.2listed the cid values of all customers with a discount less than the maximum discount.select cid from customerswhere discnt < (select max(discnt) from customers)40.EXAMPLE 3.9.3Retrieve the maximum discount of all customers.select max(discnt) from customers;select distinct discnt from customers cwhere discnt >= all (select discnt from customers dwhere d.cid<>c.cid)41.EXAMPLE 3.9.4Retrieve all data about customers whose cname begins with the letter “A”.select * from customers where cname like ‘A%’42.EXAMPLE 3.9.5Retrieve cid values of customers whose cname does not have a third letter equal to “%”.select cid from customers where cname not like ‘__[%]’43.EXAMPLE 3.9.6Retrieve cid values of customers whose cname begins “Tip_” and has an arbitrary number of characters following.select cid from customers where cname like ‘TIP\[_]%’44.EXAMPLE 3.9.7Retrieve cid values of customers whose cname starts with the sequence “ab\”.select cid from customers where cname like ‘ab\%’3.10 Insert, Update, and Delete Statements 45.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’)46.EXAMPLE 3.10.2Create a new table called swcusts of Southwestern customers, and insert into it all customers from Dallas and Austin.create table swcusts (cid char(4) not null,cname varchar(13),city varchar(20),discnt real);insert into swcustsselect * from customerswhere city in (‘Dallas’, ‘Austin’)47.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’48.EXAMPLE 3.10.4Give all customers who have total orders of more than $1000 a 10% increase in the discnt.update agents set percent = 1.1 * discntwhere cid in(select cid from orders group by cid having sum(dollars) > 1000) 49.EXAMPLE 3.10.6Delete all agents in New York.delete from agents where city = ‘New York’50.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 aidHaving sum(dollars)<600)51.EXAMPLE 3.11.2Retrieve the names of customers who order products costing $0.50. delete from agents where aid in(select aid from orders group by aid having sum(dollars)<600)(完)。

数据库上机实验题目和答案

数据库上机实验题目和答案

试用SQL的查询语句表达下列查询:1.检索王丽同学所学课程的课程号和课程名。

select Cno ,Cname from c where Cno in(select cno from sc where sno in (select sno from s where sname='王丽' ))2.检索年龄大于23岁的男学生的学号和姓名。

select sno,sname from swhere sex='男' and age>233.检索‘c01’课程中一门课程的女学生姓名select sname from swhere sex='女' and sno in(select sno from sc where cno='c01')4.检索s01同学不学的课程的课程号。

select cno from cwhere cno not in (select cno from sc where sno ='s01')5.检索至少选修两门课程的学生学号。

select sc.sno from s,scwhere s.sno=sc.snogroup by sc.snohaving count(o)>=26.每个学生选修的课程门数。

解法一:select so.sno sno,ount,s.snamefrom(select sc.sno sno,count(sc.sno) ccountfrom sc,swhere s.sno=sc.snogroup by sc.sno ) so,swhere s.sno=so.sno解法二:select sc.sno sno,s.sname,count(sc.sno) ccountfrom sc,swhere s.sno=sc.snogroup by sc.sno,sname7.求选修C4课程的学生的平均分。

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

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

三、设计题1.(1)SELECT BAuth FROM Book, PublishWHERE Book.PNo= Publish.PNo AND BName=’操作系统’ AND PName=’高等教育出版社’(2)查找为作者“张欣”出版全部“小说”类图书的出版社的电话。

SELECT PTel FROM Book, PublishWHERE Book.PNo= Publish.PNo AND BType =’小说’ AND BAuth=’张欣’(3)查询“电子工业出版社”出版的“计算机”类图书的价格,同时输出出版社名称及图书类别。

SELECT BPrice, PName, BType FROM Book, PublishWHERE Book.PNo= Publish.PNo AND PName =’电子工业出版社’ AND BType =’计算机’(4)查找比“人民邮电出版社”出版的“高等数学”价格低的同名书的有关信息。

SELECT * FROM BookWHERE BName =’高等数学’AND BPrice<ANY(SELECT BPrice FROM Book,PublishWHERE Book.PNo= Publish.PNo AND PName =’人民邮电出版社’ AND BName =’高等数学’)AND PName <>’人民邮电出版社’(5)查找书名中有“计算机”一词的图书的书名及作者。

SELECT BName, BAuth FROM BookWHERE BName LIKE’%计算机%’(6)在“图书”表中增加“出版时间”(BDate)项,其数据类型为日期型。

ALTER TABLE BookADD BDate datetime(7)在“图书”表中以“作者”建立一个索引。

CREATE INDEX Name ON Book(BAuth) desc2.(1)建立存书表和销售表。

【精选】数据库第三章课后习题

【精选】数据库第三章课后习题
order by grade
• 14、 • (1)GRANT SELECT ON 职工,部门TO 王明 • (2) GRANT INSERT,DELETE ON 职工,部门TO
李勇
• (3) GRANT SELECT ON 职工WHEN USER() = NAME TO ALL
• (4) GRANT SELECT,UPDATE(工资) ON 职工 TO 刘星
• 7、视图的优点 • 视图能够简化用户的操作 • 视图使用户能以多种角度看待同一数据 • 视图对重构数据库提供了一定程度的逻辑
独立性; • 视图能够对机密数据提供安全保护。
• 8、所有的视图是否都可以更新?
• 不是。视图是不实际存储数据的虚表,因 此对视图的更新,最终要转换为对基本表 的更新。因为有些视图的更新不能惟一有 意义地转换成对相应基本表的更新,所以 ,并不是所有的视图都是可更新的。
SPJ TO 李天明;
• 13、 • (1)INSERT INTO SC(Sno,Cno,Grade)
VALUES("2000012", "1128", NULL); • (2)SELECT Sno,Cno
FROM SC
WHERE Grade IS NULL;
• (3)SELECT cname,grade FROM course,Sc WHERE o=o AND cname="英语"
• (1) SELECT DIST PNO,QTY FROM SPQ
• (2) SELECT DIST * FROM SPQ WHERE SNO="S1‘
• 12、 • (1)GRANT INSERT
ON TABLE S TO 张勇

数据库系统及应用(SQL)第三次作业题及答案.doc

数据库系统及应用(SQL)第三次作业题及答案.doc

第3次作业一、填空题(本大题共20分,共10小题,每小题2分)1.SQL Server使用的数据库编程语言是__________。

2. _____ 是数据服务器方法支持的最自然必数据模型。

3.DBMS i访问程序找到有关的物理数据块(或页面)地址,向 ____________ 发出读块(页)操作命令。

4.乘积空间中的有限集合称为_________ ,无限集合称为_________5.一个基木的ODBC结构由_________ 、 ________ 、________ 和______ 四个部分组成。

6.SQL Sever 2000在安装过程中自动创建了6个数据库:master, model, msdb, tempdb, pubs 和Northwind,其屮______ , ________ , ______ , ________ 为系统数据库。

7.若要求分解具有无损连接性,那么分解一定可以达到 ____________ o& ________ 是用户与分布式数据库系统的接口。

根据构成各个局部数据库的DBMS及其数据模型,可以将分布式数据库系统分为两类:________________ 和9.Transact-SQL的数据类型分为_________ 和__________ 两大类,其中______ 是指系统捉供的数据类型,__________ 由基本数据类型导出。

10.抱共享同样屈性和方法的所冇对彖称为一个_____________ ,每个类冇一个______ ,所有的子类共有一个___________ o二、简答题(本大题共40分,共4小题,每小题10分)1.什么是宿主型DML和自主型DML。

2.什么是“数据建模” ?3.简述函数依赖的数学模型。

4.简述SQL语言的基本功能。

三、分析题(本大题共20分,共2小题,每小题10分)1.查询所有出版社的名称,如果它所在的州有书店,则一起显示书店的名称。

数据库上机实验题目和答案

数据库上机实验题目和答案

数据库上机实验题目和答案试用SQL的查询语句表达下列查询:1.检索王丽同学所学课程的课程号和课程名。

select Cno ,Cname from c where Cno in(select cno from sc where sno in (select sno from s where sname='王丽' ))2.检索年龄大于23岁的男学生的学号和姓名。

select sno,sname from swhere sex='男' and age>233.检索‘c01’课程中一门课程的女学生姓名select sname from swhere sex='女' and sno in(select sno from sc where cno='c01')4.检索s01同学不学的课程的课程号。

select cno from cwhere cno not in (select cno from sc where sno ='s01')5.检索至少选修两门课程的学生学号。

select sc.sno from s,scwhere s.sno=sc.snogroup by sc.snohaving count(/doc/1411529677.html,o)>=26.每个学生选修的课程门数。

解法一:select so.sno sno,/doc/1411529677.html,ount,s.sname from(select sc.sno sno,count(sc.sno) ccountfrom sc,swhere s.sno=sc.snogroup by sc.sno ) so,swhere s.sno=so.sno解法二:select sc.sno sno,s.sname,count(sc.sno) ccountfrom sc,swhere s.sno=sc.snogroup by sc.sno,sname7.求选修C4课程的学生的平均分。

数据库原理实验答案

数据库原理实验答案
Values(7,'PASCAL语言',6,4);
3)向SC表中插入数据
Insert Into SC(Sno,Cno, Grade) Values(200215121,1,92);
Insert Into SC(Sno,Cno, Grade) Values(200215121,2,85);
Insert Into SC(Sno,Cno, Grade) Values(200215121,3,88);
Insert Into SC(Sno,Cno, Grade) Values(200215122,2,90);
Insert Into SC(Sno,Cno, Grade) Values(200215122,3,80);
Insert Into SC(Sno,Cno, Grade) Values(200215121,4,92);
Insert Into SC(Sno,Cno, Grade) Values(200215121,5,85);
Insert Into SC(Sno,Cno, Grade) Values(200215121,6,88);
Insert Into SC(Sno,Cno, Grade) Values(200215123,2,90);
select sno,grade from sc where cno='3' order by grade desc;
8)查询各个课程号与相应的选课人数。
select cno, count(sno) from sc group by cno;
2. 连接查询操作。该实验包括等值连接、自然连接、求笛卡儿积、一般连接、外连接、内连接、左连接、右连接和自连接等。

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

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

数据库第三章部分习题答案3.2对于教学数据库的三个基本表s(s#,sname,age,sex)sc(s#,c#,grade)c(c#,cname,教师)试用sql的查询语句表达下列查询:3.2.1搜索17岁以下女生的学号和姓名,选择#,snamefromswhereage<17andsex=f;3.2.2检索男孩学习的课程编号和课程名称,从C中选择C#,CNAMEwherec#in(selectdistinctc#fromscwheres#in(selects#fromswheresex=m))3.2.3检索男生学习课程的教师的职务编号和姓名selectt#,tnamefromt其中#in(从C中选择distinctt#wherec#in(selectdistinctc#fromsc#in(选择)在哪里#froms其中性别=1);3.2.4检索至少选修两门课程的学生的学号挑选#fromscgroupbys#拥有计数(c#)>=2;3.2.5检索至少有学号为s2和s4所学的课程和课程名selectc#,cnamefromc其中c#in((选择c#fromscwheres#='s2')intersect(选择C#fromsc#='s4')3.2.6检索‘wang’同学不学的课程号从CEXcept中选择C#(selectdistinctc#fromscwheres#=(selects#fromswheresname='wang'));3.2.7检索所有学生的课程号和课程名称selectc#,cname弗洛姆wherenotexists(selects#弗洛姆wherec.c#notin(selectc#fromscwheresc.s#=s.s#));3.2.8检索选修课程包括“刘”老师教授的所有课程的学生的学号和姓名。

选择#,snamefromswherenotexists((selectc#弗洛姆wheret#=(selectt#fromtwhere name='liu'))除了(选择C#fromsc wheresc.s#=s.s#));3.4有两个基本表R(a、B、c)和S(a、B、c)。

数据库作业第三章习题答案

数据库作业第三章习题答案

数据库作业第三章习题答案数据库作业第三章习题答案数据库作业是数据库课程中非常重要的一部分,通过完成作业可以帮助学生巩固和加深对数据库知识的理解和应用。

第三章习题主要涉及数据库设计和查询语言的使用。

在本篇文章中,我们将回答第三章习题,并探讨一些相关的概念和技巧。

1. 设计一个关系模式,用于存储学生的基本信息,包括学生编号、姓名、性别、年龄和专业。

请给出该关系模式的定义。

答案:学生(学生编号,姓名,性别,年龄,专业)2. 设计一个关系模式,用于存储课程的信息,包括课程编号、课程名称和学分。

请给出该关系模式的定义。

答案:课程(课程编号,课程名称,学分)3. 设计一个关系模式,用于存储学生选课的信息,包括学生编号、课程编号和成绩。

请给出该关系模式的定义。

答案:选课(学生编号,课程编号,成绩)4. 编写一个SQL查询语句,查询学生的姓名和年龄。

答案:SELECT 姓名, 年龄 FROM 学生;5. 编写一个SQL查询语句,查询选修了某门课程的学生的姓名和成绩。

答案:SELECT 学生.姓名, 选课.成绩FROM 学生, 选课WHERE 学生.学生编号 = 选课.学生编号AND 选课.课程编号 = '某门课程编号';6. 编写一个SQL查询语句,查询某个学生的选课情况,包括课程名称和成绩。

答案:SELECT 课程.课程名称, 选课.成绩FROM 课程, 选课WHERE 课程.课程编号 = 选课.课程编号AND 选课.学生编号 = '某个学生编号';通过以上习题的回答,我们可以看到数据库设计和查询语言的基本应用。

关系模式的定义是数据库设计的基础,它描述了数据表的结构和属性。

在查询语言的使用中,我们可以通过SELECT语句来检索和过滤数据,通过WHERE子句来指定查询条件。

除了上述习题的答案,我们还可以进一步探讨数据库设计的一些原则和技巧。

例如,为了提高数据库的性能和可扩展性,我们可以使用索引来加快数据的检索速度。

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

数据库答案  第三章习题参考答案
8
或: Select jno from j where not exists (Select * from spj,s where spj.jno=j.jno and spj.sno=s.sno and s.city=‘天津’);
9
此课件下载可自行编辑修改,供参考! 感谢您的支持,我们努力做得更好!
4
习题三 第5题
1. 找出所有供应商的姓名及其所在城市。 Select sname, city from s; 2. 找出所有零件的名称、颜色、重量。 Select pname, color, weight from p; 3.找出使用供应商S1所供应零件的工程项目代码。 Select jno from spj where sno=‘S1’;
color=‘红’; 或: Select sno from spj Where jno =‘J1’ and pno in
(Select pno from p where color=‘红’色零件的工程号JNO。 Select jno From j Where not exists
(Select sno from s where city=‘上海’);
6
6. 找出使用上海产的零件的工程项目名。 Select jname from j,spj,s where j.jno=spj.jno and spj.sno=s.sno
and s.city=‘上海’; 或: Select jname from j where jno in (Select jno from spj, s
where spj.sno=s.sno and s.city=‘上海’);
7
7. 找出没有使用天津产的零件的工程项目代码。 Select jno from j where not exists

数据库实验3答案

数据库实验3答案

实验三:交互式SQL语句的使用1、实验目的(1)掌握数据库对象的操作过程,包括创建、修改、删除(2)熟悉表的各种操作,包括插入、修改、删除、查询(3)熟练掌握常用SQL语句的基本语法2、实验平台使用SQL Server提供的Microsoft SQL Server Management Studio工具,交互式使用SQL语句。

3实验容及要求选择如下一个应用背景之一:●学生选课系统●习题3、4、和5中使用的数据库●其它你熟悉的应用(1)建立一个数据库和相关的表、索引、视图等数据库对象,练习对表、索引和视图的各种操作。

(2)要求认真进行实验,记录各实验用例及执行结果。

(3)深入了解各个操作的功能。

实验要求包括如下方面的容:3.1数据定义1.基本表的创建、修改及删除2.索引的创建3.视图的创建3.2数据操作完成各类更新操作包括:1.插入数据2.修改数据3. 删除数据3.3数据查询操作完成各类查询操作1.单表查询2.分组统计3. 连接查询4. 嵌套查询5. 集合查询3.4数据操作1.创建视图2.视图查询参考示例:建立一个学生选课数据库,练习对表、视图和索引等数据库对象的各种操作。

一、数据定义创建学生选课数据库ST,包括三个基本表,其中Student表保存学生基本信息,Course表保存课程信息,SC表保存学生选课信息,其结构如下表:表1. Student表结构表2. Course表结构表3. SC表结构1.创建、修改及删除基本表(1)创建Student表CREATETABLE Student(Sno CHAR(8)PRIMARYKEY,Sname CHAR(8),Ssex CHAR(2)NOTNULL,Sage INT,Sdept CHAR(20));(2)创建Course表CREATETABLE Course(o CHAR(4)PRIMARYKEY,ame CHAR(40)NOTNULL,Cpno CHAR(4),Ccredit SMALLINT,);(3)创建SC表CREATETABLE SC(Sno CHAR(8)FOREIGNKEY (Sno)REFERENCES Student(Sno), o CHAR(4),Grade SMALLINT,);(4)创建员工表EmployeeCREATETABLE Employee(编号CHAR(8)PRIMARYKEY,VARCHAR(8)notnull部门CHR(40),工资numeric(8,2),生日datetime,职称char(20),);指出该语句中的错误并改正后执行。

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

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

3-2 对于教务管理数据库的三个基本表S(SNO,SNAME, SEX, AGE,SDEPT)SC(SNO,CNO,GRADE)C(CNO,CNAME,CDEPT,TNAME)试用SQL的查询语句表达下列查询:⑴检索LIU老师所授课程的课程号和课程名。

⑵检索年龄大于23岁的男学生的学号和姓名。

⑶检索学号为200915146的学生所学课程的课程名和任课教师名。

⑷检索至少选修LIU老师所授课程中一门课程的女学生姓名。

⑸检索WANG同学不学的课程的课程号。

⑹检索至少选修两门课程的学生学号。

⑺检索全部学生都选修的课程的课程号与课程名。

⑻检索选修课程包含LIU老师所授课程的学生学号。

解:⑴SELECT C#,CNAMEFROM CWHERE TEACHER=’LIU’;⑵SELECT S#,SNAMEFROM SWHERE AGE>23 AND SEX=’M’;⑶SELECT CNAME,TEACHERFROM SC,CWHERE SC.C#=C.C# AND S#=’200915146’⑷SELECT SNAME (连接查询方式)FROM S,SC,CWHERE S.S#=SC.S# AND SC.C#=C.C# AND SEX=’F’ AND TEACHER=’LIU’;或:SELECT SNAME (嵌套查询方式)FROM SWHERE SEX=’F’AND S# IN(SELECT S#FROM SCWHERE C# IN (SELECT C#FROM CWHERE TEACHER=’LIU’))或:SELECT SNAME (存在量词方式)FROM SWHERE SEX=’F’ AND E XISTS(SELECT*FROM SCWHERE SC.S#=S.S#AND EXISTS(SELECT *FROM CWHERE C.C#=SC.C# AND TEACHER=’LIU’))⑸SELECT C#FROM CWHERE NOT EXISTS(SELECT *FROM S,SCWHERE S.S#=SC.S# AND SC.C#=C.C# AND SNAME=’WANG));⑹SELECT DISTINCT X.S#FROM SC AS X,SC AS YWHERE X.S#=Y.S# AND X.C#!=Y.C#;⑺SELECT C#.CNAMEFROM CWHERE NOT EXISTS (SELECT *FROM SWHERE NOT EXISTS(SELECT *FROM SCWHERE S#=S.S# AND C#=C.C#));⑻SELECT DISTINCT S#FROM SC AS XWHERE NOT EXISTIS(SELECT *FROM CWHERE TEAC HER=’LIU’ AND NOT EXISTS(SELECT *FROM SC AS YWHERE Y.S#=X.S# AND Y.C#=C.C#));3-3 试用SQL查询语句表达下列对3.2题中教务管理数据库的三个基本表S、SC、C查询:⑴统计有学生选修的课程门数。

数据库第三章习题及答案

数据库第三章习题及答案
学生关系 S(S#,SNAME,AGE,SEX) 学习关系 SC(S#,C#,GRADE) 课程关系 C(C#,CNAME) 其中 S#、C#、SNAME、AGE、SEX、GRADE、CNAME 分别表示学号、课程号、姓名、年龄、性别、成绩和课程名。 用 SQL 语句表达下列操作。 (1)检索选修课程名称为 ’MATHS’ 的学生的学号与姓名。 (2)检索至少学习了课程号为 ’C1’ 和 ’C2’ 的学生的学号。 (3)检索年龄在 18 到 20 之间(含 18 和 20)的女生的学号、姓名和年龄。 (4)检索平均成绩超过 80 分的学生学号和平均成绩。
,不存放视图的
③ 。 答案:①一个或几个基本表 。
②插入’95031’班学号为 30、姓名为’郑和’的学生记录;

③将学号为 10 的学生姓名改为’王华’;

④将所有’95101’班号改为’95091’;

⑤删除学号为 20 的学生记录;

⑥删除姓’王’的学生记录;

答案:
①INSERT INTO R VALUES(25,’李明’,’男’,21,’95031’) ②INSERT INTO R(NO,NAME,CLASS) VALUES(30,’郑和’,’95031’) ③UPDATE R SET NAME=‘王华’ WHERE NO=10 ④UPDATE R SET CLASS=’95091’WHERE CLASS=’95101’ ⑤DELETE FROM R WHERE NO=20 ⑥DELETE FROM R WHERE NAME LIKE’王%’
(4) SELECT S# ,AVG(GRADE) FROM SC GROUP BY S# HAVING AVG(GRADE)>80

数据库原理第三章课后习题答案

数据库原理第三章课后习题答案

第三章作业一、试述SQL特点SQL集数据查询、数据操纵、数据定义和数据控制功能于一体,其主要特点包括以下几部分。

1.综合统一2.高度非过程化3.面向集合的操作方式4.以同一种语法结构提供多种使用方式5.语言简洁,易学易用二、设有两个关系S(A,B,C,D)和T(C,D,E,F),写出与下列查询等价的SQL表达式(1)select A,B,S.C, S.D,E,Ffrom S,Twhere S.C=T.C(2)select * from S,Twhere S.C=T.C三、设关系RA B C10 NULL 2020 30 NULL写出查询语句SELECT * FROM R WHERE X的查询结果,其中X分别为1.1 A IS NULL;1.2 A>8 AND B<20;1.3 A>8 OR B<20;1.4 C+10>25;1.5 EXISTS (SELECT B FROM R WHERE A=10);use Rcreate table R(A tinyint primary key,B tinyint,C tinyint)1.11.21.31.41.5四、基于教材中的学生-课程数据库,用SQL完成如下查询:2.1 创建一张新表,记录每个学生的学号、选课门数和总学分数。

格式如下SCC(sno, totalCourse, totalCredit)并插入每个学生相应的数据。

create table SCC( sno char(10),totalcourse tinyint,totalcredit int)insertinto SCC(sno,totalcourse,totalcredit)select sc.sno,count(distinct o)as totalcourse,sum(ccredit)as totalcredit from sc,student,coursegroup by sc.snoselect*from SCC2.2、查询缺考和不及格课程多于3门的学生的学号和姓名select sc.sno,snamefrom student,scwhere exists(select snofrom scwhere grade<60 or grade=nullgroup by snohaving count(grade)>3)2.3 查询每个学生超过他自己选修课程平均成绩的课程号(写出3种以上类型的方法)(1)select cnofrom sc,(select sno,avg(grade)from sc group by sno)as avg_sc(avg_sno,avg_grade)where sc.sno=avg_sc.avg_sno and sc.grade>=avg_sc.avg_grade(2)select sno,cnofrom sc xwhere grade>=(select avg(grade)from sc ywhere y.sno=x.sno);2.4 查询同时选修了“数据库”和“数据结构”的学生的学号和姓名(写出5种以上类型方法)(1)select sno,snamefrom student,coursewhere cname='数据库'and sno in(select snofrom scwhere cname='数据结构')(2)select sc.sno,snamefrom student,course,scwhere student.sno=sc.sno and o=o and cname='数据库'intersectselect sc.sno,snamefrom student,course,scwhere student.sno=sc.sno and o=o and cname='数据结构';五、在上机实践过程中遇到过什么问题?解决方案是什么?。

数据库上机实验报告答案

数据库上机实验报告答案

数据库上机实验报告答案1.建立学生数据库模式学生表:student (sno 学号,sname 姓名,ssex 性别,sage 年龄,sdept 所在系)其中:sno 长度为4的字符串,为主码;sname 长度为8的字符串;ssex 长度为2的字符串,其值只取男、女;sage 整数,其值在0-150之间;sdept 长度为10的字符串。

2.建立课程数据库模式课程表:course ( cno课程号,cname课程名,ccredit学分)其中:cno 长度为4的字符串,为主码cname 长度为10的字符串,不能有重复课程名;ccredit 整数。

3.建立选课数据库模式。

选课表: sc (sno学号, cno课程号, grade成绩)其中:sno 长度为4的字符串,和student表sno外键关联,且级联删除cno 长度为4的字符串,course表cno外键关联,grade 整数,值或空或为0—100之间,(sno, cno) 联合作主码。

(1)创建上述三个表。

(2)将年龄的数据修改为15-30之间。

(3)为Student中sname添加列级完整性约束,不能为空。

(4)为SC建立按学号升序和课程号降序建立唯一索引.(5)在表student的sname字段建立一个升序索引。

(6)删除在表student的sname字段建立的索引。

(7)给student表增加一个地址(address)属性。

(8) 删除student表地址(address)属性。

(9)建立视图view1,要求有sno,sname,cname,grade四个字段。

1) create table studen(sno char(4) primary key,sname char(8),ssex char(2) check( ssex in('男','女')),sage int check(sage between 0 and 150),sdept char(10));create table course(cno char(4) primary key,cname char(10) unique,ccredit intcreate table sc(sno char(4),cno char(4),grade int check(grade between 0 and 100),primary key (sno,cno),foreign key(sno) references student(sno) on delete cascade, foreign key(cno) references course(cno));2) alter table studentadd constraint sage_con check(sage between 15 and 30);3) alter table studentmodify sname not null;4) create unique index index1on sc (sno asc,cno desc);5) create index index2on student(sname asc);6) drop index index2;7) alter table studentadd address char(30);8) alter table studentdrop column address;9) create view view1asselect A.sno,sname,cname,gradefrom student A,course B,sc Cwhere A.sno=C.sno and /doc/287635464.html,o=http://www.doczj .com/doc/287635464.html,o;(1)在上述三个表中输入若干记录。

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
DELETE FROM SPJ WHERE SNO=’S2’; DELETE FROM S WHERE SNO=’S2’;
(11)请将(S2, J6, P4, 200)插入供应情况。 INSERT INTO SPJ VALUES(S2, P4, J6, 200);
11.请为三建工程项目建立一个供应情况的视图,包括供应商代码(SNO)、零件 代码(PNO)、供应数量(QTY)。针对该视图完成下列查询: (1)找出三建工程项目使用的各种零件代码及其数量。 (2)找出供应商 S1 的供应情况。 建视图:
( SELECT * FROM SPJ WHERE SPJ.JNO=J.JNO AND SNO IN ( SELECT SNO FROM S WHERE CITY=’天津’ ) AND PNO IN ( SELECT PNO FROM P WHERE COLOR=’红’ )
); 或者
SELECT JNO FROM J WHERE NOT EXISTS (SELECT * FROM SPJ, S, P WHERE SPJ.JNO=J.JNO AND SPJ.SNO=S.SNO AND SPJ.PNO=P.PNO AND S.CITY=’天津’ AND P.COLOR=’红’ ); (5)求至少使用了供应商 S1 所供应的全部零件的工程号。 SELECT DISTINCT JNO
4.针对上题中建立的 4 个表试用 SQL 语言完成第二章习题 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;
CREATE VIEW V_SPJ AS SELECT SNO, PNO, QTY WHERE JNO=
( SELECT JNO FROM J WHERE JNAME=’三建’
); (1)SELECT PNO, QTY
FROM V_SPJ; (2)SELECT PNO, QTY
FROM V_SPJ WHERE SNO=’S1’;
FROM SPJ SPJZ WHERE NOT EXISTS (SELECT *
FROM SPJ SPJX WHERE SNO=’S1’ AND NOT EXISTS ( SELECT * FROM SPJ SPJY WHERE SPJY.PNO=SPJX.PNO AND SPJY.JNO=SPJZ.JNO ) ); 5.针对习题 3 中的 4 个表试用 SQL 语言完成以下各项操作: (1)找出所有供应商的姓名和所在城市。 SELECT SNAME, CITY FROM S; (2)找出所有零件的名称、颜色、重量。 SELECT PNAME, COLOR, WEIGHT FROM P; (3)找出使用供应商 S1 所供应零件的工程号码。 SELECT JNO FROM SPJ WHERE SNO=’S1’; (4)找出工程项目 J2 使用的各种零件的名称及数量。 SELECT , SPJ.QTY FROM P, SPJ WHERE P.PNO=SPJ.PNO AND SPJ.JNO=’J2’; (5)找出上海厂商供应的所有零件号码。 SELECT DISTINCT PNO FROM SPJ WHERE SNO IN
(
SELECT JNO FROM SPJ, S WHERE SPJ.SNO=S.SNO AND S.CITY=’上海’ ); (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=’天津’ )
( SELECT SNO FROM S WHERE CITY=’上海’
); (6)找出使用上海产的零件的工程名称。
SELECT JNAME FROM J, SPJ, S WHERE J.JNO=SPJ.JNO AND SPJ.SNO=S.SNO AND S.CITY=’上海’; 或者
SELECT JNAME FROM J WHERE JNO IN
第三章作业及 SQL 上机实验标准答案
3.用 SQL 语句建立第二章习题 5 中的 4 个表。 S表 CREATE TABLE S ( SNO CHAR(2) PRIMARY KEY, SNAME CHAR(20), STATUS INT, CITY CHAR(4) ); P表 CREATE TABLE P ( PNO CHAR(2) PRIMARY KEY, PNAME CHAR(20), COLOR CHAR(2), WEIGHT INT ); J表 CREATE TABLE J ( JNO CHAR(2) PRIMARY KEY, JNAME CHAR(20), CITY CHAR(4) ); SPJ CREATE TABLE SPJ ( SNO CHAR(2), PNO CHAR(2), JNO CHAR(2), QTY INT, PRIMARY KEY (SNO,PNO,JNO), FOREIGN KEY (SNO) REFERENCES S(SNO), FOREIGN KEY (PNO) REFERENCES P(PNO), FOREIGN KEY (JNO) REFERENCES J(JNO) );
); 或者
SELECT JNO FROM J WHERE NOT EXISTS
( SELECT * FROM SPJ, S WHERE SPJ.JNO=J.JNO AND SPJ.SNO=S.SNO AND
S.CITY=’天津’ );
(8)把全部红色零件的颜色改为蓝色。 UPDATE P SET COLOR=’蓝’ WHERE COLOR=’红’;
SELECT SNO FROM SPJ WHERE JNONO FROM P WHERE COLOR=’红’
); 或者
SELECT SNO FROM SPJ, P WHERE JNO=’J1’ AND SPJ.PNO=P.PNO AND COLOR=’红’; (4)求没有使用天津供应商生产的红色零件的工程号 JNO; SELECT JNO FROM J WHERE NOT EXISTS
(9)把 S5 供给 J4 的零件 P6 改为 S3 供应,请做必要修改。 UPDATE SPJ SET SNO=’S3’ WHERE SNO=’S5’ AND JNO=’J4’ AND PNO=’P6’;
(10)从供应关系中删除 S2 的记录,并从供应情况关系中删除相应的记录。 (请注意删除顺序)
相关文档
最新文档