数据库第二章课后知识题解答
(完整版)数据库系统基础教程第二章答案解析

For relation Accounts, the attributes are:acctNo, type, balanceFor relation Customers, the attributes are:firstName, lastName, idNo, accountExercise 2.2.1bFor relation Accounts, the tuples are:(12345, savings, 12000),(23456, checking, 1000),(34567, savings, 25)For relation Customers, the tuples are:(Robbie, Banks, 901-222, 12345),(Lena, Hand, 805-333, 12345),(Lena, Hand, 805-333, 23456)Exercise 2.2.1cFor relation Accounts and the first tuple, the components are:123456 → acctNosavings → type12000 → balanceFor relation Customers and the first tuple, the components are:Robbie → firstNameBanks → lastName901-222 → idNo12345 → accountExercise 2.2.1dFor relation Accounts, a relation schema is:Accounts(acctNo, type, balance)For relation Customers, a relation schema is:Customers(firstName, lastName, idNo, account) Exercise 2.2.1eAn example database schema is:Accounts (acctNo,type,balance)Customers (firstName,lastName,idNo,account)A suitable domain for each attribute:acctNo → Integertype → Stringbalance → IntegerfirstName → StringlastName → StringidNo → String (because there is a hyphen we cannot use Integer)account → IntegerExercise 2.2.1gAnother equivalent way to present the Account relation:Another equivalent way to present the Customers relation:Exercise 2.2.2Examples of attributes that are created for primarily serving as keys in a relation:Universal Product Code (UPC) used widely in United States and Canada to track products in stores.Serial Numbers on a wide variety of products to allow the manufacturer to individually track each product.Vehicle Identification Numbers (VIN), a unique serial number used by the automotive industry to identify vehicles.Exercise 2.2.3aWe can order the three tuples in any of 3! = 6 ways. Also, the columns can be ordered in any of 3! = 6 ways. Thus, the number of presentations is 6*6 = 36.Exercise 2.2.3bWe can order the three tuples in any of 5! = 120 ways. Also, the columns can be ordered in any of 4! = 24 ways. Thus, the number of presentations is 120*24 = 2880Exercise 2.2.3cWe can order the three tuples in any of m! ways. Also, the columns can be ordered in any of n! ways. Thus, the number of presentations is n!m!Exercise 2.3.1aCREATE TABLE Product (maker CHAR(30),model CHAR(10) PRIMARY KEY,type CHAR(15));CREATE TABLE PC (model CHAR(30),speed DECIMAL(4,2),ram INTEGER,hd INTEGER,price DECIMAL(7,2));Exercise 2.3.1cCREATE TABLE Laptop (model CHAR(30),speed DECIMAL(4,2),ram INTEGER,hd INTEGER,screen DECIMAL(3,1),price DECIMAL(7,2));Exercise 2.3.1dCREATE TABLE Printer (model CHAR(30),color BOOLEAN,type CHAR (10),price DECIMAL(7,2));Exercise 2.3.1eALTER TABLE Printer DROP color;Exercise 2.3.1fALTER TABLE Laptop ADD od CHAR (10) DEFAULT ‘none’; Exercise 2.3.2aCREATE TABLE Classes (class CHAR(20),type CHAR(5),country CHAR(20),numGuns INTEGER,bore DECIMAL(3,1),displacement INTEGER);Exercise 2.3.2bCREATE TABLE Ships (name CHAR(30),class CHAR(20),launched INTEGER);Exercise 2.3.2cCREATE TABLE Battles (name CHAR(30),date DATE);Exercise 2.3.2dCREATE TABLE Outcomes (ship CHAR(30),battle CHAR(30),result CHAR(10));Exercise 2.3.2eALTER TABLE Classes DROP bore;Exercise 2.3.2fALTER TABLE Ships ADD yard CHAR(30); Exercise 2.4.1aR1 := σspeed ≥ 3.00 (PC)R2 := πmodel(R1)model100510061013Exercise 2.4.1bR1 := σhd ≥ 100 (Laptop)R2 := Product (R1)R3 := πmaker (R2)makerEABFGExercise 2.4.1cR1 := σmaker=B (Product PC)R2 := σmaker=B (Product Laptop)R3 := σmaker=B (Product Printer)R4 := πmodel,price (R1)R5 := πmodel,price (R2)R6: = πmodel,price (R3)R7 := R4 R5 R6model price1004 6491005 6301006 10492007 1429Exercise 2.4.1dR1 := σcolor = true AND type = laser (Printer)R2 := πmodel (R1)model30033007Exercise 2.4.1eR1 := σtype=laptop (Product)R2 := σtype=PC(Product)R3 := πmaker(R1)R4 := πmaker(R2)R5 := R3 – R4Exercise 2.4.1fR1 := ρPC1(PC)R2 := ρPC2(PC)R3 := R1 (PC1.hd = PC2.hd AND PC1.model <> PC2.model) R2R4 := πhd(R3)Exercise 2.4.1gR1 := ρPC1(PC)R2 := ρPC2(PC)R3 := R1 (PC1.speed = PC2.speed AND PC1.ram = PC2.ram AND PC1.model < PC2.model) R2R4 := πPC1.model,PC2.model(R3)Exercise 2.4.1hR1 := πmodel(σspeed ≥ 2.80(PC)) πmodel(σspeed ≥ 2.80(Laptop))R2 := πmaker,model(R1 Product)R3 := ρR3(maker2,model2)(R2)R4 := R2 (maker = maker2 AND model <> model2) R3R5 := πmaker(R4)Exercise 2.4.1iR1 := πmodel,speed(PC)R2 := πmodel,speed(Laptop)R3 := R1 R2R4 := ρR4(model2,speed2)(R3)R5 := πmodel,speed (R3 (speed < speed2 ) R4)R6 := R3 – R5makerBExercise 2.4.1jR1 := πmaker,speed(Product PC)R2 := ρR2(maker2,speed2)(R1)R3 := ρR3(maker3,speed3)(R1)R4 := R1 (maker = maker2 AND speed <> speed2) R2R5 := R4 (maker3 = maker AND speed3 <> speed2 AND speed3 <> speed) R3R6 := πmaker(R5)makerFGhd25080160PC1.model PC2.model1004 1012makerBEmakerADEExercise 2.4.1kR1 := πmaker,model(Product PC)R2 := ρR2(maker2,model2)(R1)R3 := ρR3(maker3,model3)(R1)R4 := ρR4(maker4,model4)(R1)R5 := R1 (maker = maker2 AND model <> model2) R2R6 := R3 (maker3 = maker AND model3 <> model2 AND model3 <> model) R5R7 := R4 (maker4 = maker AND (model4=model OR model4=model2 OR model4=model3)) R6R8 := πmaker(R7)makerABDEExercise 2.4.2aπmodelσspeed≥3.00PCExercise 2.4.2bπmakerσhd ≥ 100 ProductLaptopExercise 2.4.2cσmaker=B πmodel,priceσmaker=B πmodel,price σmaker=Bπmodel,priceProduct PC Laptop Printer ProductProductExercise 2.4.2dPrinter σcolor = true AND type = laserπmodelExercise 2.4.2e σtype=laptop σtype=PC πmakerπmaker –Product ProductExercise 2.4.2fρPC1ρPC2 (PC1.hd = PC2.hd AND PC1.model <> PC2.model)πhdPC PCExercise 2.4.2gρPC1ρPC2PC PC(PC1.speed = PC2.speed AND PC1.ram = PC2.ram AND PC1.model < PC2.model)πPC1.model,PC2.modelExercise 2.4.2hPC Laptop σspeed ≥ 2.80σspeed ≥ 2.80πmodelπmodel πmaker,modelρR3(maker2,model2)(maker = maker2 AND model <> model2)makerExercise 2.4.2iPCLaptopProductπmodel,speed πmodel,speed ρR4(model2,speed2)πmodel,speed(speed < speed2 )–makerExercise 2.4.2jProduct PC πmaker,speed ρR3(maker3,speed3)ρR2(maker2,speed2)(maker = maker2 AND speed <> speed2)(maker3 = maker AND speed3 <> speed2 AND speed3 <> speed)πmakerExercise 2.4.2kπmaker(maker4 = maker AND (model4=model OR model4=model2 OR model4=model3)) (maker3 = maker AND model3 <> model2 AND model3 <> model)(maker = maker2 AND model <> model2)ρR2(maker2,model2)ρR3(maker3,model3)ρR4(maker4,model4)πmaker,modelProduct PCExercise 2.4.3aR1 := σbore ≥ 16 (Classes)R2 := πclass,country (R1)Exercise 2.4.3bR1 := σlaunched < 1921 (Ships)R2 := πname (R1)KirishimaKongoRamilliesRenownRepulseResolutionRevengeRoyal OakRoyal SovereignTennesseeExercise 2.4.3cR1 := σbattle=Denmark Strait AND result=sunk(Outcomes)R2 := πship (R1)shipBismarckHoodExercise 2.4.3dR1 := Classes ShipsR2 := σlaunched > 1921 AND displacement > 35000 (R1)R3 := πname (R2)nameIowaMissouriMusashiNew JerseyNorth CarolinaWashingtonWisconsinYamatoExercise 2.4.3eR1 := σbattle=Guadalcanal(Outcomes)R2 := Ships (ship=name) R1R3 := Classes R2R4 := πname,displacement,numGuns(R3)name displacement numGuns Kirishima 32000 8Washington 37000 9Exercise 2.4.3fR1 := πname(Ships)R2 := πship(Outcomes)R3 := ρR3(name)(R2)R4 := R1 R3nameCaliforniaHarunaHieiIowaKirishimaKongoMissouriMusashiNew JerseyExercise 2.4.3gFrom 2.3.2, assuming that every class has one ship named after the class.R1 := πclass (Classes) R2 := πclass (σname <> class (Ships)) R3 := R1 – R2Exercise 2.4.3hR1 := πcountry (σtype=bb (Classes)) R2 := πcountry (σtype=bc (Classes)) R3 := R1 ∩ R2Exercise 2.4.3iR1 := πship,result,date (Battles (battle=name) Outcomes)R2 := ρR2(ship2,result2,date2)(R1)R3 := R1 (ship=ship2 AND result=damaged AND date < date2) R2R4 := πship (R3)No results from sample data.Exercise 2.4.4aσbore ≥ 16πclass,countryClassesExercise 2.4.4bNorth Carolina Ramillies Renown Repulse Resolution Revenge Royal Oak Royal Sovereign Tennessee Washington Wisconsin Yamato Arizona Bismarck Duke of York Fuso Hood King George V Prince of Wales Rodney Scharnhorst South Dakota West Virginia Yamashiro class Bismarck country Japan Gt. Britainπnameσlaunched < 1921ShipsExercise 2.4.4cπshipσbattle=Denmark Strait AND result=sunkOutcomesExercise 2.4.4dπnameσlaunched > 1921 AND displacement > 35000Classes Ships Exercise 2.4.4eσbattle=Guadalcanal Outcomes Ships(ship=name)πname,displacement,numGunsExercise 2.4.4f Ships Outcomesπnameπship ρR3(name)Exercise 2.4.4g Classes Shipsπclass σname <> class πclass–Exercise 2.4.4hClasses Classesσtype=bb σtype=bcπcountry πcountry∩Exercise 2.4.4iBattles Outcomes (battle=name)πship,result,dateρR2(ship2,result2,date2)(ship=ship2 AND result=damaged AND date < date2)πshipExercise 2.4.5The result of the natural join has only one attribute from each pair of equated attributes. On the other hand, the result of the theta-join has both columns of the attributes and their values are identical.Exercise 2.4.6UnionIf we add a tuple to the arguments of the union operator, we will get all of the tuples of the original result and maybe the added tuple. If the added tuple is a duplicate tuple, then the set behavior will eliminate that tuple.Thus the union operator is monotone.IntersectionIf we add a tuple to the arguments of the intersection operator, we will get all of the tuples of the originalresult and maybe the added tuple. If the added tuple does not exist in the relation that it is added but does exist in the other relation, then the result set will include the added tuple. Thus the intersection operator is monotone.DifferenceIf we add a tuple to the arguments of the difference operator, we may not get all of the tuples of the originalresult. Suppose we have relations R and S and we are computing R – S. Suppose also that tuple t is in R but not in S. The result of R – S would include tuple t. However, if we add tuple t to S, then the new result will not have tuple t. Thus the difference operator is not monotone.ProjectionIf we add a tuple to the arguments of the projection operator, we will get all of the tuples of the original result and the projection of the added tuple. The projection operator only selects columns from the relation and does not affect the rows that are selected. Thus the projection operator is monotone.SelectionIf we add a tuple to the arguments of the selection operator, we will get all of the tuples of the original result and maybe the added tuple. If the added tuple satisfies the select condition, then it will be added to the newresult. The original tuples are included in the new result because they still satisfy the select condition. Thusthe selection operator is monotone.Cartesian ProductIf we add a tuple to the arguments of the Cartesian product operator, we will get all of the tuples of the original result and possibly additional tuples. The Cartesian product pairs the tuples of one relation with the tuples ofanother relation. Suppose that we are calculating R x S where R has m tuples and S has n tuples. If we add a tuple to R that is not already in R, then we expect the result of R x S to have (m + 1) * n tuples. Thus the Cartesianproduct operator is monotone.Natural JoinsIf we add a tuple to the arguments of a natural join operator, we will get all of the tuples of the original result and possibly additional tuples. The new tuple can only create additional successful joins, not less. If, however, the added tuple cannot successfully join with any of the existing tuples, then we will have zero additionalsuccessful joins. Thus the natural join operator is monotone.Theta JoinsIf we add a tuple to the arguments of a theta join operator, we will get all of the tuples of the original result and possibly additional tuples. The theta join can be modeled by a Cartesian product followed by a selection onsome condition. The new tuple can only create additional tuples in the result, not less. If, however, the addedtuple does not satisfy the select condition, then no additional tuples will be added to the result. Thus the theta join operator is monotone.RenamingIf we add a tuple to the arguments of a renaming operator, we will get all of the tuples of the original result and the added tuple. The renaming operator does not have any effect on whether a tuple is selected or not. In fact, the renaming operator will always return as many tuples as its argument. Thus the renaming operator is monotone.Exercise 2.4.7aIf all the tuples of R and S are different, then the union has n + m tuples, and this number is the maximum possible.The minimum number of tuples that can appear in the result occurs if every tuple of one relation also appears in the other. Then the union has max(m , n) tuples.Exercise 2.4.7bIf all the tuples in one relation can pair successfully with all the tuples in the other relation, then the natural join has n * m tuples. This number would be the maximum possible.The minimum number of tuples that can appear in the result occurs if none of the tuples of one relation can pairsuccessfully with all the tuples in the other relation. Then the natural join has zero tuples.Exercise 2.4.7cIf the condition C brings back all the tuples of R, then the cross product will contain n * m tuples. This number would be the maximum possible.The minimum number of tuples that can appear in the result occurs if the condition C brings back none of the tuples of R. Then the cross product has zero tuples.Exercise 2.4.7dAssuming that the list of attributes L makes the resulting relation πL(R) and relation S schema compatible, then the maximum possible tuples is n. This happens when all of the tuples of πL(R) are not in S.The minimum number of tuples that can appear in the result occurs when all of the tuples in πL(R) appear in S. Then the difference has max(n–m , 0) tuples.Exercise 2.4.8Defining r as the schema of R and s as the schema of S:1.πr(R S)2.R δ(πr∩s(S)) where δ is the duplicate-elimination operator in Section 5.2 pg. 2133.R – (R –πr(R S))Exercise 2.4.9Defining r as the schema of R1.R - πr(R S)Exercise 2.4.10πA1,A2…An(R S)Exercise 2.5.1aσspeed < 2.00 AND price > 500(PC) = øModel 1011 violates this constraint.Exercise 2.5.1bσscreen < 15.4 AND hd < 100 AND price ≥ 1000(Laptop) = øModel 2004 violates the constraint.Exercise 2.5.1cπmaker(σtype = laptop(Product)) ∩ πmaker(σtype = pc(Product)) = øManufacturers A,B,E violate the constraint.Exercise 2.5.1dThis complex expression is best seen as a sequence of steps in which we define temporary relations R1 through R4 that stand for nodes of expression trees. Here is the sequence:R1(maker, model, speed) := πmaker,model,speed(Product PC)R2(maker, speed) := πmaker,speed(Product Laptop)R3(model) := πmodel(R1 R1.maker = R2.maker AND R1.speed ≤ R2.speed R2)R4(model) := πmodel(PC)The constraint is R4 ⊆ R3Manufacturers B,C,D violate the constraint.Exercise 2.5.1eπmodel(σLaptop.ram > PC.ram AND Laptop.price ≤ PC.price(PC × Laptop)) = øModels 2002,2006,2008 violate the constraint.Exercise 2.5.2aπclass(σbore > 16(Classes)) = øThe Yamato class violates the constraint.Exercise 2.5.2bπclass(σnumGuns > 9 AND bore > 14(Classes)) = øNo violations to the constraint.Exercise 2.5.2cThis complex expression is best seen as a sequence of steps in which we define temporary relations R1 through R5 that stand for nodes of expression trees. Here is the sequence:R1(class,name) := πclass,name(Classes Ships)R2(class2,name2) := ρR2(class2,name2)(R1)R3(class3,name3) := ρR3(class3,name3)(R1)R4(class,name,class2,name2) := R1 (class = class2 AND name <> name2) R2R5(class,name,class2,name2,class3,name3) := R4 (class=class3 AND name <> name3 AND name2 <> name3) R3The constraint is R5 = øThe Kongo, Iowa and Revenge classes violate the constraint.Exercise 2.5.2dπcountry(σtype = bb(Classes)) ∩ πcountry(σtype = bc(Classes)) = øJapan and Gt. Britain violate the constraint.Exercise 2.5.2eThis complex expression is best seen as a sequence of steps in which we define temporary relations R1 through R5 that stand for nodes of expression trees. Here is the sequence:R1(ship,bat tle,result,class) := πship,battle,result,class(Outcomes (ship = name) Ships)R2(ship,battle,result,numGuns) := πship,battle,result,numGuns(R1 Classes)R3(ship,battle) := πship,battle(σnumGuns < 9 AND result = sunk (R2))R4(ship2,battle2) := ρR4(ship2,battle2)(πship,battle(σnumGuns > 9(R2)))R5(ship2) := πship2(R3 (battle = battle2) R4)The constraint is R5 = øNo violations to the constraint. Since there are some ships in the Outcomes table that are not in the Ships table, we are unable to determine the number of guns on that ship.Exercise 2.5.3Defining r as the schema A1,A2,…,A n and s as the schema B1,B2,…,B n:πr(R) πs(S) = øwhere is the antisemijoinExercise 2.5.4The form of a constraint as E1 = E2 can be expressed as the other two constraints.Using the “equating an expression to the empty set” method, we can simply say:E1– E2 = øAs a containment, we can simply say:E1⊆ E2 AND E2⊆ E1Thus, the form E1 = E2 of a constraint cannot express more than the two other forms discussed in this section.。
数据库第二章课后习题解答

