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

合集下载

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

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

(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章习题参考答案

第3章习题解答1.选择题(1)表设计器的“允许空”单元格用于设置该字段是否可输入空值,实际上就是创建该字段的(D)约束。

A.主键B.外键C.NULL D.CHECK(2)下列关于表的叙述正确的是(C)。

A.只要用户表没有人使用,则可将其删除B.用户表可以隐藏C.系统表可以隐藏D.系统表可以删除(3)下列关于主关键字叙述正确的是( A )。

A.一个表可以没有主关键字B.只能将一个字段定义为主关键字C.如果一个表只有一个记录,则主关键字字段可以为空值D.都正确(4)下列关于关联叙述正确的是( C )。

A.可在两个表的不同数据类型的字段间创建关联B.可在两个表的不同数据类型的同名字段间创建关联C.可在两个表的相同数据类型的不同名称的字段间创建关联D.在创建关联时选择了级联更新相关的字段,则外键表中的字段值变化时,可自动修改主键表中的关联字段(5)CREATE TABLE语句(C )。

A.必须在数据表名称中指定表所属的数据库B.必须指明数据表的所有者C.指定的所有者和表名称组合起来在数据库中必须唯一D.省略数据表名称时,则自动创建一个本地临时表(6)删除表的语句是(A)。

A.Drop B.Alter C.Update D.Delete (7)数据完整性不包括(B )。

A.实体完整性B.列完整性C.域完整性D.用户自定义完整(8)下面关于Insert语句的说法正确的是(A )。

A.Insert一次只能插入一行的元组B.Insert只能插入不能修改C.Insert可以指定要插入到哪行D.Insert可以加Where条件(9)表数据的删除语句是( A )。

A.Delete B.Inser C.Update D.Alter (10)SQL数据定义语言中,表示外键约束的关键字是(B )。

A.Check B.Foreign Key C.Primary Key D.Unique2.填空题(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-3章习题参考答案

数据库概论第1-3章习题参考答案

第1章绪论习题参考答案1、试述数据、数据库、数据库管理系统、数据库系统的概念。

(参见P3、4、5页)参考答案:描述事物的符号记录称为数据;数据库是长期储存在计算机内的、有组织的、可共享的数据集合;数据库管理系统是位于用户与操作系统之间的一层数据管理软件; 数据库系统是指在计算机系统中引入数据库后的系统,一般由数据库、数据库管理系统(及其开发工具)、应用系统、数据库管理员和用户构成。

2.使用数据库系统有什么好处?(参见P12页)参考答案:数据库系统使信息系统从以加工数据的程序为中心转向围绕共享的数据库为中心的阶段,这样既便于数据的集中管理,又有利于应用程序的研制和维护,提高了数据的利用率和相容性,提高了决策的可靠性。

3.试述文件系统与数据库系统的区别和联系。

(8、9、10页)参考答案:1)数据结构化是数据库与文件系统的根本区别。

在文件系统中,相互独立的文件的记录内部是有结构的,管其记录内部已有了某些结构,但记录之间没有联系。

数据库系统实现整体数据的结构化,是数据库的主要特征之一。

2)在文件系统中,数据的最小存取单位是记录,粒度不能细到数据项。

而在数据库系统中,存取数据的方式也很灵活,可以存取数据库中的某一个数据项、一组数据项一个记录或或一组记录。

3)文件系统中的文件是为某一特定应用服务的,文件的逻辑结构对该应用程序来说是优化的,因此要想对现有的数据再增加一些新的应用会很困难,系统不容易扩充。

而在数据库系统中数据不再针对某一应用,而是面向全组织,具有整体的结构化。

5.试述数据库系统的特点。

(9、10、11页)参考答案:数据结构化;数据的共享性高、冗余度低、易扩充;数据独立性高;数据由DBMS统一管理和控制。

6.数据库管理系统的主要功能有哪些? (4页)参考答案:数据定义功能、数据操纵功能、数据库的运行管理、数据库的建立和维护功能。

