数据库系统概念答案(第五版)_第1页
数据库系统概念答案(第五版)_第2页
数据库系统概念答案(第五版)_第3页
数据库系统概念答案(第五版)_第4页
数据库系统概念答案(第五版)_第5页
已阅读5页,还剩12页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、Exercises 2.4 Describethedifferencesinmeaningbetweenthetermsrelationandrelationschema. Answer: A relation schema is a type defi nition, and a relation is an instance of that schema. For example, student (ss#, name) is a relation schema and ss#name 123-45-6789Tom Jones 456-78-9123Joe Brown is a relat

2、ion based on that schema. 2.5 Consider the relational database of Figure 2.35, where the primary keys are un- derlined. Give an expression in the relational algebra to express each of the fol- lowing queries: a. Find the names of all employees who work for First Bank Corporation. b. Find the names a

3、nd cities of residence of all employees who work for First Bank Corporation. c. Find the names, street address, and cities of residence of all employees who work for First Bank Corporation and earn more than $10,000 per annum. d. Find the names of all employees in this database who live in the same

4、city as the company for which they work. e. Assume the companies may be located in several cities. Find all companies located in every city in which Small Bank Corporation is located. Answer: a. person-name(company-name = “First Bank Corporation” (works) 7 CHAPTER2 8Chapter 2Relational Model employe

5、e (person-name, street, city) works (person-name, company-name, salary) company (company-name, city) manages (person-name, manager-name) Figure 2.35. Relational database for Exercises 2.1, 2.3 and 2.9. b. person-name,city(employee 1 (company-name = “First Bank Corporation” (works) c. person-name, st

6、reet, city (company-name = “First Bank Corporation” salary 10000) works 1 employee) d. person-name(employee 1 works 1 company) e. Note: Small Bank Corporation will be included in each answer. company-name(company (city(company-name=“Small Bank Corporation”(company) 2.6 Consider the relation of Figur

7、e 2.20, which shows the result of the query “Find the names of all customers who have a loan at the bank.” Rewrite the query to include not only the name, but also the city of residence for each customer. ObservethatnowcustomerJacksonnolongerappearsintheresult,eventhough Jackson does in fact have a

8、loan from the bank. a. Explain why Jackson does not appear in the result. b. Suppose that you want Jackson to appear in the result. How would you modify the database to achieve this effect? c. Again, suppose that you want Jackson to appear in the result. Write a query using an outer join that accomp

9、lishes this desire without your having to modify the database. Answer: The rewritten query is customer-name,customer-city,amount(borrower 1 loan 1 customer) a. Although Jackson does have a loan, no address is given for Jackson in the customer relation. Since no tuple in customer joins with the Jacks

10、on tuple of borrower, Jackson does not appear in the result. b. The best solution is to insert Jacksons address into the customer relation. If the address is unknown, null values may be used. If the database system does not support nulls, a special value may be used (such as unknown) for Jacksons st

11、reet and city. The special value chosen must not be a plausible name for an actual city or street. c. customer-name,customer-city,amount(borrower 1 loan)1 customer) 2.7 Consider the relational database of Figure 2.35. Give an expression in the rela- tional algebra for each request: a. Give all emplo

12、yees of First Bank Corporation a 10 percent salary raise. Exercises9 b. Give all managersin this database a10 percentsalary raise, unlessthe salary would be greater than $100,000. In such cases, give only a 3 percent raise. c. Delete all tuples in the works relation for employees of Small Bank Corpo

13、ra- tion. Answer: a. works person-name,company-name,1.1salary( (company-name=“First Bank Corporation”)(works) (works company-name=“First Bank Corporation”(works) b. The same situation arises here. As before, t1, holds the tuples to be updated and t2holds these tuples in their updated form. t1 works.

14、person-name,company-name,salary (works.person-name=manager-name(works manages) t2 works.person-name,company-name,salary1.03 (t1.salary 1.1 100000(t1) t2 t2 (works.person-name,company-name,salary1.1 (t1.salary 1.1 100000(t1) works (works t1) t2 c. works works companyname=“Small Bank Corporation”(work

15、s) 2.8 Using the bank example, write relational-algebra queries to fi nd the accounts held by more than two customers in the following ways: a. Using an aggregate function. b. Without using any aggregate functions. Answer: a. t1 account-numberGcount customer-name(depositor) account-number ? num-hold

16、ers2 ? account-holders(account-number,num-holders)(t1) ? b. t1 (d1(depositor) d2(depositor) d3(depositor) t2 (d1.account-number=d2.account-number=d3.account-number)(t1) d1.account-number(d1.customer-name?=d2.customer-name d2.customer-name?=d3.customer-name d3.customer-name?=d1.customer-name)(t2) 2.9

17、 Consider the relational database of Figure 2.35. Give a relational-algebra expres- sion for each of the following queries: a. Find the company with the most employees. b. Find the company with the smallest payroll. c. Find those companies whose employees earn a higher salary, on average, than the a

18、verage salary at First Bank Corporation. Answer: 10Chapter 2Relational Model a. t1 company-nameGcount-distinct person-name(works) t2 maxnum-employees(company-strength(company-name,num-employees)(t1) company-name(t3(company-name,num-employees)(t1) 1 t4(num-employees)(t2) b. t1 company-nameGsumsalary(

19、works) t2 minpayroll(company-payroll(company-name,payroll)(t1) company-name(t3(company-name,payroll)(t1) 1 t4(payroll)(t2) c. t1 company-nameGavgsalary(works) t2 company-name = “First Bank Corporation”(t1) pany-name(t3(company-name,avg-salary)(t1) 1t3.avg-salary first-bank.avg-salary(first-bank(comp

20、any-name,avg-salary)(t2) 2.10 List two reasons why null values might be introduced into the database. Answer: Nulls may be introduced into the database because the actual value is either unknown or does not exist. For example, an employee whose address has changed and whose new address is not yet kn

21、own should be retained with a null address. If employee tuples have a composite attribute dependents, and a particular employee has no dependents, then that tuples dependents attribute should be given a null value. 2.11 Consider the following relational schema employee(empno, name, offi ce, age) boo

22、ks(isbn, title, authors, publishe r) loan(empno, isbn, date) Write the following queries in relational algebra. a. Find the names of employees who have borrowed a book published by McGraw-Hill. b. Find the names of employees who have borrowed all books published by McGraw-Hill. c. Find the names of

23、employees who have borrowed more than fi ve different books published by McGraw-Hill. d. For each publisher, fi nd the names of employees who have borrowed more than fi ve books of that publisher. Answer: No answer Exercises 3.8 Consider the insurance database of Figure 3.11, where the primary keys

24、are un- derlined. Construct the followingSQLqueries for this relational database. a. Find the number of accidents in which the cars belonging to “John Smith” were involved. b. Update the damage amount for the car with license number “AABB2000” in the accident with report number “AR2197” to $3000. An

25、swer: Note: The participated relation relates drivers, cars, and accidents. a.SQLquery: selectcount (distinct *) fromaccident whereexists (select * from participated, person where participated.driver id = person.driver id and = John Smith and accident.report number = participated.report

26、number) b.SQLquery: update participated set damage amount = 3000 where report number = “AR2197” and driver id in (select driver id from owns where license = “AABB2000”) 11 CHAPTER3 12Chapter 3SQL person (driver id, name, address) car (license, model, year) accident (report number, date, location) ow

27、ns (driver id, license) participated (driver id, car, report number, damage amount) Figure 3.11. Insurance database. employee (employee name, street, city) works (employee name, company name, salary) company (company name, city) manages (employee name, manager name) Figure 3.12. Employee database. 3

28、.9 Consider the employee database of Figure 3.12, where the primary keys are un- derlined. Give an expression inSQLfor each of the following queries. a. Find the names of all employees who work for First Bank Corporation. b. Find all employees in the database who live in the same cities as the com-

29、panies for which they work. c. Find all employees in the database who live in the same cities and on the same streets as do their managers. d. Find all employees who earn more than the average salary of all employees of their company. e. Find the company that has the smallest payroll. Answer: a. Fin

30、d the names of all employees who work for First Bank Corporation. select employee name from works where company name = First Bank Corporation b. Find all employees in the database who live in the same cities as the com- panies for which they work. select e.employee name from employee e, works w, com

31、pany c where e.employee name = w.employee name and e.city = c.city and pany name = pany name c. Find all employees in the database who live in the same cities and on the same streets as do their managers. select P.employee name from employee P, employee R, manages M where P.employee name = M.employe

32、e name and M.manager name = R.employee name and P.street = R.street and P.city = R.city Exercises13 d. Find all employees who earn more than the average salary of all employees of their company. Thefollowing solution assumes thatall peoplework for at mostone com- pany. select employee name from work

33、s T where salary (select avg (salary) from works S where T.company name = S.company name) e. Find the company that has the smallest payroll. select company name from works group by company name having sum (salary) = all (select sum (salary) from works group by company name) 3.10 Consider the relatio

34、nal database of Figure 3.12. Give an expression inSQLfor each of the following queries. a. Give all employees of First Bank Corporation a 10 percent raise. b. Give all managers of First Bank Corporation a 10 percent raise. c. Delete all tuples in the works relation for employees of Small Bank Corpor

35、a- tion. Answer: a. Give all employees of First Bank Corporation a 10-percent raise. (the solu- tion assumes that each person works for at most one company.) update works set salary = salary * 1.1 where company name = First Bank Corporation b. Give all managers of First Bank Corporation a 10-percent

36、 raise. update works set salary = salary * 1.1 where employee name in (select manager name from manages) and company name = First Bank Corporation c. Delete all tuples in the works relation for employees of Small Bank Corpora- tion. delete works where company name = Small Bank Corporation 3.11 Let t

37、he following relation schemas be given: 14Chapter 3SQL R = (A,B,C) S = (D,E,F) Let relations r(R) and s(S) be given. Give an expression inSQLthat is equivalent to each of the following queries. a. A(r) b. B =17(r) c. r s d. A,F(C =D(r s) Answer: a. A(r) select distinct A from r b. B =17(r) select *

38、from r where B = 17 c. r s select distinct * from r, s d. A,F(C =D(r s) select distinct A, F from r, s where C = D 3.12 Let R = (A,B,C), and let r1and r2both be relations on schema R. Give an expression inSQLthat is equivalent to each of the following queries. a. r1 r2 b. r1 r2 c. r1 r2 d. AB(r1) 1

39、BC(r2) Answer: a. r1 r2 (select * from r1) union (select * from r2) b. r1 r2 We can write this using the intersect operation, which is the preferred approach, but for variety we present an solution using a nested subquery. Exercises15 select * from r1 where (A, B, C) in (select * from r2) c. r1 r2 s

40、elect from r1 where (A, B, C) not in (select from r2) This can also be solved using the except clause. d. AB(r1) 1 BC(r2) select r1.A, r2.B, r3.C from r1, r2 where r1.B = r2.B 3.13 Show that, inSQL, all is identical to not in. Answer: Let the set S denote the result of anSQLsubquery. We compare (x a

41、ll S) with (x not in S). If a particular value x1 satisfi es (x1 all S) then for all elements y of S x1?= y. Thus x1is not a member of S and must satisfy (x1not in S). Similarly, suppose there is a particular value x2 which satisfi es (x2not in S). It cannot be equal to any element w belonging to S,

42、 and hence (x2 all S) will be satisfi ed. Therefore the two expressions are equivalent. 3.14 Consider the relational database of Figure 3.12. UsingSQL , defi ne a view con- sisting of manager name and the average salary of all employees who work for that manager. Explain why the database system shou

43、ld not allow updates to be expressed in terms of this view. Answer: create view salinfo as select manager name, avg(salary) from manages m, works w where m.employee name = w.employee name group by manager name Updates should not be allowed in this view because there is no way to de- termine how to c

44、hange the underlying data. For example, suppose the request is “change the average salary of employees working for Smith to $200”. Should everybody who works for Smith have their salary changed to $200? Or should the fi rst (or more, if necessary) employee found who works for Smith have their salary

45、 adjusted so that the average is $200? Neither approach really makes sense. 3.15 Write anSQL query, without using a with clause, to fi nd all branches where the total account deposit is less than the average total account deposit at all branches, 16Chapter 3SQL a. Using a nested query in the from cl

46、auser. b. Using a nested query in a having clause. Answer: We output the branch names along with the total account deposit at the branch. a. Using a nested query in the from clauser. select branch name, tot balance from (select branch name, sum (balance) from account group by branch name) as branch

47、total(branch name, tot balance) where tot balance ( select avg (tot balance) from ( select branch name, sum (balance) from account group by branch name) as branch total(branch name, tot balance) ) b. Using a nested query in a having clause. select branch name, sum (balance) from account group by bra

48、nch name having sum (balance) ( select avg (tot balance) from ( select branch name, sum (balance) from account group by branch name) as branch total(branch name, tot balance) ) 3.16 List two reasons why null values might be introduced into the database. Answer: No Answer 3.17 Show how to express the

49、 coalesce operation from Exercise 3.4 using the case operation. Answer: No Answer. 3.18 Give anSQL schemadefi nition for the employee database of Figure 3.12. Choose an appropriate domain for each attribute and an appropriate primary key for each relation schema. Answer: create domaincompany names c

50、har(20) create domaincity names char(30) create domainperson names char(20) create tableemployee Exercises17 (employee nameperson names, streetchar(30), citycity names, primary key(employee name) create tableworks (employee nameperson names, company namecompany names, salarynumeric(8, 2), primary ke

51、y(employee name) create tablecompany (company namecompany names, citycity names, primary key(company name) create tablemanages (employee nameperson names, manager nameperson names, primary key(employee name) 3.19 Usingtherelationsofoursamplebankdatabase, writeSQL expressionstodefi ne the following v

52、iews: a. A view containing the account numbers and customer names (but not the balances) for all accounts at the Deer Park branch. b. A view containing the names and addresses of all customers who have an account with the bank, but do not have a loan. c. A view containingthe name and average account

53、balance of everycustomer of the Rock Ridge branch. Answer: No Answer. 3.20 For each of the views that you defi ned in Exercise 3.19, explain how updates would be performed (if they should be allowed at all). Answer: No Answer. 3.21 Consider the following relational schema employee(empno, name, offi

54、ce, age) books(isbn, title, authors, publisher) loan(empno, isbn, date) Write the following queries inSQL. a. Print the names of employees who have borrowed any book published by McGraw-Hill. 18Chapter 3SQL b. Print the names of employees who have borrowed all books published by McGraw-Hill. c. For

55、each publisher, print the names of employees who have borrowed more than fi ve books of that publisher. Answer: No Answer. 3.22 Consider the relational schema student(student id, student name) registered(student id, course id) Write anSQLquery to list the student-id and name of each student along wi

56、th the total number of courses that the student is registered for. Students who are not registered for any course must also be listed, with the number of registered courses shown as 0. Answer: No Answer. 3.23 Suppose that we have a relation marks(student id, score). Write anSQLquery to fi nd the den

57、se rank of each student. That is, all students with the top mark get a rank of 1, those with the next highest mark get a rank of 2, and so on. Hint: Split the task into parts, using the with clause. Answer: No Answer. Exercises 4.7 Referential-integrity constraints as defi ned in this chapter involv

58、e exactly two relations. Consider a database that includes the following relations: salaried-worker (name, offi ce, phone, salary) hourly-worker (name, hourly-wage) address (name, street, city) Suppose that we wish to require that every name that appears in address appear in either salaried-worker o

59、r hourly-worker, but not necessarily in both. a. Propose a syntax for expressing such constraints. b. Discuss the actions that the system must take to enforce a constraint of this form. Answer: a. For simplicity, we present a variant of theSQLsyntax. As part of the create table expression for address we include foreign key (name) references salaried-worker or hourly-worker b. To enforce this constraint, whenever a tuple is inserted into the address rela- tion,alookuponthenamevaluemustbemadeonthesalaried-workerrelation and (if

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

最新文档

评论

0/150

提交评论