第3部分习题及其解答第一章的两道题3-2 习题22.6 分别把习题1.10、习题1.11的ER图转换成关系模型数据结构。
【参考答案】1.习题1.10的ER图可转换成如下的关系模型数据结构。
①程序员(编号,姓名,性别,年龄,单位,职称),其中编号是关键字;②程序(程序名称,版权,专利号,价格),其中程序名称是关键字;③设计(编号,程序名称,开始时间,结束时间),其中(编号,程序名称)是关键字。
2.习题1.11的ER图可转换成如下的关系模型数据结构。
①工厂(工厂名称,厂址,联系电话),其中工厂名称是关键字;②产品(产品号,产品名,规格,单价),其中产品号是关键字;③工人(工人编号,姓名,性别,职称,工厂名称,雇用期,月薪),其中工人编号是关键字,工厂名称是外关键字,雇用期和月薪是联系属性;④生产(工厂名称,产品号,月产量),其中(工厂名称,产品号)是关键字,生产关系是表示联系的。
2.8 判断下列情况,分别指出它们具体遵循那一类完整性约束规则?1.用户写一条语句明确指定月份数据在1~12之间有效。
2.关系数据库中不允许主键值为空的元组存在。
3.从A关系的外键出发去找B关系中的记录,必须能找到。
【解答】1.用户用语句指定月份数据在1~12之间有效,遵循用户定义的完整性约束规则。
2.关系数据库中不允许主键值为空的元组存在,遵循实体完整性约束规则;3.从A关系的外键出发去找B关系的记录,必须能找到,遵循引用完整性约束规则。
2.9 判断下列情况,分别指出他们是用DML还是用DDL来完成下列操作?1.创建“学生”表结构。
2.对“学生”表中的学号属性,其数据类型由“整型”修改为“字符型”。
3.把“学生”表中学号“021”修改为“025”。
【解答】1.创建“学生”表结构,即定义一个关系模式,用DDL 完成。
2.修改“学生”表中学号属性的数据类型,即修改关系模式的定义,用DDL 完成。
3.修改“学生”表中学号属性的数据值,即对表中的数据进行操作,用DML 完成。
数据库技术与应用第二版第2章习题参考答案

