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

合集下载

《数据库》第三章参考答案

《数据库》第三章参考答案

(1)检索 检索LIU老师所授课程的课程号、课程名。 老师所授课程的课程号、 检索 老师所授课程的课程号 课程名。
π CNO,CNAME(σTNAME =‘LIU’(C)) ,
(2) 检索年龄大于 岁的男学生的学号与姓名。 检索年龄大于23岁的男学生的学号与姓名 。 岁的男学生的学号与姓名
πsno,sname
[例 3.11]设有三个关系: 例 设有三个关系: 设有三个关系 学生关系: 学生关系 S(SNO,SNAME,AGE,SEX,SDEPT) ( , , , , ) 学习关系: 学习关系 SC(SNO,CNO,GRADE) ( , , ) 课程关系: 课程关系 C(CNO,CNAME,CDEPT,TNAME) ( , , , ) 试用关系代数表达式表示下列查询语句。 试用关系代数表达式表示下列查询语句。
(7)检索全部学生都选修的课程的课程号与 ) 课程名。 课程名。
πcno
(S))) )
,CNAME
(C
∞ ( πSNO,CNO(SC) , )
÷
π
SNO
(8)检索选修课程包含 )检索选修课程包含LIU老师所授 老师所授 课程的学生学号。 课程的学生学号。
π sno,CNO(SC)
÷πCNO(σTNAME =‘LIU’(C))
(σAGE>’23’ ∧ SEX=‘M’(s)) >
(3)检索学号为 学生所学课程的课程名与 )检索学号为S3学生所学课程的课程名与 任课老师名。 任课老师名。
πCNAME,TNAME(σSNO =‘S3’ ( sc∞c)) ,
( 4) 检索至少选修 ) 检索至少选修LIU老师所授课程中一门 老师所授课程中一门 课的女学生姓名。 课的女学生姓名。
πSNAME(σSEX=‘F’∧TNAME=‘LIU’ (s∞sc ∞c))

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

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

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)(完)。

数据库习题答案

数据库习题答案

《数据库习题答案》来自五星文库点这里,有很多篇《数据库习题答案》在线阅读本文:数据库习题答案导读:第三章习题,1.关系数据库设计理论,数据依赖范式和关系模式的规范化设计方法,其中数据依赖起着核心的作用,2.关系数据库中的关系模式至少要满足第一范式,如果每个属性值都是不可再分的最小数据单位,(2)试分析模式R的数据冗余问题,关系R中的C属性会存在在数据冗余,相应地原来存储在一张二维表内的数据就要分散存储到多张二维表中,第四章习题,A删除基本表B修改基本表中的数据,A数据项B 元组,C表D数据库第三章习题一、单项选择题1.在关系模型R中,函数依赖X→Y的语义是( B )A.在R的某一关系中,若两个元组的X值相等,则Y值也相等B.在R的每一关系中,若两个元组的X值相等,则Y值也相等C.在R的某一关系中,X值应与Y值相等D.在R的每一关系中,X值应与Y值相等2.设学生关系模式为:学生(学号,姓名,年龄,性别,成绩,专业),则该关系模式的主键是( B )A.性别 B.学号C.学号,姓名 D.学号,姓名,性别3.如果X→Y(Y不包含于X,且Y不能决定X)和Y→Z成立,那么X→Z成立。

