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

下载本文档

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

文档简介

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 relation 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 and 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 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 employee (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, street, 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 Figure 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 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 accomplishes 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 Jackson 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 street 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 employees 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 Corpora- 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.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”(works) 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-holders2 ? 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 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 average 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(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(company-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 known 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) books(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 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 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. Answer: 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 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) owns (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.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- 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. Find 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, company 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.employee 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 thei

温馨提示

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

评论

0/150

提交评论