第2章习题解答1.选择题(1)下列(C)不是sql 数据库文件的后缀。
A..mdf B..ldf C..tif D..ndf(2)SQL Server数据库对象中最基本的是(B)。
A.表和语句B.表和视图C.文件和文件组D.用户和视图(3)事务日志用于保存(C.)。
A. 程序运行过程B. 程序的执行结果C. 对数据的更新操作D. 数据操作(4)Master数据库是SQL Server系统最重要的数据库,如果该数据库被损坏,SQL Server将无法正常工作。
该数据库记录了SQL Server系统的所有(D)。
A. 系统设置信息B. 用户信息C. 对数据库操作的信息D. 系统信息(5)SQL Server中组成数据库的文件有(B)类型。
A. 2B. 3C. 4D. 5(6)分离数据库就是将数据库从(B)中删除,但是保持组成该数据的数据文件和事务日志文件中的数据完好无损。
A. WindowsB. SQL ServerC. U盘D. 企业管理器(7)以下是指对数据库的完整备份,包括所有的数据以及数据库对象。
A. 数据库完全备份B. 数据库差异份C. 事务日志备份D. 文件或文件组备份(8)下面描述错误的是()。
A.每个数据文件中有且只有一个主数据文件。
B.日志文件可以存在于任意文件组中。
C.主数据文件默认为PRIMARY文件组。
D.文件组是为了更好的实现数据库文件组织。
(9)下列文件中不属于SQL Server数据库文件的是()。
A.device_data.MDF B.device_log.LDFC.device_mdf.DAT D.device_data.NDF(10)SQL Server 对象的完整名称不包括下面()。
A.服务器名B.数据库名C.对象名D.文件夹名2.填空题(1)数据库逻辑结构中的文件主要存储(用户数据),而且存储所有与(用户数据)相关的信息。
(2)数据库物理结构主要应用于面向计算机的(数据)组织和管理。
数据库第二章课后习题解答