这条规则称为( B )A.自反律 B.传递律C.伪传递律 D.增广律4.关系模式R?2NF,则R一定是(b )A.1NF B.3NF5.设一关系模式为:运货路径(顾客姓名,顾客地址,商品名,供应商姓名,供应商地址),则该关系模式的主键是( C )A.顾客姓名,供应商姓名,供应商地址 B.顾客姓名,商品名C.顾客姓名,供应商姓名,商品名 D.顾客姓名,顾客地址6.下列有关范式的叙述中正确的是( B )A.如果关系模式R?1NF,且R中主属性完全函数依赖于主键,则R是2NFB.如果关系模式 R?3NF,则R?2NF一定成立C.如果关系模式R?1NF,则只要消除了R中非主属性对主键的传递依赖,则R可转换成2NFD.如果关系模式R?1NF,则只要消除了R中非主属性对主键的部分依赖,则R可转换成3NF7.关系模式学生(学号,课程号,名次),若每一名学生每门课程有一定的名次,每门课程每一名次只有一名学生,则以下叙述中错误的是( B )A.(学号,课程号)和(课程号,名次)都可以作为候选键B.只有(学号,课程号)能作为候选键C.该关系模式属于第三范式D.该关系模式属于BCNF8.已知关系模式R(ABCD),F={A→C,B→C,C→D },则以下成立的是( B )A.A→B B.A→DC.AD→BC D.AC→BD9.如果X→Y且Z?U成立,那么XZ→YZ成立,这条规则称为( D )A.自反律 B.传递律`C.伪传递律 D.增广律10.能够消除多值依赖引起的冗余是( D )A.1NF B.2NF二、填空题1.关系数据库设计理论,数据依赖范式和关系模式的规范化设计方法。

数据库习题答案-3

数据库习题答案-3

《数据库习题答案》来自五星文库点这里,有很多篇《数据库习题答案》在线阅读本文:数据库习题答案导读:第三章习题,1.关系数据库设计理论,数据依赖范式和关系模式的规范化设计方法,其中数据依赖起着核心的作用,2.关系数据库中的关系模式至少要满足第一范式,如果每个属性值都是不可再分的最小数据单位,(2)试分析模式R的数据冗余问题,关系R中的C属性会存在在数据冗余,相应地原来存储在一张二维表内的数据就要分散存储到多张二维表中,第四章习题,A删除基本表B修改基本表中的数据,A数据项B 元组,C表D数据库第三章习题一、单项选择题1.在关系模型R中,函数依赖X→Y的语义是(B )A.在R的某一关系中,若两个元组的X值相等,则Y值也相等B.在R的每一关系中,若两个元组的X值相等,则Y值也相等C.在R的某一关系中,X值应与Y值相等D.在R的每一关系中,X值应与Y值相等2.设学生关系模式为:学生(学号,姓名,年龄,性别,成绩,专业),则该关系模式的主键是( B )A.性别B.学号C.学号,姓名D.学号,姓名,性别3.如果X→Y(Y不包含于X,且Y不能决定X)和Y→Z成立,那么X→Z成立。

这条规则称为( B )A.自反律B.传递律C.伪传递律D.增广律4.关系模式R2NF,则R一定是(b )A.1NF B.3NFC.BCNF D.4NF5.设一关系模式为:运货路径(顾客姓名,顾客地址,商品名,供应商姓名,供应商地址),则该关系模式的主键是( C )A.顾客姓名,供应商姓名,供应商地址B.顾客姓名,商品名C.顾客姓名,供应商姓名,商品名D.顾客姓名,顾客地址6.下列有关范式的叙述中正确的是(B )A.如果关系模式R1NF,且R中主属性完全函数依赖于主键,则R是2NFB.如果关系模式R3NF,则R2NF一定成立C.如果关系模式R1NF,则只要消除了R中非主属性对主键的传递依赖,则R可转换成2NFD.如果关系模式R1NF,则只要消除了R中非主属性对主键的部分依赖,则R可转换成3NF7.关系模式学生(学号,课程号,名次),若每一名学生每门课程有一定的名次,每门课程每一名次只有一名学生,则以下叙述中错误的是( B )A.(学号,课程号)和(课程号,名次)都可以作为候选键B.只有(学号,课程号)能作为候选键C.该关系模式属于第三范式D.该关系模式属于BCNF8.已知关系模式R(ABCD),F={A→C,B→C,C→D },则以下成立的是( B )A.A→B B.A→DC.AD→BC D.AC→BD9.如果X→Y且ZU成立,那么XZ→YZ成立,这条规则称为(D )A.自反律B.传递律`C.伪传递律D.增广律10.能够消除多值依赖引起的冗余是( D )A.1NF B.2NFC.3NF D.4NF二、填空题1.关系数据库设计理论,数据依赖范式和关系模式的规范化设计方法。