7.试述数据模型的概念(13页)、数据模型的作用、数据模型的三个要素。

(完整版)数据库练习题及答案解析

(完整版)数据库练习题及答案解析

第一章习题一、单项选择题1 •数据库(DB),数据库系统(DBS)和数据库管理系统(DBMS )之间的关系是(A )。

A. DBS 包括DB 和DBMSB. DBMS 包括DB 和DBSC. DB包括DBS和DBMSD. DBS就是DB,也就是DBMS2. 下面列出的数据库管理技术发展的三个阶段中,没有专门的软件对数据进行管理的是(D )。

I •人工管理阶段II.文件系统阶段III •数据库阶段A. I 和IIB. 只有IIC. II 和IIID. 只有I3. 下列四项中,不属于数据库系统特点的是(C )。

A. 数据共享B. 数据完整性C. 数据冗余度高D. 数据独立性高4. 数据库系统的数据独立性体现在(B )。

A .不会因为数据的变化而影响到应用程序B. 不会因为系统数据存储结构与数据逻辑结构的变化而影响应用程序C. 不会因为存储策略的变化而影响存储结构D. 不会因为某些存储结构的变化而影响其他的存储结构5. 要保证数据库的数据独立性,需要修改的是(C )。

A. 模式与外模式B. 模式与内模式C. 三层之间的两种映射D. 三层模式6. 要保证数据库的逻辑数据独立性,需要修改的是(A )。

A. 模式与外模式的映射B. 模式与内模式之间的映射C. 模式D. 三层模式7. 用户或应用程序看到的那部分局部逻辑结构和特征的描述是(C ),它是模式的逻辑子集。

A.模式B.物理模式C. 子模式D. 内模式8. 下述(B )不是DBA 数据库管理员的职责。

A.完整性约束说明B.定义数据库模式C.数据库安全D.数据库管理系统设计9. 常见的数据模型有三种,它们是(B )A 网状、关系和语义B 层次、关系和网状C 环状、层次和关系D 字段名、字段类型和记录10. 在E-R 图中,用来表示属性的图形是(B )A 矩形B 椭圆形C 菱形D 平行四边形二、填空题1. 描述数据库全体数据的全局逻辑结构和特性的是___________ 模式 ______ 。

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

对于教学数据库的三个基本表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 SCgroup by s#.having count(c#)>=2;3.2.5检索至少有学号为S2和S4所学的课程和课程名select c#,cnamefrom 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 cexcept(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 # not in (select c# from sc where #=# ));3.2.8检索选修课程包含'LIU'老师所授课程的全部课程的学生的学号和姓名select s#,snamefrom swhere not exists((select c#from cwhere t#=(select t#from twhere tname='LIU')) except(select c# from sc where #=#) );设有两个基本表R(A,B,C)和S(A,B,C),试用SQL查询语句表达下列关系代数表达式:① R∪S ② R∩S ③ R-S ④ R×S ⑤π(R) A,Bπ(S)B,C⑥π(σ(R×S)⑦π(R ⑧ R÷π(S) S) 1,63=43C1,2, 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 , ,FROM R, SWHERE =;⑥ SELECT ,FROM R, SWHERE =;⑦ SELECT R.* (R.*表示R中全部属性)FROM R, SWHERE =;⑧ R÷π(S)的元组表达式如下:C{ 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 = AND = AND =);试叙述SQL语言的关系代数特点和元组演算特点。

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

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