第3部分习题及其解答第一章的两道题3-2 习题22.6 分别把习题1.10、习题1.11的ER图转换成关系模型数据结构。
【参考答案】1.习题1.10的ER图可转换成如下的关系模型数据结构。
①程序员(编号,,性别,年龄,单位,职称),其中编号是关键字;②程序(程序名称,,专利号,价格),其中程序名称是关键字;③设计(编号,程序名称,开始时间,结束时间),其中(编号,程序名称)是关键字。
2.习题1.11的ER图可转换成如下的关系模型数据结构。
①工厂(工厂名称,厂址,联系),其中工厂名称是关键字;②产品(产品号,产品名,规格,单价),其中产品号是关键字;③工人(工人编号,,性别,职称,工厂名称,雇用期,月薪),其中工人编号是关键字,工厂名称是外关键字,雇用期和月薪是联系属性;④生产(工厂名称,产品号,月产量),其中(工厂名称,产品号)是关键字,生产关系是表示联系的。
2.8 判断下列情况,分别指出它们具体遵循那一类完整性约束规则?1.用户写一条语句明确指定月份数据在1~12之间有效。
2.关系数据库中不允许主键值为空的元组存在。
3.从A关系的外键出发去找B关系中的记录,必须能找到。
【解答】1.用户用语句指定月份数据在1~12之间有效,遵循用户定义的完整性约束规则。
2.关系数据库中不允许主键值为空的元组存在,遵循实体完整性约束规则;3.从A关系的外键出发去找B关系的记录,必须能找到,遵循引用完整性约束规则。
2.9 判断下列情况,分别指出他们是用DML还是用DDL来完成下列操作?1.创建“学生”表结构。
2.对“学生”表中的学号属性,其数据类型由“整型”修改为“字符型”。
3.把“学生”表中学号“021”修改为“025”。
【解答】1.创建“学生”表结构,即定义一个关系模式,用DDL完成。
2.修改“学生”表中学号属性的数据类型,即修改关系模式的定义,用DDL完成。
3.修改“学生”表中学号属性的数据值,即对表中的数据进行操作,用DML完成。
数据库系统基础教程第二章答案