3数据库基本操作习题与答案

3数据库基本操作习题与答案

第三章数据库基本操作一、选择题1. 如果需要给当前表增加一个字段,应使用的命令是________。

A) APPEND B) INSERTC) EDIT D) MODIFY STRU2. 设表文件及其索引已打开,为了确保指针定位在物理记录号为1的记录上,应该使用命令________。

A) SKIP 1 B) SKIP -1C) GO 1 D) GO TOP3. 要显示数据库中当前一条记录的内容,可使用命令________。

A) LIST B) BROWSEC) TYPE D) DISPLAY4. 在当前表中,查找第2个女同学的记录,应使用命令________。

A) LOCATE FOR 性别="女"B) LOCATE FOR 性别="女" NEXT 2C) LIST FOR 性别="女"CONTINUED) LOCATE FOR 性别="女"CONTINUE5. Visual FoxPro的数据库表之间可建立两种联系,它们是________。

A) 永久联系和临时联系B) 长期联系和短期联系C) 永久联系和短期联系D) 长期联系和临时联系6. 数据库表的索引中,字段值不能有重复的索引有________种。

A) 1 B) 2C) 3 D) 47. 建立表间临时关联的命令是________。

A) LET RELATION TO命令B) JOIN命令C) SET RELATION TO命令D) 以上都不是8. 通过关键字建立表间的临时关联的前提是________。

A) 父表必须索引并打开B) 子表必须索引并打开C) 两表必须索引并打开D) 两表都不必索引9. 查询设计器的“筛选”选项卡上,“插入”按钮的作用是________。

A) 用于增加查询输出字段B) 用于增加查询的表C) 用于增加查询去向D) 用于插入查询输出条件10. 在多工作区的操作中,如果选择了4,7,8号工作区并打开了相应的数据库,在命令窗口执行命令SELECT 0,其功能是________。

数据库第三版课后习题答案

数据库第三版课后习题答案

数据库第三版课后习题答案数据库第三版课后习题答案在学习数据库的过程中,课后习题是巩固知识的重要方式。

本文将为大家提供数据库第三版课后习题的答案,希望对大家的学习有所帮助。

第一章:数据库基础1. 数据库是什么?答:数据库是一个存储、管理和操作数据的集合。

它可以用来存储和检索大量数据,并提供数据的安全性和完整性。

2. 数据库管理系统(DBMS)的作用是什么?答:DBMS是一种软件,用于管理数据库。

它提供了对数据库的访问和操作,包括数据的增删改查、数据安全性和完整性的控制、数据备份和恢复等功能。

3. 数据库系统的组成部分有哪些?答:数据库系统由数据库、数据库管理系统和应用程序组成。

数据库是存储数据的仓库,数据库管理系统用于管理和操作数据库,应用程序用于访问和处理数据库中的数据。

4. 什么是关系型数据库?答:关系型数据库是一种基于关系模型的数据库。

它使用表格(称为关系)来组织和存储数据,每个表格包含行和列,行表示记录,列表示属性。

第二章:SQL基础1. SQL是什么?答:SQL(Structured Query Language)是一种用于管理关系型数据库的语言。

它可以用来创建、修改和查询数据库中的数据。

2. SQL语句分为哪几类?答:SQL语句分为数据定义语言(DDL)、数据操作语言(DML)、数据查询语言(DQL)和数据控制语言(DCL)四类。

3. 什么是DDL语句?举例说明。

答:DDL语句用于定义数据库的结构和模式,包括创建表格、修改表格结构、删除表格等。

例如,创建表格的语句如下:CREATE TABLE student (id INT PRIMARY KEY,name VARCHAR(50),age INT);4. 什么是DML语句?举例说明。

答:DML语句用于操作数据库中的数据,包括插入、更新和删除数据。

例如,插入数据的语句如下:INSERT INTO student (id, name, age) VALUES (1, 'Tom', 20);第三章:关系数据库设计1. 什么是关系数据库设计?答:关系数据库设计是指根据实际需求,设计数据库的结构和模式。

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

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

三、设计题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)建立存书表和销售表。

