数据库第三章作业
国开作业数据库应用技术-第三章 本章自测35参考(含答案)

题目:下列关于SQL Server中扩大数据库空间的说法,正确的是()。
选项A:只能扩大日志文件的空间,不能扩大数据文件的空间
选项B:在数据库空间未用满时不能进行扩大数据库空间的操作
选项C:日志文件和数据文件的空间都可以扩大
选项D:只能扩大数据文件的空间,不能扩大日志文件的空间
答案:日志文件和数据文件的空间都可以扩大
题目:下列关于SQL Server数据库组成的说法,正确的是()。
选项A:一个数据库可由仅一个数据文件和仅一个日志文件组成
选项B:一个数据库可由仅一个数据文件和多个日志文件组成
选项C:一个数据库可由多个数据文件和多个日志文件组成
选项D:一个数据库可由多个数据文件和仅一个日志文件组成
答案:一个数据库可由多个数据文件和多个日志文件组成
题目:在一台计算机上只能安装一个SQL Server默认实例。
选项A:对
选项B:错
答案:对
题目:SQL Server 2008最核心的服务是SSMS。
选项A:对
选项B:错
答案:错
题目:一个数据库必须包含次要数据文件,可以包含一个或多个次要数据文件。
选项A:对
选项B:错
答案:错
题目:主要数据文件的推荐扩展名是mdf。
选项A:对
选项B:错
答案:对
题目:删除数据库,只能删除数据文件,并不删除日志文件。
选项A:对
选项B:错
答案:错。
《数据库》第三章参考答案

(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)(完)。
数据库第三章习题参考

5.求至少用了供应商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));
习题三 第5题
1. 找出所有供应商的姓名及其所在城市。 Select sname, city from s; 2. 找出所有零件的名称、颜色、重量。 Select pname, color, weight from p; 3.找出使用供应商S1所供应零件的工程项目代码。 Select jno from spj where sno=‘S1’;
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=‘天津’) );
3.求供应工程J1零件为红色的供应商号码。 Select sno from spj, p Where spj.pno=p.pno and jno=‘J1’ and color=‘红’; 或: Select sno from spj Where jno =‘J1’ and pno in (Select pno from p Biblioteka where color=‘红’ );
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=‘上海’);
数据库第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数据库基本操作习题与答案

第三章数据库基本操作一、选择题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,其功能是________。
数据库系统原理第三章同步练习