数据库系统基础教程第二章答案(总15页)--本页仅作为文档封面,使用时请直接删除即可----内页可以根据需求调整合适字体及大小--Exercise relation Accounts, the attributes are:acctNo, type, balanceFor relation Customers, the attributes are:firstName, lastName, idNo, accountExercise relation Accounts, the tuples are:(12345, savings, 12000),(23456, checking, 1000),(34567, savings, 25)For relation Customers, the tuples are:(Robbie, Banks, 901-222, 12345),(Lena, Hand, 805-333, 12345),(Lena, Hand, 805-333, 23456)Exercise relation Accounts and the first tuple, the components are:123456 ? acctNosavings ? type12000 ? balanceFor relation Customers and the first tuple, the components are:Robbie ? firstNameBanks ? lastName901-222 ? idNo12345 ? accountExercise relation Accounts, a relation schema is:Accounts(acctNo, type, balance)For relation Customers, a relation schema is:Customers(firstName, lastName, idNo, account)Exercise example database schema is:Accounts (acctNo,type,balance)Customers (firstName,lastName,idNo,account)Exercise suitable domain for each attribute:acctNo ? Integertype ? Stringbalance ? IntegerfirstName ? StringlastName ? StringidNo ? String (because there is a hyphen we cannot use Integer)account ? IntegerExercise equivalent way to present the Account relation:Another equivalent way to present the Customers relation:Exercise of attributes that are created for primarily serving as keys in a relation:Universal Product Code (UPC) used widely in United States and Canada to track products in stores.Serial Numbers on a wide variety of products to allow the manufacturer to individually track each product.Vehicle Identification Numbers (VIN), a unique serial number used by the automotive industry to identify vehicles.Exercise can order the three tuples in any of 3! = 6 ways. Also, the columns can be ordered in any of 3! = 6 ways. Thus, the number of presentations is 6*6 = 36.Exercise can order the three tuples in any of 5! = 120 ways. Also, the columns can be ordered in any of 4! = 24 ways. Thus, the number of presentations is 120*24 = 2880Exercise can order the three tuples in any of m! ways. Also, the columns can be ordered in any of n! ways. Thus, the number of presentations is n!m!Exercise TABLE Product (maker CHAR(30),model CHAR(10) PRIMARY KEY,type CHAR(15));Exercise TABLE PC (model CHAR(30),speed DECIMAL(4,2),ram INTEGER,hd INTEGER,price DECIMAL(7,2));Exercise TABLE Laptop (model CHAR(30),speed DECIMAL(4,2),ram INTEGER,hd INTEGER,screen DECIMAL(3,1),price DECIMAL(7,2));Exercise TABLE Printer (model CHAR(30),color BOOLEAN,type CHAR (10),price DECIMAL(7,2));Exercise TABLE Printer DROP color;Exercise TABLE Laptop ADD od CHAR (10) DEFAULT ‘none’;Exercise TABLE Classes (class CHAR(20),type CHAR(5),country CHAR(20),numGuns INTEGER,bore DECIMAL(3,1),displacement INTEGER);Exercise TABLE Ships (name CHAR(30),class CHAR(20),launched INTEGER);Exercise TABLE Battles (name CHAR(30),date DATE);Exercise TABLE Outcomes (ship CHAR(30),battle CHAR(30),result CHAR(10));Exercise TABLE Classes DROP bore;Exercise TABLE Ships ADD yard CHAR(30);ExerciseR1 := σspeed ≥ (PC)R2 := πmodel (R1)Exercise R1 := σhd ≥ 100 (Laptop)R2 := Product (R1)R3 := πmaker (R2)Exercise R1 := σmaker=B (ProductPC)R2 := σmaker=B (Product Laptop)R3 := σmaker=B (Product Printer)R4 := πmodel,price (R1)R5 := πmodel,price (R2)R6: = πmodel,price (R3)R7 := R4 R5R6 modelprice 1004649 1005630 1006 1049 model 1005 1006 1013maker E A B FG20071429ExerciseR1 := σcolor = true AND type = laser (Printer)R2 := πmodel (R1)ExerciseR1 := σtype=laptop (Product)R2 := σtype=PC(Product)R3 := πmaker(R1)R4 := πmaker(R2)R5 := R3 – R4Exercise PC1R2 := ρPC2(PC)R3 := R1 = AND <> R2R4 := πhd(R3)Exercise R1 := ρPC1(PC)R2 := ρPC2(PC)R3 := R1 = AND = AND < R2R4 := π,(R3)Exercise R1 := πmodel(σspeed ≥ (PC)) πmodel(σspeed ≥ (Laptop)) R2 := πmaker,model(R1 Product)R3 := ρR3(maker2,model2)(R2)R4 := R2 (maker = maker2 AND model <> model2) R3R5 := πmaker(R4)ExerciseR1 := πmodel,speed(PC)R2 := πmodel,speed(Laptop)R3 := R1 R2R4 := ρR4(model2,speed2)(R3)R5 := πmodel,speed (R3 (speed < speed2 ) R4)R6 := R3 – R5makerBExercise R1 := πmaker,speed(Product PC)R2 := ρR2(maker2,speed2)(R1)R3 := ρR3(maker3,speed3)(R1)R4 := R1 (maker = maker2 AND speed <> speed2) R2R5 := R4 (maker3 = maker AND speed3 <> speed2 AND speed3 <> speed) R3R6 := πmaker(R5)model30033007makerFGhd2508016010041012makerBEExerciseR1 := πmaker,model (Product PC)R2 := ρR2(maker2,model2)(R1)R3 := ρR3(maker3,model3)(R1)R4 := ρR4(maker4,model4)(R1)R5 := R1 (maker = maker2 AND model <> model2) R2R6 := R3 (maker3 = maker AND model3 <> model2 AND model3 <> model) R5R7 := R4 (maker4 = maker AND (model4=model OR model4=model2 OR model4=model3)) R6R8 := πmaker (R7)Exercise πmodelσspeed≥3.00PC Laptop σhd ≥ 100 Productπmakerσmaker=Bπmodel,price σmaker=B πmodel,priceσmaker=B πmodel,price Product PC Laptop Printer Product Productmaker A D E maker A B D EPrinter σcolor = true AND type = laser πmodelσtype=laptopσtype=PC πmakerπmaker –Product Product ρPC1ρPC2 (PC1.hd = PC2.hd AND PC1.model <> PC2.model)πhd PC PC ρPC1ρPC2PC PC (PC1.speed = PC2.speed AND PC1.ram = PC2.ram AND PC1.model < PC2.model)πPC1.model,PC2.modelPC Laptopσspeed ≥ 2.80σspeed ≥ 2.80πmodelπmodel πmaker,modelρR3(maker2,model2)(maker = maker2 AND model <> model2)makerPC LaptopProductπmodel,speed πmodel,speed ρR4(model2,speed2)πmodel,speed(speed < speed2 )–makerProduct PC πmaker,speed ρR3(maker3,speed3)ρR2(maker2,speed2)(maker = maker2 AND speed <> speed2)(maker3 = maker AND speed3 <> speed2 AND speed3 <> speed)makerProduct PC πmaker,modelρR2(maker2,model2)ρR3(maker3,model3)ρR4(maker4,model4)(maker = maker2 AND model <> model2)(maker3 = maker AND model3 <> model2 AND model3 <> model)(maker4 = maker AND (model4=model OR model4=model2 OR model4=model3))πmakerR1 := σbore ≥ 16 (Classes)Exercise R1 := σlaunched < 1921 (Ships)R2 := πname (R1)ResolutionRevengeRoyal OakRoyal SovereignTennesseeExercise R1 := σbattle=Denmark Strait AND result=sunk(Outcomes)R2 := πship (R1)shipBismarckHoodExercise R1 := Classes ShipsR2 := σlaunched > 1921 AND displacement > 35000 (R1)R3 := πname (R2)nameIowaMissouriMusashiNew JerseyNorth CarolinaWashingtonWisconsinYamatoExercise R1 := σbattle=Guadalcanal(Outcomes)R2 := Ships (ship=name) R1R3 := Classes R2R4 := πname,displacement,numGuns(R3)name displacement numGuns Kirishima320008Washington370009Exercise R1 := πname(Ships)R2 := πship(Outcomes)R3 := ρR3(name)(R2)R4 := R1 R3nameCaliforniaHarunaHieiIowaKirishimaKongoMissouriMusashiNew JerseyNorth CarolinaRamilliesRenownRepulseResolutionRevengeRoyal OakRoyal SovereignTennesseeWashingtonWisconsinExercise From assuming that every class has one ship named after the class.R1 := πclass (Classes) R2 := πclass (σname <> class (Ships)) R3 := R1 – R2Exercise R1 := πcountry (σtype=bb (Classes)) R2 := πcountry (σtype=bc (Classes)) R3 := R1 ∩ R2ExerciseR1 := πship,result,date (Battles (battle=name) Outcomes)R2 := ρR2(ship2,result2,date2)(R1) R3 := R1 (ship=ship2 AND result=damaged AND date < date2) R2R4 := πship (R3)No results from sample data.Exercise σbore ≥ 16 πclass,country Classes σlaunched < 1921 πname Ships Outcomes πshipσbattle=Denmark Strait AND result=sunkClasses Ships σlaunched > 1921 AND displacement > 35000πname σbattle=Guadalcanal Outcomes (ship=name)πname,displacement,numGunsYamato Arizona Bismarck Duke of York Fuso Hood King George V Prince of Wales Rodney Scharnhorst South Dakota West VirginiaYamashiroclass Bismarck countryJapanGt. BritainShips Outcomesπname πship ρR3(name) ClassesShips πclass σname <> class πclass–Classes Classes σtype=bb σtype=bc πcountry πcountry ∩Battles Outcomes (battle=name)πship,result,dateρR2(ship2,result2,date2)(ship=ship2 AND result=damaged AND date < date2)πshipresult ofthe natural join has only one attribute from each pair of equated attributes. On the other hand, the result of the theta-join has both columns of the attributes and their values are identical.Exercise Union we add a tuple to the arguments of the union operator, we will get all of the tuples of the original result and maybe the added tuple. If the added tuple is a duplicate tuple, then the set behavior will eliminate that tuple. Thus the union operator is monotone.IntersectionIf we add a tuple to the arguments of the intersection operator, we will get all of the tuples of the original result and maybe the added tuple. If the added tuple does not exist in the relation that it is added but does exist in the other relation, then the result set willinclude the added tuple. Thus the intersection operator is monotone.DifferenceIf we add a tuple to the arguments of the difference operator, we may not get all of the tuples of the original result. Suppose we have relations R and S and we are computing R – S. Suppose also that tuple t is in R but not in S. The result of R – S would include tuple t . However, if we add tuple t to S, then the new result will not have tuple t . Thus the difference operator is not monotone.ProjectionIf we add a tuple to the arguments of the projection operator, we will get all of the tuples of the original result and the projection of the added tuple. The projection operator only selects columns from the relation and does not affect the rows that are selected. Thus the projection operator is monotone.SelectionIf we add a tuple to the arguments of the selection operator, we will get all of the tuples of the original result and maybe the added tuple. If the added tuple satisfies the select condition, then it will be added to the new result. The original tuples are included in thenew result because they still satisfy the select condition. Thus the selection operator is monotone.Cartesian ProductIf we add a tuple to the arguments of the Cartesian product operator, we will get all of the tuples of the original result and possiblyadditional tuples. The Cartesian product pairs the tuples of one relation with the tuples of another relation. Suppose that we arecalculating R x S where R has m tuples and S has n tuples. If we add a tuple to R that is not already in R, then we expect the result of R x S to have (m + 1) * n tuples. Thus the Cartesian product operator is monotone.Natural JoinsIf we add a tuple to the arguments of a natural join operator, we will get all of the tuples of the original result and possibly additional tuples. The new tuple can only create additional successful joins, not less. If, however, the added tuple cannot successfully join withany of the existing tuples, then we will have zero additional successful joins. Thus the natural join operator is monotone.Theta JoinsIf we add a tuple to the arguments of a theta join operator, we will get all of the tuples of the original result and possibly additional tuples. The theta join can be modeled by a Cartesian product followed by a selection on some condition. The new tuple can onlycreate additional tuples in the result, not less. If, however, the added tuple does not satisfy the select condition, then no additionaltuples will be added to the result. Thus the theta join operator is monotone.RenamingIf we add a tuple to the arguments of a renaming operator, we will get all of the tuples of the original result and the added tuple. The renaming operator does not have any effect on whether a tuple is selected or not. In fact, the renaming operator will always return as many tuples as its argument. Thus the renaming operator is monotone.Exercise all the tuples of R and S are different, then the union has n + m tuples, and this number is the maximum possible.The minimum number of tuples that can appear in the result occurs if every tuple of one relation also appears in the other. Then the union has max(m , n) tuples.Exercise all the tuples in one relation can pair successfully with all the tuples in the other relation, then the natural join has n * m tuples. This number would be the maximum possible.The minimum number of tuples that can appear in the result occurs if none of the tuples of one relation can pair successfully with all the tuples in the other relation. Then the natural join has zero tuples.Exercise the condition C brings back all the tuples of R, then the cross product will contain n * m tuples. This number would be the maximum possible.The minimum number of tuples that can appear in the result occurs if the condition C brings back none of the tuples of R. Then the cross product has zero tuples.Exercise that the list of attributes L makes the re sulting relation πL(R) and relation S schema compatible, then the maximum possible tuples is n. This happens when all of the tuples of πL(R) are not in S.The minimum number of tuples that can appear in the result occurs when all of the tuples in πL(R) appear in S. Then the difference has max(n–m , 0) tuples.Exercise r as the schema of R and s as the schema of S:1.πr(R S)2.R δ(πr∩s(S)) where δ is the duplicate-elimination operator in Section pg. 2133.R – (R –πr(R S))Exercise r as the schema of R1.R - πr(R S)Exercise …An(R S)Exercise < AND price > 500(PC) = øModel 1011 violates this constraint.Exercise < AND hd < 100 AND price ≥ 1000(Laptop) = øModel 2004 violates the constraint.Exercise = laptop(Product)) ∩ πmaker(σtype = pc(Product)) = øManufacturers A,B,E violate the constraint.Exercise complex expression is best seen as a sequence of steps in which we define temporary relations R1 through R4 that stand for nodes of expression trees. Here is the sequence:R1(ma ker, model, speed) := πmaker,model,speed(Product PC)R2(maker, speed) := πmaker,speed(Product Laptop)R3(model) := πmodel(R1 = AND ≤ R2)R4(model) := πmodel(PC)The constraint is R4 ⊆ R3Manufacturers B,C,D violate the constraint.Exercise > AND ≤ (PC × Laptop)) = øModels 2002,2006,2008 violate the constraint.Exercise > 16(Classes)) = øThe Yamato class violates the constraint.Exercise > 9 AND bore > 14(Classes)) = øNo violations to the constraint.Exercise complex expression is best seen as a sequence of steps in which we define temporary relations R1 through R5 that stand for nodes of expression trees. Here is the sequence:R1(class,name) := πclass,name(Classes Ships)R2(class2,name2) := ρR2(class2,name2)(R1)R3(class3,name3) := ρR3(class3,name3)(R1)R4(class,name,class2,name2) := R1 (class = class2 AND name <> name2) R2R5(class,name,class2,name2,class3,name3) := R4 (class=class3 AND name <> name3 AND name2 <> name3) R3The constraint i s R5 = øThe Kongo, Iowa and Revenge classes violate the constraint.Exercise = bb(Classes)) ∩ πcountry(σtype = bc(Classes)) = øJapan and Gt. Britain violate the constraint.Exercise complex expression is best seen as a sequence of steps in which we define temporary relations R1 through R5 that stand for nodes of expression trees. Here is the sequence:R1(ship,battle,result,class) := πship,battle,result,class(Outcomes (ship = name) Ships)R2(ship,battle,result,numGuns) := πship,battle,result,numGuns(R1 Classes)R3(ship,battle) := πship,battle(σnumGuns < 9 AND result = sunk (R2))R4(ship2,battle2) := ρR4(ship2,battle2)(πship,battle(σnumGuns > 9(R2)))R5(ship2) := πship2(R3 (battle = battle2) R4)The constraint is R5 = øNo violations to the constraint. Since there are some ships in the Outcomes table that are not in the Ships table, we are unable to determine the number of guns on that ship.Exercise r as the schema A1,A2,…,A n and s as the schema B1,B2,…,B n:πr(R) πs(S) = øwhere is the antisemijoinExercise form of a constraint as E1 = E2 can be expressed as the other two constraints.Using the “equating an expression to the empty set” method, we can simply say:E1– E2= øAs a containment, we can simply say:E1⊆ E2 AND E2⊆ E1Thus, the form E1 = E2 of a constraint cannot express more than the two other forms discussed in this section.。
数据库系统原理第二章基本概念及课后习题有答案