《数据库技术与应用》第3章习题答案

《数据库技术与应用》第3章习题答案

第3章关系数据库1. 试述关系模型的三个组成部分。

解:关系模型的三个组成部分(1) 关系数据模型的数据结构(2) 关系数据模型的操纵与完整性约束(3) 关系数据模型的存储结构2. 解释下列术语的含义:①笛卡尔积;②主码;③候选码;④外码;⑤关系;⑥关系模式;⑦关系数据库解:①笛卡尔积:两个分别为n目和m目的关系R和S的笛卡尔积是一个(n+m)列的元组的集合。

元组的前n列是关系R的一个元组,后m列是关系S的一个元组。

若R有k1个元组,S有K2个元组,则关系R和关系S的笛卡尔积有k1×k2个元组。

记作:R×S={trts|tr∈R⋀ts∈S}②主码:若关系中的某一属性组的值能唯一的标识一个元组,则称该属性组为候选码。

若一个关系有多个候选码,则选定其中一个为主码。

③候选码:若关系中的某一属性组的值能唯一的标识一个元组,则称该属性组为候选码。

④外码:如果关系模式R中的某属性集是另一个关系模式S的主码,则该属性集为关系模式R的外码。

⑤关系:关系是集合论的一个概念,也是关系模型的数据结构,它只包含单一的数据结构——关系。

在关系模型中,现实世界的实体以及实体间的各种联系均用关系来表示。

在用户看来,一个关系就是一张二维表,这种简单的数据结构能够表达丰富的语义。

⑥关系模式:关系的描述称为关系模式。

它可以形式化地表示为R(U,D,DOM,F)其中R为关系名,U为组成该关系的属性名集合,D为属性组U中属性所来自的域,DOM为属性向域的映像集合,F为属性间数据的依赖关系集合。

⑦关系数据库:在关系模型中,实体以及实体之间的联系都是通过关系来表示的。

因此,在一个给定的应用领域中,所有实体以及实体之间的联系所对应的关系的集合就构成一个关系数据库。

3.关系数据库的三个完整性约束是什么?各是什么含义?解:关系模式中有3类完整性约束:实体完整性、参照完整性和用户自定义完整性。

实体完整性:若属性(指一个或一组属性)A是基本关系R的主属性,则A不能取空值。

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