试用SQL更新语句表达对题教学数据库中关系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 CVALUES('C8','VC++','T6');② INSERT INTO FACULTY(TNAME)SELECT DISTINCT TEACHERFROM (SELECT TEACHER, #, AVG(GRADE)FROM S, SCWHERE #=#GROUP BY TEACHER, #)AS RESULT(TEACHER, C#, AVG_GRADE) AS XWHERE 80<=ALL(SELECT AVG_GRADEFROM RESULT AS YWHERE =;③ DELETE FROM SCWHERE GRADE IS NULL;④ DELETE FROM SCWHERE S# IN(SELECT S# FROM S WHERE SEX='F') AND C# IN(SELECT C# FROM C WHERE TEACHER='LIU'); UPDATE SC⑤.SET GRADE=60WHERE GRADE<60AND C# IN(SELECT C# FROM C WHERE CNAME='MATHS');⑥ UPDATE SCSET GRADE=GRADE*WHERE S# IN(SELECT S# FROM S WHERE SEX='F') AND GRADE<(SELECT AVG(GRADE) FROM SC);⑦用两个UPDATE语句实现:UPDATE SCSET GRADE=GRADE*WHERE C#='C4' AND GRADE>70;UPDATE SCSET GRADE=GRADE*WHERE C#='C4' AND GRADE<=70;(这两个UPDATE语句的顺序不能颠倒。

)用一个UPDATE语句实现:UPDATE SCSET GRADE=GRADE*CASEWHEN GRADE>70 THENELSEENDWHERE C#='C4';⑧ UPDATE SCSET GRADE=GRADE*WHERE GRADE<(SELECT AVG(GRADE)FROM SC);设数据库中有三个关系:职工表 EMP(E#,ENAME,AGE,SEX,ECITY),其属性分别表示职工工号、姓名、年龄、性别和籍贯。

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

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

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

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

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

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

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

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

⑦工号为E6的职工在多个公司工作,试检索至少在E6职工兼职的所有公司工作的职工工号。

⑧检索联华公司中低于本公司平均工资的职工工号和姓名。

⑨在每一公司中为50岁以上职工加薪100元(若职工为多个公司工作,可重复加)。

⑩在EMP表和WORKS表中删除年龄大于60岁的职工有关元组。

解:① CREATE TABLE EMP( E# CHAR(4) NOT NULL,ENAME CHAR(8) NOT NULL,AGE SMALLINT,SEX CHAR(1),ECITY CHAR(20),PRIMARY KEY(E#));CREATE TABLE COMP( C# CHAR(4) NOT NULL,CNAME CHAR(20) NOT NULL,CITY CHAR(20),PRIMARY KEY(C#));CREATE TABLE WORKS( E# CHAR(4) NOT NULL,C# CHAR(4) NOT NULL,SALARY SMALLINT,PRIMARY KEY(E#, C#),FOREIGN KEY(E#) REFERENCES EMP(E#),FOREIGN KEY(C#) REFERENCES COMP(C#));② SELECT E#, ENAMEFROM EMPWHERE AGE>50 AND SEX='M';③ SELECT #, ENAMEFROM EMP, WORKSWHERE #=# AND SALARY>1000;④ SELECT #,FROM EMP A, WORKS B, WORKS CWHERE #=# AND #=#AND #='C4' AND #='C8';⑤ SELECT #,FROM EMP A, WORKS B, COMP CWHERE #=# AND #=#AND CNAME='联华公司' AND SALARY>1000AND SEX='M';⑥ SELECT E#, COUNT(C#) AS NUM, SUM(SALARY) AS SUM_SALARY FROM WORKS;GROUP BY E#.⑦ SELECT #FROM WORKS XWHERE NOT EXISTS(SELECT *FROM WORKS YWHERE E#='E6'AND NOT EXISTS(SELECT *FROM WORKS ZWHERE #=#AND #=#));⑧ SELECT #,FROM EMP A, WORKS B, COMP CWHERE #=# AND #=#AND CNAME='联华公司'AND SALARY<(SELECT AVG(SALARY)FROM WORKS, COMPWHERE #=#AND CNAME='联华公司');⑨ UPDATE WORKSSET SALARY=SALARY+100WHERE E# IN (SELECT E# FROM EMP WHERE AGE>50);⑩ DELETE FROM WORKSWHERE E# IN (SELECT E# FROM EMP WHERE AGE>60);DELETE FROM EMP;WHERE AGE>60。

相关文档
最新文档