数据库系统原理第二章基本概念及课后习题有答案一、数据库系统生存期1.数据库系统生存期:数据库应用系统从开始规划、设计、实现、维护到最后被新的系统取代而停止使用的整个期间。
2.数据库系统生存期分七个阶段:规划、需求分析、概念设计、逻辑设计、物理设计、实现、运行维护。
3.规划阶段三个步骤:系统调查、可行性分析、确定数据库系统总目标。
4.需求分析阶段:主要任务是系统分析员和用户双方共同收集数据库系统所需要的信息内容和用户对处理的需求,并以需求说明书的形式确定下来。
5.概念设计阶段:产生反映用户单位信息需求的概念模型。
与硬件和DBMS无关。
6.逻辑设计阶段:将概念模型转换成DBMS能处理的逻辑模型。
外模型也将在此阶段完成。
7.物理设计阶段:对于给定的基本数据模型选取一个最适合应用环境的物理结构的过程。
数据库的物理结构主要指数据库的存储记录格式、存储记录安排和存取方法。
8.数据库的实现:包括定义数据库结构、数据装载、编制与调试应用程序、数据库试运行。
二、ER模型的基本概念ER模型的基本元素是:实体、联系和属性。
2.实体:是一个数据对象,指应用中可以区别的客观存在的事物。
实体集:是指同一类实体构成的集合。
实体类型:是对实体集中实体的定义。
一般将实体、实体集、实体类型统称为实体。
3.联系:表示一个或多个实体之间的关联关系。
联系集:是指同一类联系构成的集合。
联系类型:是对联系集中联系的定义。
一般将联系、联系集、联系类型统称为联系。
4.同一个实体集内部实体之间的联系,称为一元联系;两个不同实体集实体之间的联系,称为二元联系,以此类推。
5.属性:实体的某一特性称为属性。
在一个实体中,能够惟一标识实体的属性或属性集称为实体标识符。
6. ER模型中,方框表示实体、菱形框表示联系、椭圆形框表示属性、实体与联系、实体与其属性、联系与其属性之间用直线连接。
实体标识符下画横线。
联系的类型要在直线上标注。
注意:联系也有可能存在属性,但联系本身没有标识符。
数据库第二章习题及答案