性。
8. 消除了非主属性对候选键局部依赖的关系模式, 9. 两个函数依赖集F和G等价的充分必要条件是
10. 消除了每一属性对候选键传递依赖的关系模
式称为 BCNF 模式
11. 一个关系模式属于 4NF ,它必定属于BCNF。
A. 互不相关的
B. 不可分解的
C. 长度可变的
D. 互相关联的
6. 假设关系模式R(A,B)属于3NF,下列说法( B )
是正确的
A. 它一定消除了插入和删除异常
B. 仍存在一定的插入和删除异常
C. 一定属于BCNF
D. A和C
7. 设有关系W(工号, 姓名, 工种, 定额), 将其规范
化到第三范式正确的答案是( C )
1NF变成了3NF
A. 局部函数依赖和传递函数依赖
B. 完全函数依赖和传递函数依赖
C. 完全函数依赖
D. 局部函数依赖
13. 下述说法正确的是( D )
A. 属于BCNF的关系模式不存在存储异常
B. 函数依赖可由属性值决定,不由语义决定
C. 超键就是候选键
D. 键是唯一能决定一个元组的属性或属性组
一、单项选择题
1. 当B属性函数依赖于A属性时,属性A与B的联
系是(B )
A. 一对多
C. 多对多
B. 多对一
C. 以上都不是
2. 关系模式R中的属性全部是主属性,则R的最高
范式必定是(B )
A. 2NF
B. 3NF
C. BCNF
D. 4NF
3. 在关系模式R(A,B,C,D)中,有函数依赖集F={
Z=U-X-Y,则 X →→Z
5. 若关系模式R已属于第一范式,且其中的每一
数据库第3章习题

(一)选择题1.关系数据库管理系统应能实现的专门关系运算包括____。
A.排序、索引、统计B.选择、投影、连接C.关联、更新、排序D.显示、打印、制表2.在一个关系中如果有这样一个属性或属性组存在,它的值能唯一地标识关系中的每一个元组,称这个属性或属性组为____。
A.关键字B.数据项C.主属性D.主属性值3.同一个关系模型的任两个元组值____。
A.不能全同B.可全同C.必须全同D.以上都不是4.一个关系数据库文件中的各条记录____。
A.前后顺序不能任意颠倒,一定要按照输入的顺序排列B.前后顺序可以任意颠倒,不影响库中的数据关系C.前后顺序可以任意颠倒,但排列顺序不同,统计处理的结果就可能不同D.前后顺序不能任意颠倒,一定要按照关键字段值的顺序排列5.在关系代数的传统集合运算中,假定有关系R和S,运算结果为W。
如果W中的元组属于R,或者属于S,则W为____运算的结果。
如果W中的元组属于R而不属于S,则为____运算的结果。
如果W中的元组既属于R又属于S,则W为____运算的结果。
A.笛卡尔积B.并C.差D.交6.在关系代数的专门关系运算中,从表中取出满足条件的属性的操作称为____;从表中选取满足某种条件的元组的操作称为____;将两个关系中具有共同属性值的元组连接到一起构成新表的操作称为____。
A.选择B.投影C.连接D.扫描7.自然连接是构成新关系的有效方法。
一般情况下,当对关系R和S使用自然连接时,要求R和S含有一个或多个共有的____。
A.元组B.行C.记录D.属性8.等值连接与自然连接是____。
A.相同的B.不同的9.如图所示的关系R,经操作ΠA,B(σB=b(R))(Π为“投影”运算符,σ“选择”运算符)的运算结果是____。
D10.设有属性A,B,C,D,以下表示中不是关系的是____。
A.R(A)B.R(A,B,C,D)C.R(A×B×C×D)D.R(A,B)11.关系运算中花费时间可能最长的运算是____。
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
第3章关系数据库系统RDBS一. 简答题1.对于表中几个特殊的列,如主键、候选键和外键,分别用什么限制来保证它们的完整性?对表中其它一般性的列,用什么限制来保证它们的完整性?答:主键:其值必须是唯一,不为空值;候选键:其值必须唯一,可有也只能有一个空值;外键:主键值在修改和删除时,从表中与该主键值相同的外键值可级联(CASCADE)修改和删除,或改为空值、默认值、禁止主表主键值的修改和删除;一般的列:检查约束和断言。
2.SQL SERVER中规则的目的?答:规则的目的针对表中的某一列,指明该列的取值范围。
3.SQL SERVER中在定义某些限制时,分列级与表级,其分类的原则是什么?答:列级检查约束针对表中一列;表级检查约束则针对同一表中多列4.外键限制定义的条件?答:外键限制的列必须是另一个表中的主键。
5.关系代数的基本操作符?笛卡尔乘积最大的作用是什么?答:基本操作符:投影,选择,交,积,差;作用:把任意两个不相关的表相连。
6.为什么说在实际查询中自然连接是用得比较多的?答:可以从两个关系实例的笛卡尔乘积中选出同时满足一个或多个条件等式的行,每个条件等式中的列名相同。
7.关系代数中对结果有重复元组时,如何处理?答:将去掉重复元组。
8.连接的分类?答:条件连接:加入连接条件,对两个关系实施连接;等连接:是条件连接的特例。
要求连接条件由等式组成;自然连接:是等连接的特例。
要求等式中涉及的字段名必须相等;外连接:是涉及有空值的自然连接。
9.外连接又分3种,其依据是什么?答:左外联结,右外联结,全外联结。
二. 单项选择题1. (③)不是关系代数的基本操作。
①Selection ②Projection ③Join ④Intersection2. (③)用唯一限制来约束。
①主键②外键③候选键④简单键3. (②)与“列”不同义。
①字段②元组③成员④属性三. 判断题(正确打√,错误打×)1. (√)关系代数中的改名操作既可用于改名也可用于存放临时关系模式结果。
2. (×)对主表,插入操作可能会违背参照完整性限制,但删除和更新不会。
3. (×)等连接是自然连接的特例.4. (√)关系代数是与关系模型有关的查询语言。
5. (√)外连接可能涉及有空值。
六. 设有如下图所示三个关系实例X、Y和Z,请分别求出下列各表达式的值。
(1)σA = a1(Y×Z)(2)Y Z(3)X Y ZX A B Y B C Z A Ca1 b1 b1 c2 a1 c1a1 b2 b2 c1 a1 c2a2 b1 b1 c1 a2 c3a3 b1 b1 c3 a3 c4答:(1){<b1,c2,a1,c1>,<b1,c2,a1,c2>,<b2,c1,a1,c1>,<b2,c1,a1,c2>,<b1,c1,a1,c1>,<b 1,c1,a1,c2>,<b1,c3,a1,c1>,<b1,c3,a1,c2>}(2)C A Bc1 a1 b2c1 a1 b1c2 a1 b1c3 a2 b1c4 a3 null(3)A B Ca1 b1 c1a1 b1 c2a1 b2 c1a2 b1 c31.一个电影资料库有四个实体“电影”,“演员”,“导演”,“电影公司”。
“电影”的属性有电影编号,电影名,电影类型,对白语言;“演员”的属性有演员工作证号,姓名,出生年,性别;“导演”的属性有导演工作证号,姓名,出生年,性别;“电影公司”的属性有公司名称,所在国家。
这些实体间的联系及它们的属性有:演员出演电影,为多对多联系,该联系含角色属性;导演执导电影,每部电影只由一个导演执导;演员和导演属于电影公司;电影公司出品电影,有出品年份属性。
1)请画出ER图,要求标出实体的主键、联系的约束类型和键约束。
2)将此ER图转换为关系模型,要求标出各关系的主键,如果存在的话还应指明其候选键和外键。
3)请用关系代数表达式和SQL分别表达下列查询①查询1957年之前出生的男演员的姓名。
②查询2000年环球公司出品的电影的名字和导演姓名。
③查询张一导演所导演的影片中的主角演员姓名。
答:(1)电影类型(2)演员:演员工作证号为主健;导演:导演工作证号为主健;电影公司:电影公司为主健;电影:电影编号为主键,导演工作证号为外键,电影名为候选键;出演(演员工作证号,电影编号,角色)(演员工作证号,电影编号)为主健,演员工作证号和电影编号各为外键;出品:为主健,电影编号和公司名称各为外键;属于a:为主健,公司名称和演员工作证号各为外键;属于b:为主健,公司名称,导演工作证号各为外键(3)①σ出生年< 1957(演员)∩σ性别=男(演员)SELECT *FROM 演员WHERE出生年<1957INTERSECTSELECT*FROM WHERE性别=男②ρ(Temp,σ出品年份=2000(出品)∩σ公司名称=环球公司(出品))π电影名,姓名(Temp 电影导演)SELECT 电影名FROM 出品,电影WHERE出品.出品年份=‘2000’AND出品.公司名称=环球公司AND出品.电影编号=电影.电影编号INTERSECTSELECT姓名FROM 出品,电影,导演WHERE出品.出品年份=‘2000’AND出品.公司名称=环球公司AND出品.电影编号=电影.电影编号AND导演.导演工作证号=电影.导演工作证号③π姓名(σ姓名=张一(导演)电影出演演员)SELECCT 姓名FROM 导演,电影,出演,演员WHERE 导演.姓名=‘张一’AND电影.导演工作证号=导演.导演工作证号AND出演.电影编号=演员.工作证号2.某出版社管理系统有四个实体,即出版社(Publisher)、编辑(Editor)、作者(Author)和书籍(Book)。
“出版社”的属性有出版社编码(Pid)、出版社名称(Pname)、地址(Paddr)和电话(Ptel);“编辑”的属性有编辑工号(Eid)、姓名(Ename)、性别(Egender)、出生日期;“作者”的属性有作者编码(Aid)、姓名(Aname)、性别(Agender)、电话(Atel);“书籍”的属性有国际图书分类号(Isbn)、书名(Bname)、单价(Bprice)。
这些实体间的联系及它们的属性有:作者“主编”(ZX)书籍,为1:n联系;编辑“校对”(JD)书籍,为1:n联系;出版社“出版”(CB)书籍,为1:n联系;“出版”的属性有出版日期(Pdate)。
(1)请画出概念数据模型的E-R图,要求标注联系的约束类型和键约束。
(2)将此E-R图表示的数据模型转换为关系模型,要求标出各关系的主键。
(3)给出创建“出版”关系(表)的SQL语句(需要创建相应的主键约束和外键约束)。
(4)创建一个由地址中含有“成都市”的出版社出版的书籍的视图。
(5)请分别用关系代数表达式和SQL查询语句表达下列查询:①由出版社“XNJDP”出版的、由编辑名为“MTQ”校对的书籍的ISBN号和书名。
②由“男”性作者主编的、且由出版社“XNJDP”在2008.1.1至2008.12.31之间出版的书籍的ISBN号和书名。
③由“女”性编辑校对的、且单价在20至40元之间的书籍的ISBN号和书名。
答:(1)2)出版社Publisher (出版社编码Pid,出版社名称Pname,地址Paddr,电话Ptel, 国际图书分类号Isbn, 出版日期Pdate)编辑Editor(编辑工号Eid,姓名Ename,性别Egender,,出生日期, 国际图书分类号Isbn)作者Author(作者编码Aid,姓名Aname,性别Agender,电话Atel, 国际图书分类号Isbn)书籍Book(国际图书分类号Isbn,书名Bname,单价Bprice)。
3)CREATE TABLE Publisher( Pid char NOT NULL,Pname char NOT NULL,Paddr char NOT NULL,Ptel char NOT NULL,Isbn char NOT NULL,Pdete datetime NOT NULL,CONSTREINT Pid_Key PRIMARY KEY (Pid),CONSTRAINT Isbn_constREFERENCES Book( Isbn)ON DELETE CASCADEON UPDATE CASCADE)4) CREATE VIEW Book(Isbn, Bname,Bprice)ASSELECT Isbn, Bname,BpriceFROM Publisher,BookWHERE Publisher .Paddr=‘成都市’AND Publisher.Isbn= Book .Isbn5)①πIsbn, Bname(σPname=XNJDP(Publisher)σEname= MTQ(Editor)Book)SELECT Isbn, BnameFROM Publisher,Book, EditorWHERE Publisher.Pname=’XNJDP’AND Editor.Ename=’MTQ’ANG Publisher.Isbn= Book .Isbn AND Editor.Isbn= Book .Isbn②πIsbn, Bname(σPname=XNJDP(Publisher)∩σPdate<2008.12.31(Publisher)∩σPdate>2008.1.1(Publisher))σAgender=‘男‘(Author)Book)SELECT Isbn, BnameFROM Publisher,Book, AuthorWHERE Publisher.Pname=’XNJDP’AND Publisher. Pdate<2008.12.31 AND Publisher.Pdate>2008.1.1 AND Author. Agender=‘男’ANG Publisher.Isbn= Book .Isbn AND Author.Isbn= Book .Isbn③πIsbn, Bname(σEgender=‘女‘(Editor)(σBprice>20(Book)∩σBprice<40(Book))SELECT Isbn, BnameFROM Book, EditorWHERE Book.Bprice>20 AND Book.Bprice<40 AND Editor. Egender=‘女’ANG Editor.Isbn= Book .Isbn如有侵权请联系告知删除,感谢你们的配合!。