数据库第三章部分习题答案
3.2 对于教学数据库的三个基本表 S(S#,SNAME,AGE,SEX) SC(S#,C#,GRADE)
C(C#,CNAME,TEACHER)
试用SQL的查询语句表达下列查询:
3.2.1检索年龄小于17岁的女学生的学号和姓名 select s#,sname from S
where age<17 and sex=F;
3.2.2检索男生所学课程的课程号和课程名 select c#,cname from C
where c# in (select distinct c#
from SC
where s# in (select s# from S where sex=M))
3.2.3检索男生所学课程的任课老师的工号和姓名
select t#,tname from T
where t# in(select distinct t# from C
where c# in(select distinct c# from SC
where s# in(select s#
from S
where 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 C
where 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 sc
where s# =(select s# from s where sname='WANG'));
3.2.7检索全部学生都选修的课程号和课程名
select c#,cname
from c
where not exists(select s#
from s
where c.c# not in (select c# from sc where sc.s#=s.s# ));
3.2.8检索选修课程包含'LIU'老师所授课程的全部课程的学生的学号和姓名 select s#,sname from s
where not exists((select c#
from c
where t#=(select t# from t
where tname='LIU')) except(select c# from sc where
sc.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.C
FROM R, S
WHERE R.B=S.B;⑥ SELECT R.A, S.C
FROM R, S
WHERE R.C=S.A;
⑦ SELECT R.* (R.*表示R中全部属性)
FROM R, S
WHERE 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, B
FROM R RX
WHERE NOT EXISTS
( SELECT * FROM S
WHERE NOT EXISTS
( SELECT * FROM R RY
WHERE RY.A=RX.A AND RY.B=RX.B AND RY.C=S.C));
3.6 试叙述SQL语言的关系代数特点和元组演算特点。

答:SQL的关系代数特点如下:
① 有关系代数运算的并、交、差、自然联接等运算符;
② FROM子句体现了笛卡尔积操作,WHERE子句体现了选择操作,SELECT子
句体现了投影操作。

SQL的元组演算特点如下:
① FROM子句中的基本表名应视为“元组变量”,属性名应视为“元组分量”;②
有存在量词EXISTS符号。

3.12 试用SQL更新语句表达对3.2题教学数据库中关系S、SC、C的更新操作:
① 往关系C中插一个课程元组('C8','VC++','T6')。

② 检索所授每门课程平均成绩均大于80分的教师姓名,并把检索到的值送往另一个
已存在的表FACULTY(TNAME)。

③ 在SC中删除尚无成绩的选课元组。

④ 把选修LIU老师课程的女同学选课元组全部删去。

⑤ 把MATHS课不及格的成绩
全改为60分。

⑥ 把低于所有课程总平均成绩的女同学成绩提高5%。

⑦ 在表SC中修改C4课程的成绩,若成绩小于等于70分时提高5%,若成绩大于70 分时提高4%(用两种方法实现,一种方法是用两个UPDATE语句实现,另一种方法是用带CASE操作的一个UPDATE语句实现)。

⑧ 在表SC中,当某个成绩低于全部课程的平均成绩时,提高5%。

解:① INSERT INTO C
VALUES('C8','VC++','T6');
② INSERT INTO FACULTY(TNAME)
SELECT DISTINCT TEACHER
FROM (SELECT TEACHER, C.C#, AVG(GRADE)
FROM S, SC
WHERE SC.C#=C.C#
GROUP BY TEACHER, C.C#)
AS RESULT(TEACHER, C#, AVG_GRADE) AS X WHERE 80<=ALL(SELECT AVG_GRADE FROM RESULT AS Y
WHERE Y.TEACHER=X.TEACHER);
③ DELETE FROM SC
WHERE GRADE IS NULL;④ DELETE FROM SC
WHERE S# IN(SELECT S# FROM S WHERE SEX='F')
AND C# IN(SELECT C# FROM C WHERE TEACHER='LIU');
⑤ UPDATE SC
SET GRADE=60 WHERE GRADE<60
AND C# IN(SELECT C# FROM C WHERE CNAME='MATHS');
⑥ UPDATE SC
SET GRADE=GRADE*1.05
WHERE S# IN(SELECT S# FROM S WHERE SEX='F') AND GRADE
⑦ 用两个UPDATE语句实现:
UPDATE SC
SET GRADE=GRADE*1.04
WHERE C#='C4' AND GRADE>70; UPDATE SC
SET GRADE=GRADE*1.05
WHERE C#='C4' AND GRADE<=70;
(这两个UPDATE语句的顺序不能颠倒。

)用一个UPDATE语句实现:
UPDATE SC
SET GRADE=GRADE*CASE
WHEN GRADE>70 THEN 1.04 ELSE 1.05 END
WHERE C#='C4';⑧ UPDATE SC
SET GRADE=GRADE*1.05
WHERE GRADE
3.13 设数据库中有三个关系:
职工表 EMP(E#,ENAME,AGE,SEX,ECITY),
其属性分别表示职工工号、姓名、年龄、性别和籍贯。

工作表 WORKS(E#,C#,SALARY),
其属性分别表示职工工号、工作的公司编号和工资。

公司表 COMP(C#,CNAME,CITY),
其属性分别表示公司编号、公司名称和公司所在城市。

试用SQL语句写出下列操作:
① 用CREATE TABLE语句创建上述三个表,需指出主键和外键。

② 检索超过50岁
的男职工的工号和姓名。

③ 假设每个职工只能在一个公司工作,检索工资超过1000元的男性职工工号和姓名。

④ 假设每个职工可在多个公司工作,检索在编号为C4和C8公司兼职的职工工号和
姓名。

⑤ 检索在“联华公司”工作、工资超过1000元的男性职工的工号和姓名。

感谢您的阅读,祝您生活愉快。

相关文档
最新文档