第二章 关系数据库习题二一、单项选择题:1、系数据库管理系统应能实现的专门关系运算包括 B 。
A .排序、索引、统计 B.选择、投影、连接 C .关联、更新、排序 D.显示、打印、制表2、关系模型中,一个关键字是 C 。
A .可由多个任意属性组成 B .至多由一个属性组成C .可由一个或多个其值能惟一标识该关系模型中任何元组的属性组成D .以上都不是3、个关系数据库文件中的各条记录 B 。
A .前后顺序不能任意颠倒,一定要按照输入的顺序排列B .前后顺序可以任意颠倒,不影响库中的数据关系C .前后顺序可以任意颠倒,但排列顺序不同,统计处理的结果就可能不同D .前后顺序不能任意颠倒,一定要按照关键字段值的顺序排列 4、有属性A ,B ,C ,D ,以下表示中不是关系的是 C 。
A .R (A ) B .R (A ,B ,C ,D ) C .D)C B R(A ⨯⨯⨯ D .R (A ,B )5、概念模型中,一个实体相对于关系数据库中一个关系中的一个 B 。
A 、属性 B 、元组 C 、列 D 、字段二、设有一个SPJ 数据库,包括S ,P ,J ,SPJ 四个关系模式: S( SNO ,SNAME ,STA TUS ,CITY); P(PNO ,PNAME ,COLOR ,WEIGHT); J(JNO ,JNAME ,CITY);SPJ(SNO ,PNO ,JNO ,QTY);供应商表S 由供应商代码(SNO )、供应商姓名(SNAME )、供应商状态(STATUS )、供应商所在城市(CITY )组成;零件表P 由零件代码(PNO )、零件名(PNAME )、颜色(COLOR )、重量(WEIGHT )组成; 工程项目表J 由工程项目代码(JNO )、工程项目名(JNAME )、工程项目所在城市(CITY )组成; 供应情况表SPJ 由供应商代码(SNO )、零件代码(PNO )、工程项目代码(JNO )、供应数量(QTY )组成,表示某供应商供应某种零件给某工程项目的数量为QTY 。
数据库学习课程第二章习题和答案

数据库学习课程第二章习题和答案一.单项选择1.SQL语言是 B 的语言,易学习.A 过程化B 非过程化C 格式化D 导航式提示:SQL是一种介于关系代数与关系演算之间的结构化查询语言,它是高度非过程化的.2.SQL语言是 C 语言.A 层次数据库B 网络数据库C 关系数据库D 非数据库提示:SQL是关系数据库标准语言.3.SQL语言具有 B 的功能.A 关系规范化,数据操纵,数据控制B 数据定义,数据操纵,数据控制C 数据定义,关系规范化,数据控制D 数据定义,关系规范化,数据操纵提示:SQL语言自身不具备关系规范化功能.4.在SQL中,用户可以直接操作的是 D .A 基本表B 视图C 基本表和视图D 基本表和视图5.在SQL语言中,实现数据检索的语句是 A .A SELECTB INSERTC UPDATED DELETE6.SELECT语句执行结果是 C .A 数据项B 元组C 表D 数据库7.在SQL语句中,对输出结果排序的语句是 B .A GROUP BYB ORDER BYC WHERED HA VING8.在SELECT语句中,需对分组情况满足的条件进行判断时,应使用 D .A WHEREB GROUP BYC ORDER BYD HA VING9.在SELECT语句中使用*表示 B .A 选择任何属性B 选择全部属性C 选择全部元组D 选择主码10.在SELECT语句中,使用MAX(列名)时,该”列名”应该 D .A 必须是数值型B 必须是字符型C 必须是数值型或字符型D 不限制数据类型11.使用CREATE TABLE语句创建的是 B .A 数据库B 表C 试图D 索引12.下列SQL语句中,修改表结构的是 A .A ALTERB CREATEC UPDATED INSERT13.在SQL中使用UPDATE语句对表中数据进行修改时,应使用的语句是 D .A WHEREB FROMC V ALUESD SET14.视图建立后,在数据库中存放的是 C .A 查询语句B 组成视图的表的内容C 视图的定义D 产生视图的表的定义15.以下叙述中正确的是 B .A SELECT命令是通过FOR子句指定查询条件B SELECT命令是通过WHERE子句指定查询条件C SELECT命令是通过WHILE子句指定查询条件D SELECT命令是通过IS子句指定查询条件16.与WHERE AGE BETWEEN 18 AND 23完全等价的是 D .A WHERE AGE>18 AND AGE<23B WHERE AGE<18 AND AGE>23C WHERE AGE>18 AND AGE<=23 D WHERE AGE>=18 AND AGE<=2317.在查询中统计记录(元组)的个数时,应使用 C 函数.A SUMB COUNT(列名)C COUNT(*)D A VG18.在查询中统计某列中值的个数应使用 B 函数.A SUMB COUNT(列名)C COUNT(*)D A VG19.已知基本表SC(S#,C#,GRADE),其中S#为学号,C#为课程号,GRADE为成绩.则”统计选修了课程的学生人数”的SQL—SELECT语句为 A .A SELECT COUNT(DISTINCT S#)FROM SCB SELECT COUNT(S#)FROM SC C SELECT COUNT()FROM SCD SELECT COUNT(DISTINCT *)FROM SC20.在数据库中有如图所示的两个表,若职工表的主码是职工号,部门表的主码是部门号,SQL 操作 B 不能执行.A 从职工表中删除行(‘025’,’王芳’,’03’,720)B 将行(‘005’,’乔兴’,’04’,750)插入到职工表中C 将职工号为”001”的工资改为700D 将职工号为”038”的部门号改为’03’提示:由于职工表中的职工号为主码,不能向其中插入同主码的记录.21.若用如下SQL语句创建一个表studentCREATE TABLE student(NO CHAR(4) NOT NULL,NAME CHAR(8) NOT NULL,SEX CHAR(2),AGE INT)可以插入到student 表中的是 B .A (‘1031’,’曾华’,男,23)B (‘1031’,’曾华’,NULL,NULL)C (NULL,’曾华’,’男’,’23’)D (‘1031’,NULL,’男’,23)提示:A 中性别SEX 属性值格式不正确,C 中NO 属性值不能为空,D 中NAME 属性值不能为空.22.假设学生关系是S(S#,SNAME,SEX,AGE),课程关系是C(C#,CNAME,TEACHER),学生选课关系是SC(S#,C#,GRADE).要查询选修”COMPUTER ”课程的”女”同学的姓名,将涉及关系 D .A SB SC,C C S,SCD S,SC,C 二.填空1.SQL 语言的数据定义功能包括 定义数据 、 定义基本表 、 定义视图 、 定义索引 。
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
第3部分 习题及其解答第一章的两道题设计NM编号开始时间姓名性别年龄单位职称结束时间程序名称版权价格专利号厂址工厂名称联系电话3-2 习题22.6 分别把习题1.10、习题1.11的ER图转换成关系模型数据结构。
【参考答案】1.习题1.10的ER图可转换成如下的关系模型数据结构。
①程序员(编号,姓名,性别,年龄,单位,职称),其中编号是关键字;N雇用月薪雇用期②程序(程序名称,版权,专利号,价格),其中程序名称是关键字;③设计(编号,程序名称,开始时间,结束时间),其中(编号,程序名称)是关键字。
2.习题1.11的ER图可转换成如下的关系模型数据结构。
①工厂(工厂名称,厂址,联系电话),其中工厂名称是关键字;②产品(产品号,产品名,规格,单价),其中产品号是关键字;③工人(工人编号,姓名,性别,职称,工厂名称,雇用期,月薪),其中工人编号是关键字,工厂名称是外关键字,雇用期和月薪是联系属性;④生产(工厂名称,产品号,月产量),其中(工厂名称,产品号)是关键字,生产关系是表示联系的。
2.8 判断下列情况,分别指出它们具体遵循那一类完整性约束规则?1.用户写一条语句明确指定月份数据在1~12之间有效。
2.关系数据库中不允许主键值为空的元组存在。
3.从A关系的外键出发去找B关系中的记录,必须能找到。
【解答】1.用户用语句指定月份数据在1~12之间有效,遵循用户定义的完整性约束规则。
2.关系数据库中不允许主键值为空的元组存在,遵循实体完整性约束规则;3.从A关系的外键出发去找B关系的记录,必须能找到,遵循引用完整性约束规则。
2.9 判断下列情况,分别指出他们是用DML 还是用DDL 来完成下列操作?1.创建“学生”表结构。
2.对“学生”表中的学号属性,其数据类型由“整型”修改为“字符型”。
3.把“学生”表中学号“021”修改为“025”。
【解答】1.创建“学生”表结构,即定义一个关系模式,用DDL 完成。
2.修改“学生”表中学号属性的数据类型,即修改关系模式的定义,用DDL 完成。
3.修改“学生”表中学号属性的数据值,即对表中的数据进行操作,用DML 完成。
2.12 给出两个学生选修课程关系A 和B ,属性为姓名、课程名、成绩。
分别写出后列各关系代数运算的结果关系。
1.A 和2.σ3.π1,3(σ2='数学'(B ));π姓名(σ成绩>'75'(A B ));π姓名(σ课程名='数学'∨课程名='英语' (A -B ))。
4.B A 11=; B A3322>∧= 。
5.A [] B ; A ]B ; A [ B 。
【解答】1.结果关系见表3.2(a)~表3.2(e)。
2.结果关系见表3.2(f)~表3.2(i)。
3.结果关系见表3.2(j)~表3.2(l)。
4.结果关系见表3.2(m)~表3.2(n)。
5.结果关系见表3.2(o)~表3.2(q)。
2.15学生关系student(sno,sname,sex,birth,height,class,address)课程关系course(cno,cname,credit)选修关系elective(sno,cno,grade)用关系代数表达式表达下列查询:1.检索学习课程号为C06的学生学号与成绩。
2.检索学习课程号为C06的学生学号与姓名。
3.检索学习课程名为ENGLISH的学生学号与姓名。
4.检索选修课程号为C02或C06的学生学号。
5.检索至少选修课程号为C02和C06的学生学号。
6.检索没有选修C06课程的学生姓名及其所在班级。
7.检索学习全部课程的学生姓名。
8.检索学习课程中包含了S08学生所学课程的学生学号。
【解答】1.检索学习课程号为C06的学生学号与成绩。
πsno, grade (σcno='C06' (elective)) 或π1,3 (σ2='C06' (elective)) 2.检索学习课程号为C06的学生学号与姓名。
πsno, sname (σcno='C06' (student elective))3.检索学习课程名为ENGLISH的学生学号与姓名。
πsno, sname (σcname='ENGLISH' (student elective course)) 4.检索选修课程号为C02或C06的学生学号。
πsno (σcno='C02'∨cno='C06' (elective))5.检索至少选修课程号为C02和C06的学生学号。
πsno (σ1=4∧2='C02'∧5='C06' (elective⨯elective))6.检索没有选修C06课程的学生姓名及其所在班级。
πsname, class (student) - πsname, class (σcno='C06' (student elective)) 7.检索学习全部课程的学生姓名。
πsname (student (πsno, cno (elective) ÷πcno (course))) 8.检索学习课程中包含了S08学生所学课程的学生学号。
πsno, cno (elective) ÷ (πcno (σsno='S08'(elective)))2.25 已知关系模式R(A,B,C,D,E)和函数依赖集F={AB→C, B→D, C→E, EC→B, AC→B,D→BE},试问AC→BE能否从F导出?【解答】方法一:运用推理规则推导。
对已知的AC→B和B→D,根据Å3传递律,AC→D成立;对已证的AC→D和已知的D→BE,根据Å3传递律,AC→BE成立;即AC→BE能从F中导出。
方法二:按算法2.1(求属性集合X关于函数依赖集F的闭包X+),求(AC)+。
(1) 置初始值A=ф,A*=AC;(2) 因A≠A*,置A=AC;(3) 第一次扫描F,找到C→E和AC→B,其左部⊆AC,故置A*=ACEB。
搜索完,转(4);(4) 因A≠A*,置A=ACEB;(5) 第二次扫描F,找到AB→C,B→D和EC→B,其左部⊆ACEB,故置A*=ACEBD。
搜索完,转(6);(6) 因A≠A*,置A=ACEBD;(7) 第三次扫描F,找到D→BE,其左部⊆ACEBD,故置A*=ACEBD。
搜索完,转(8);(8) 因A=A*,转(9);(9) 输出A*,即(AC)+=ACEBD。
因为BE⊆(AC)+ ,所以AC→BE能够从F中导出。
方法三:运行算法2.1的VB程序,输入相应数据后,得出(AC)+=ACEBD,如图3.5所示。
因为BE⊆(AC)+ ,所以AC→BE能够从F中导出。
图3.5 运行算法2.1的VB程序,求(AC)+。
2.求属性集BG关于F的闭包(BG)+,其算法步骤如下:(1) 置初始值A=ф,A*=BG;(2) 因A≠A*,置A=BG;(3) 第一次扫描F,找到B→CE,其左部⊆BG,故置A*=BGCE。
搜索完,转(4);(4) 因A≠A*,置A=BGCE;(5) 第二次扫描F,找到GC→A,其左部⊆BGCE,故置A*=BGCEA。
搜索完,转(6);(6) 因A≠A*,置A=BGCEA;(7) 第三次扫描F,找到AC→PE,A→P,GA→B和AE→GB,其左部⊆BGCEA,故置A*=BGCEAP。
搜索完,转(8);(8) 因A≠A*,置A=BGCEAP;(9) 第四次扫描F,找到PG→A,PAB→G和ABCP→H,其左部⊆BGCEAP,故置A*=BGCEAPH。
搜索完,转(10);(10) 因A≠A*,置A=BGCEAPH;(11) 第五次扫描F,找不到其左部⊆BGCEAPH的函数依赖,转(12);(12) 因A=A*,转(13);(13) 输出A*,即(BG)+=BGCEAPH。
运行算法2.1的VB程序,输入相应数据后,可验证(BG)+=BGCEAPH,如图3.7所示。
图3.7 运行算法2.1的VB程序,求(BG)+。
2.29 已知关系模式R(A,B,C,D,E)和函数依赖集F={A→D,E→D,D→B,BC→D,DC→A},问分解ρ={R1(A,B),R2(A,E),R3(E,C),R4(D,B,C),R5(A,C)}是否为R的无损联接分解。
【解答】1.根据“测试一个分解ρ是否为无损连接分解”的算法,首先建立习题2.29的初始Rρ表,如表3.6(1)所示。
2.反复检查F的每一个函数依赖,修改Rρ表中的元素,一直到表格不能修改为止。
①对A→D,第1,2,5行的A同为a1,把这三行的D均改为b14;②对E→D,第2,3行的E同为a5,把这两行的D均改为b14;③对D→B,第1,2,3,5行的D同为b14,把这四行的B均改为a2;④对BC→D,第3,4,5行的BC同为(a2,a3),把这三行的D均改为a4;⑤对DC→A,第3,4,5行的DC同为(a4,a3),把这三行的A均改为a1;⑥重复扫描F,对A→D,五行的A同为a1,把这五行的D均改为a4;⑦表格不能再修改了,算法终止,结果Rρ表如表3.6(2)所示。
第3行全为a,即ρ是R的无损联接分解。
若本题用算法2.2的VB程序解题,结果见图3.8。
图3.8 习题2.29用算法2.2的VB程序解题2.30 试分析下列分解是否具有无损联接和保持函数依赖的特点。
1.设R1(ABC),F1={A→B},ρ1={AB,AC}。
2.设R2(ABC),F2={A→C,B→C},ρ2={AB,AC}。
3.设R3(ABC),F3={A→B},ρ3={AB,BC}。
表3.6(2) 习题2.29的结果Rρ表A B C D EABAEECDBCACa1a1a1a1a1a2a2a2a2a2b13b23a3a3a3a4a4a4a4a4b15a5a5b45b554.设R4(ABC),F4={A→B,B→C},ρ4={AC,BC}。
5.设R5(ABC),F5={AB→C,C→A},ρ5={AC,BC}。
【解答】1.因为AB∩AC=A,AB-AC=B,A→B成立,所以分解ρ1具有无损连接性。
运行算法2.2的VB程序如图3.9(a)所示,验证结果正确。
因为π{AB}(F1)∪π{AC}(F1) = A→B = F1 ;所以分解ρ1是保持函数依赖集F1的。
图3.9(a) 分解ρ1具有无损连接性2.因为AB∩AC=A,AC-AB=C,A→C成立,所以分解ρ2具有无损连接性。
运行算法2.2的VB程序如图3.9(b)所示,验证结果正确。