├── _config.yml ├── Chapter 22 - Solutions.md ├── Chapter 7 - Solutions.md ├── .gitattributes ├── Chapter 10 - Solutions.md ├── Chapter 11 - Solutions.md ├── Chapter 12 - Solutions.md ├── Chapter 13 - Solutions.md ├── Chapter 14 - Solutions.md ├── Chapter 15 - Solutions.md ├── Chapter 17 - Solutions.md ├── Chapter 18 - Solutions.md ├── Chapter 19 - Solutions.md ├── Chapter 20 - Solutions.md ├── Chapter 21 - Solutions.md ├── Chapter 23 - Solutions.md ├── Chapter 24 - Solutions.md ├── Chapter 25 - Solutions.md ├── Chapter 26 - Solutions.md ├── Chapter 27 - Solutions.md ├── Chapter 28 - Solutions.md ├── Chapter 29 - Solutions.md ├── Chapter 30 - Solutions.md ├── README.md ├── LICENSE.md ├── Chapter 6 - Solutions.md ├── Chapter 8 - Solutions.md ├── Chapter 9 - Solutions.md ├── Chapter 16 - Solutions.md ├── Chapter 1 - Solutions.md ├── Chapter 2 - Solutions.md ├── Chapter 5 - Solutions.md ├── Chapter 4 - Solutions.md └── Chapter 3 - Solutions.md /_config.yml: -------------------------------------------------------------------------------- 1 | theme: jekyll-theme-cayman -------------------------------------------------------------------------------- /Chapter 22 - Solutions.md: -------------------------------------------------------------------------------- 1 | # Database Management Systems - Chapter 14 Solutions 2 | -------------------------------------------------------------------------------- /Chapter 7 - Solutions.md: -------------------------------------------------------------------------------- 1 | # Database Management Systems - Chapter 7 Solutions 2 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | -------------------------------------------------------------------------------- /Chapter 10 - Solutions.md: -------------------------------------------------------------------------------- 1 | # Database Management Systems - Third Edition Solutions 2 | -------------------------------------------------------------------------------- /Chapter 11 - Solutions.md: -------------------------------------------------------------------------------- 1 | # Database Management Systems - Third Edition Solutions 2 | -------------------------------------------------------------------------------- /Chapter 12 - Solutions.md: -------------------------------------------------------------------------------- 1 | # Database Management Systems - Third Edition Solutions 2 | -------------------------------------------------------------------------------- /Chapter 13 - Solutions.md: -------------------------------------------------------------------------------- 1 | # Database Management Systems - Third Edition Solutions 2 | -------------------------------------------------------------------------------- /Chapter 14 - Solutions.md: -------------------------------------------------------------------------------- 1 | # Database Management Systems - Third Edition Solutions 2 | -------------------------------------------------------------------------------- /Chapter 15 - Solutions.md: -------------------------------------------------------------------------------- 1 | # Database Management Systems - Third Edition Solutions 2 | -------------------------------------------------------------------------------- /Chapter 17 - Solutions.md: -------------------------------------------------------------------------------- 1 | # Database Management Systems - Third Edition Solutions 2 | -------------------------------------------------------------------------------- /Chapter 18 - Solutions.md: -------------------------------------------------------------------------------- 1 | # Database Management Systems - Third Edition Solutions 2 | -------------------------------------------------------------------------------- /Chapter 19 - Solutions.md: -------------------------------------------------------------------------------- 1 | # Database Management Systems - Third Edition Solutions 2 | -------------------------------------------------------------------------------- /Chapter 20 - Solutions.md: -------------------------------------------------------------------------------- 1 | # Database Management Systems - Third Edition Solutions 2 | -------------------------------------------------------------------------------- /Chapter 21 - Solutions.md: -------------------------------------------------------------------------------- 1 | # Database Management Systems - Third Edition Solutions 2 | -------------------------------------------------------------------------------- /Chapter 23 - Solutions.md: -------------------------------------------------------------------------------- 1 | # Database Management Systems - Third Edition Solutions 2 | -------------------------------------------------------------------------------- /Chapter 24 - Solutions.md: -------------------------------------------------------------------------------- 1 | # Database Management Systems - Third Edition Solutions 2 | -------------------------------------------------------------------------------- /Chapter 25 - Solutions.md: -------------------------------------------------------------------------------- 1 | # Database Management Systems - Third Edition Solutions 2 | -------------------------------------------------------------------------------- /Chapter 26 - Solutions.md: -------------------------------------------------------------------------------- 1 | # Database Management Systems - Third Edition Solutions 2 | -------------------------------------------------------------------------------- /Chapter 27 - Solutions.md: -------------------------------------------------------------------------------- 1 | # Database Management Systems - Third Edition Solutions 2 | -------------------------------------------------------------------------------- /Chapter 28 - Solutions.md: -------------------------------------------------------------------------------- 1 | # Database Management Systems - Third Edition Solutions 2 | -------------------------------------------------------------------------------- /Chapter 29 - Solutions.md: -------------------------------------------------------------------------------- 1 | # Database Management Systems - Third Edition Solutions 2 | -------------------------------------------------------------------------------- /Chapter 30 - Solutions.md: -------------------------------------------------------------------------------- 1 | # Database Management Systems - Third Edition Solutions 2 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Database Management Systems - Third Edition Solutions 2 | 3 | 4 | ![](https://images-na.ssl-images-amazon.com/images/I/51ssbAeFyaL._SX383_BO1,204,203,200_.jpg) 5 | 6 | 7 | This repository is built to collect the answers of Database Management Systems Third Edition. 8 | 9 | 10 | ##### Contributing: 11 | If you find an incorrect or missing question/answer or even a typo and want to contribute please feel free to submit a PR. 12 | 13 | 14 | ##### Note: 15 | Some answers are taken from - http://pages.cs.wisc.edu/~dbbook/openAccess/thirdEdition/solutions/ans3ed-oddonly.pdf 16 | 17 | 18 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | Copyright 2018 Nivedita Gautam 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 4 | 5 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 6 | 7 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -------------------------------------------------------------------------------- /Chapter 6 - Solutions.md: -------------------------------------------------------------------------------- 1 | # Database Management Systems - Chapter 6 Solutions 2 | 3 | ### Exercise 6.1 4 | Briefly answer the following questions. 5 | 1. Explain the following terms: Cursor, Embedded SQL, JDBC, SQLJ, stored procedure. 6 | 2. What are the differences between JDBC and SQLJ? \Nhy do they both exist? 7 | 3. Explain the term stored procedure, and give examples why stored procedures are useful. 8 | #### `Answer` 9 | 10 | ### Exercise 6.2 11 | Explain how the following steps are performed in JDBC: 12 | 1. Connect to a data source. 13 | 2. Start, commit, and abort transactions. 14 | 3. Call a stored procedure. 15 | How are these steps performed in SQLJ? 16 | #### `Answer` 17 | 18 | ### Exercise 6.3 19 | Compare exception handling and handling of warnings ill embedded SQL, dynamic SQL, .IDBC, and SQL.I. 20 | #### `Answer` 21 | 22 | ### Exercise 6.4 23 | Answer the following questions. 24 | 1. Why do we need a precompiler to translate embedded SQL and SQL.J? Why do we not need a precompiler for .IDBC? 25 | 2. SQL.J and embedded SQL use variables in the host language to pass parameters to SQL queries, whereas .JDBC uses placeholders marked with a ''1'. Explain the difference, and why the different mechanisms are needed. 26 | #### `Answer` 27 | 28 | ### Exercise 6.5 29 | A dynamic web site generates HTML pages from information stored in a 30 | database. Whenever a page is requested, is it dynamically assembled from static data and data in a database, resulting in a database access. Connecting to the database is usually a time-consuming process, since resources need to be allocated, and the user needs to be authenticated. Therefore, connection pooling--setting up a pool of persistent database connections and then reusing them for different requests can significantly improve the performance 31 | of database-backed websites. Since servlets can keep information beyond single requests, we can create a connection pool, and allocate resources from it to new requests. 32 | 33 | Write a connection pool class that provides the following methods: 34 | - Create the pool with a specified number of open connections to the database system. 35 | - Obtain an open connection from the pool. 36 | - Release a connection to the pool. 37 | - Destroy the pool and close all connections. 38 | 39 | #### `Answer` 40 | -------------------------------------------------------------------------------- /Chapter 8 - Solutions.md: -------------------------------------------------------------------------------- 1 | # Database Management Systems - Chapter 8 Solutions 2 | 3 | ### Exercise 8.1 4 | Answer the following questions about data on external storage in a DBMS: 5 | 1. Why does a DBMS store data on external storage? 6 | 2. Why are I/O costs important in a DBMS? 7 | 3. What is a record id? Given a record's id, how many I/Os are needed to fetch it into 8 | main memory? 9 | 4. What is the role of the buffer manager in a DBMS? What is the role of the disk space 10 | manager? How do these layers interact with the file and access methods layer? 11 | 12 | #### `Answer` 13 | 14 | *** 15 | ### Exercise 8.2 Answer the following questions about files and indexes: 16 | 1. What operations arc supported by the file of records abstraction? 17 | 2. What is an index on a file of records? What is a search key for an index? Why do we 18 | need indexes? 19 | 3. What alternatives are available for the data entries in an index? 20 | 4. What is the difference between a primary index and a secondary index? What is a duplicate data entry in an index? Can a primary index contain duplicates? 21 | 5. What is the difference between a clustered index and an unclustered index? If an index contains data records as 'data entries,' can it be unclustered? 22 | 6. How many clustered indexes can you create on a file? Would you always create at least one clustered index for a file? 23 | 7. Consider Alternatives (1), (2) and (3) for 'data entries' in an index, as discussed in Section 8.2 . Are all of them suitable for secondary indexes? Explain. 24 | 25 | #### `Answer` 26 | 27 | *** 28 | ### Exercise 8.3 29 | Consider a relation stored as a randomly ordered file for which the only index is an unclustered index on a field called sal. If you want to retrieve all records with sal> 20, is using the index always the best alternative? Explain. 30 | #### `Answer` 31 | 32 | *** 33 | ### Exercise 8.4 34 | Consider the instance of the Students relation shown in Figure 8.6, sorted by age: For the purposes of this question, assume that these tuples are stored in a sorted file in the order shown; the first tuple is on page 1 the second tuple is also on page 1; and so on. Each page can store up to three data records; so the fourth tuple is on page 2. Explain what the data entries in each of the following indexes contain. If the order of entries is significant, say so and explain why. If such all index cannot be constructed, say so and explain why. 35 | 1. An unclustered index on age using Alternative (1). 36 | 2. An unclustered index on age using Alternative (2). 37 | 3. An unclustered index on age using Alternative (3). 38 | 4. A clustered index on age using Alternative (1). 39 | 5. A clustered index on age using Alternative (2). 40 | 6. A clustered index on age using Alternative (3). 41 | 7. An unclustered index on gpa using Alternative (1). 42 | 8. An unclustered index on gpa using Alternative (2). 43 | 9. An unclustered index on gpa using Alternative (3). 44 | 10. A clustered index on gpa using Alternative (1). 45 | 11. A clustered index on gpa using Alternative (2). 46 | 12. A clustered index on gpa using Alternative (3). 47 | 48 | #### `Answer` 49 | 50 | *** 51 | ### Exercise 8.5 52 | Explain the difference between Hash indexes and B+-tree indexes. In particular, discuss how equality and range searches work, using an example. 53 | #### `Answer` 54 | 55 | *** 56 | ### Exercise 8.6 57 | Fill in the I/O costs in Figure 8.7. 58 | #### `Answer` 59 | 60 | *** 61 | ### Exercise 8.7 62 | If you were about to create an index on a relation, what considerations would guide your choice? Discuss: 63 | 1. The choice of primary index. 64 | 2. Clustered versus unclustered indexes. 65 | 3. Hash versus tree indexes. 66 | 4. The use of a sorted file rather than a tree-based index. 67 | 5. Choice of search key for the index. What is a composite search key, and what considerations are made in choosing composite search keys? What are index-only plans, and what is the influence of potential index-only evaluation plans on the choice of search key 68 | for an index? 69 | 70 | #### `Answer` 71 | 72 | *** 73 | ### Exercise 8.8 74 | Consider a delete specified using an equality condition. For each of the five file organizations, what is the cost if no record qualifies? What is the cost if the condition is not on a key? 75 | #### `Answer` 76 | 77 | *** 78 | ### Exercise 8.9 79 | What main conclusions can you draw from the discussion of the five basic file organizations discussed in Section 8.4? Which of the five organizations would you choose for a file where the most frequent operations are as follows? 80 | 1. Search for records based on a range of field values. 81 | 2. Perform inserts and scans, where the order of records docs not matter. 82 | 3. Search for a record based on a particular field value. 83 | 84 | #### `Answer` 85 | 86 | *** 87 | ### Exercise 8.10 Consider the following relation: 88 | Emp( eid: integer, sal: integer l age: real, did: integer) 89 | There is a clustered index on cid and an llnclustered index on age. 90 | 1. How would you use the indexes to enforce the constraint that eid is a key? 91 | 2. Give an example of an update that is definitely speeded 1lJi because of the available 92 | indexes. (English description is sufficient.) 93 | 3. Give an example of an update that is definitely slowed down because of the indexes. 94 | (English description is sufficient.) 95 | 4. Can you give an example of an update that is neither speeded up nor slowed down by the indexes? 96 | 97 | #### `Answer` 98 | 99 | *** 100 | ### Exercise 8.11 101 | Consider the following relations: 102 | 103 | Emp( eid: integer, ename: varchar, sal: integer, age: integer, did: integer) 104 | Dept(did: integer, budget: integer, floor: integer, mgr_eid: integer) 105 | 106 | Salaries range from $10,000 to $100,000, ages vary from 20 to 80, each department has about five employees on average, there are 10 floors, and budgets vary from $10,000 to $1 million. You can assume uniform distributions of values. For each of the following queries, which of the listed index choices would you choose to speed up the query? If your database system does not consider index-only plans (i.e., data records are always retrieved even if enough information is available in the index entry), how would 107 | your answer change? Explain briefly. 108 | 1. Query: Print ename, age, and sal for all employees. 109 | (a) Clustered hash index on (ename, age, sal) fields of Emp. 110 | (b) Unclustered hash index on (ename, age, sal) fields of Emp. 111 | (c) Clustered B+ tree index on (ename, age, sal) fields of Emp. 112 | (d) Unclustered hash index on (eid, did) fields of Emp. 113 | (e) No index. 114 | 115 | 2. Query: Find the dids of departments that are on the 10th floor and have a budget of less than $15,000. 116 | (a) Clustered hash index on the floor field of Dept. 117 | (b) Unclustered hash index on the floor' field of Dept. 118 | (c) Clustered B+ tree index on (floor, budget) fields of Dept. 119 | (d) Clustered B+ tree index on the budget: field of Dept. 120 | (e) No index. 121 | 122 | #### `Answer` 123 | 124 | *** 125 | -------------------------------------------------------------------------------- /Chapter 9 - Solutions.md: -------------------------------------------------------------------------------- 1 | # Database Management Systems - Chapter 9 Solutions 2 | 3 | ### Exercise 9.1 4 | What is the most important difference behveen a disk and a tape? 5 | #### `Answer` 6 | 7 | *** 8 | ### Exercise 9.2 9 | Explain the terms seek time, rotational delay, and transfer time. 10 | #### `Answer` 11 | 12 | *** 13 | ### Exercise 9.3 14 | Both disks and main memory support direct access to any desired location (page). On average, main memory accesses are faster, of course. That is the other important 15 | difference (from the perspective of the time required to access a desired page)? 16 | #### `Answer` 17 | 18 | *** 19 | ### Exercise 9.4 20 | If you have a large file that is frequently scanned sequentially, explain how you would store the pages in the file on a disk. 21 | 22 | #### `Answer` 23 | 24 | *** 25 | ### Exercise 9.5 26 | Consider a disk with a sector size of 512 bytes, 2000 tracks per surface, 50 sectors per track, five double-sided platters, and average seek time of 10 msec. 27 | 1. What is the capacity of a track in bytes? What is the capacity of each surface? What is the capacity of the disk? 28 | 2. How many cylinders does the disk have? Give examples of valid block sizes. Is 256 bytes a valid block size? 2048? 51,200? 29 | 4. If the disk platters rotate at 5400 rpm (revolutions per minute), what is the maximum rotational delay? 30 | 5. If one track of data can be transferred per revolution, what is the transfer rate? 31 | #### `Answer` 32 | 33 | *** 34 | ### Exercise 9.6 35 | Consider again the disk specifications from Exercise 9.5 and suppose that a 36 | block size of 1024 bytes is chosen. Suppose that a file containing 100,000 records of 100 bytes 37 | each is to be stored on such a disk and that no record is allowed to span two blocks. 38 | 1. How many records fit onto a block? 39 | 2. How many blocks are required to store the entire file? If the file is arranged sequentially 40 | on disk, how many surfaces are needed? How many records of 100 bytes each can be stored using this disk? 41 | 4. If pages are stored sequentially on disk, with page 1 on block 1 of track 1, what page is stored on block 1 of track 1 on the next disk surface? How would your answer change if the disk were capable of reading and writing from all heads in parallel? 42 | 5. What time is required to read a file containing 100,000 records of 100 bytes each sequentially? Again, how would your answer change if the disk were capable of reading/writing from all heads in parallel (and the data was arranged optimally)? 43 | 6. What is the time required to read a file containing 100,000 records of 100 bytes each in a 44 | random order? To read a record, the block containing the record has to be fetched from 45 | disk. Assume that each block request incurs the average seek time and rotational delay. 46 | 47 | #### `Answer` 48 | 49 | *** 50 | ### Exercise 9.7 51 | Explain what the buffer manager must do to process a read request for a page. What happens if the requested page is in the pool but not pinned? 52 | 53 | #### `Answer` 54 | 55 | *** 56 | ### Exercise 9.8 57 | When does a buffer manager write a page to disk? 58 | 59 | #### `Answer` 60 | 61 | *** 62 | ### Exercise 9.9 63 | What does it mean to say that a page is pinned in the buffer pool? Who is responsible for pinning pages? Who is responsible for unpinning pages? 64 | 65 | #### `Answer` 66 | 67 | *** 68 | ### Exercise 9.10 69 | When a page in the buffer pool is modified, how does the DBMS ensure that this change is propagated to disk? (Explain the role of the buffer manager as well as the modifier of the page.) 70 | Exercise 9.11 What happens if a page is requested when all pages in the buffer pool are dirty? 71 | 72 | #### `Answer` 73 | 74 | *** 75 | ### Exercise 9.12 76 | What is sequential flooding of the buffer pool? 77 | 78 | #### `Answer` 79 | 80 | *** 81 | ### Exercise 9.13 82 | Name an important capability of a DBMS buffer manager that is not supported by a typical operating system's buffer manager. 83 | 84 | #### `Answer` 85 | 86 | *** 87 | ### Exercise 9.14 88 | Explain the term prefetching. Why is it important? 89 | 90 | #### `Answer` 91 | 92 | *** 93 | ### Exercise 9.15 94 | Modern disks often have their own main memory caches, typically about 1 MB, and use this to prefetch pages. The rationale for this technique is the empirical observation that, if a disk page is requested by some (not necessarily database!) application, 95 | 80% of the time the next page is requested as well. So the disk gambles by reading ahead. 96 | 1. Give a nontechnical reason that a DBMS may not want to rely on prefetching controlled 97 | by the disk. 98 | 2. Explain the impact on the disk's cache of several queries running concurrently, each 99 | scanning a different file. 100 | 3. Is this problem addressed by the DBMS buffer manager prefetching pages? Explain. 101 | 4. Modern disks support segmented caches, with about four to six segments, each of which is used to cache pages from a different file. Does this technique help, with respect to the preceding problem? Given this technique, does it matter whether the DBMS buffer manager also does prefetching? 102 | 103 | #### `Answer` 104 | 105 | *** 106 | ### Exercise 9.16 107 | Describe two possible record formats. What are the trade-offs between them? 108 | 109 | #### `Answer` 110 | 111 | *** 112 | ### Exercise 9.17 113 | Describe two possible page formats. What are the trade-offs between them? 114 | 115 | #### `Answer` 116 | 117 | *** 118 | ### Exercise 9.18 119 | Consider the page format for variable-length records that uses a slot directory. 120 | 1. One approach to managing the slot directory is to use a maximum size (i.e., a maximum number of slots) and allocate the directory array when the page is created. Discuss the pros and cons of this approach with respect to the approach discussed in the text. 121 | 2. Suggest a modification to this page format that would allow us to sort records (according to the value in some field) without moving records and without changing the record ids. Exercise 9.19 Consider the two internal organizations for heap files (using lists of pages and a directory of pages) discussed in the text. 122 | 1. Describe them briefly and explain the trade-offs. Which organization would you choose if records are variable in length? 123 | 2. Can you suggest a single page format to implement both internal file organizations? 124 | 125 | #### `Answer` 126 | 127 | *** 128 | ### Exercise 9.20 129 | Consider a list-based organization of the pages in a heap file in which two lists are maintained: a list of all pages in the file and a list of all pages with free space. In 130 | contrast, the list-based organization discussed in the text maintains a list of full pages and a 131 | list of pages with free space. 132 | 133 | 134 | 1. What are the trade-offs, if any'? Is one of them clearly superior? 135 | 2. For each of these organizations, describe a suitable page format. 136 | 137 | #### `Answer` 138 | 139 | *** 140 | ### Exercise 9.21 141 | Modern disk drives store more sectors on the outer tracks than the inner tracks. Since the rotation speed is constant, the sequential data transfer rate is also higher on 142 | the outer tracks. The seek time and rotational delay are unchanged. Given this information, 143 | explain good strategies for placing files with the following kinds of access patterns: 144 | 1. Frequent, random accesses to a small file (e.g., catalog relations). 145 | 2. Sequential scans of a large file (e.g., selection from a relation with no index). 146 | 3. Random accesses to a large file via an index (e.g., selection from a relation via the index). 147 | 4. Sequential scans of a small file. 148 | 149 | #### `Answer` 150 | 151 | *** 152 | ### Exercise 9.22 153 | Why do frames in the buffer pool have a pin count instead of a pin flag? 154 | 155 | #### `Answer` 156 | 157 | *** 158 | -------------------------------------------------------------------------------- /Chapter 16 - Solutions.md: -------------------------------------------------------------------------------- 1 | # Database Management Systems - Third Edition Solutions 2 | 3 | ### Exercise 16.1 4 | Give brief answers to the following questions: 5 | 1. What is a transaction? In what ways is it different from an ordinary program (in a 6 | language such as C)? 7 | 2. Define these terms: atomicity, consistency, isolation, durability, schedule, blind write, 8 | dirty read, unrepeatable read, serializable schedule, recoverable schedule, avoidsvcascadingaborts 9 | schedule. 10 | 3. Describe Strict 2PL. 11 | 4. What is the phantom problem? Can it occur in a database where the set of database 12 | objects is fixed and only the values of objects can be changed? 13 | 14 | #### `Answer` 15 | 16 | *** 17 | 18 | ### Exercise 16.2 19 | Consider the following actions taken by transaction T1 on database objects 20 | X and Y: 21 | R(X), W(X),R(Y), W(Y) 22 | 1. Give an example of another transaction T2 that, if run concurrently to transaction T 23 | without some form of concurrency control, could interfere with T1. 24 | 2. Explain how the use of Strict 2PL would prevent interference between the two transactions. 25 | 3. Strict 2PL is used in many database systems. Give two reasons for its popularity. 26 | 27 | #### `Answer` 28 | 29 | *** 30 | 31 | ### Exercise 16.3 32 | Consider a database with objects X and Y and assume that there are two 33 | transactions Tl and T2. Transaction T1 reads objects X and Y and then writes object X. 34 | Transaction T2 reads objects X and Y and then writes objects X and Y. 35 | 1. Give an example schedule with actions of transactions T1 and T2 on objects X and Y 36 | that results in a write-read conflict. 37 | 2. Give an example schedule with actions of transactions T1 and T2 on objects X and Y 38 | that results in a read-write conflict. 39 | 3. Give an example schedule with actions of transactions T1 and T2 on objects X and Y 40 | that results in a write-write conflict. 41 | 4. For each of the three schedules, show that Strict 2PL disallows the schedule. 42 | 43 | #### `Answer` 44 | 45 | 1. Write-read conflict (Reading uncommited data or Dirty Read) - 46 | T1 R(X) 47 | T1 R(Y) 48 | T1 W(X) 49 | T2 R(X) - Dirty Read 50 | ... 51 | 52 | 2. Read-Write Conflict (Unrepeatable reads) - 53 | T1 R(X) 54 | T1 R(Y) 55 | T2 R(X) 56 | T2 R(Y) 57 | T1 W(X) 58 | ... 59 | Now T2 will get unrepeatable read. 60 | 61 | 3. Write-Write conflict (Overwriitng uncommited data) - 62 | T1 R(X) 63 | T1 R(Y) 64 | T2 R(X) 65 | T1 W(X) - Step 4 66 | T2 R(Y) 67 | T2 W(X) - Write-Write conflict 68 | ... 69 | 70 | 4. Write-read conflict - T2 will not get a Shared lock on X, untill T1 commits. 71 | Read-Write Conflict - T1 will not get Exclusive lock on X untill T2 commits. 72 | Write-Write conflict - T1 will not get Exclusive lock on X in Step 4, unitll T2 commits. 73 | 74 | *** 75 | ### Exercise 16.4 76 | We call a transaction that only reads database object a read-only transaction, 77 | otherwise the transaction is called a read-write transaction. Give brief answers to the 78 | following questions: 79 | 1. What is lock thrashing and when does it occur? 80 | 2. What happens to the database system throughput if the number of read-write transactions 81 | is increased? 82 | 3. What happens to the datbase system throughput if the number of read-only transactions 83 | is increased? 84 | 4. Describe three ways of tuning your system to increase transaction throughput. 85 | 86 | #### `Answer` 87 | 88 | 1. Lock thrashing occurs when a large number of active transactions get blocked while competing for locks. 89 | 2. Number of transactions waiting for locks , increases. 90 | 3. No impact, multiple transactions can acquire Shared locks simultaneously. 91 | 4. Throughput can be increased by - 92 | - Lock the smallest size objects possible. 93 | - Reduce the time for which a transaction holds locks. 94 | - Reduce number of hotspots (Objects that are frequently accessed and modified). 95 | 96 | 97 | *** 98 | 99 | ### Exercise 16.5 100 | Suppose that a DBMS recognizes increment, which increments an integervalued 101 | object by 1, and decrement as actions, in addition to reads and writes. A transaction 102 | that increments an object need not know the value of the object; increment and decrement 103 | are versions of blind writes. In addition to shared and exclusive locks, two special locks are 104 | supported: An object must be locked in I mode before incrementing it and locked in D mode 105 | before decrementing it. An I lock is compatible with another I or D lock on the same object, 106 | but not with S and X locks. 107 | 1. Illustrate how the use of I and D locks can increase concurrency. (Show a schedule 108 | allowed by Strict 2PL that only uses S and X locks. Explain how the use of I and D 109 | locks can allow more actions to be interleaved, while continuing to follow Strict 2PL.) 110 | 2. Informally explain how Strict 2PL guarantees serializability even in the presence of I 111 | and D locks. (Identify which pairs of actions conflict, in the sense that their relative 112 | order can affect the result, and show that the use of 5, X, I, and D locks according 113 | to Strict 2PL orders all conflicting pairs of actions to be the same as the order in some 114 | serial schedule.) 115 | 116 | #### `Answer` 117 | 118 | 1. Take the following two transactions as example: 119 | T1: Increment A, Decrement B, Read C; 120 | T2: Increment B, Decrement A, Read C 121 | If using only strict 2PL, all actions are versions of blind writes, they have to obtain 122 | exclusive locks on objects. Following strict 2PL, T1 gets an exclusive lock on A, 123 | if T2 now gets an exclusive lock on B, there will be a deadlock. Even if T1 is fast 124 | enough to have grabbed an exclusive lock on B first, T2 will now be blocked until 125 | T1 finishes. This has little concurrency. If I and D locks are used, since I and 126 | D are compatible, T1 obtains an I-Lock on A, and a D-Lock on B; T2 can still 127 | obtain an I-Lock on B, a D-Lock on A; both transactions can be interleaved to 128 | allow maximum concurrency. 129 | 2. The pairs of actions which conflicts are: 130 | RW, WW, WR, IR, IW, DR, DW 131 | We know that strict 2PL orders the first 3 conflicts pairs of actions to be the same 132 | as the order in some serial schedule. We can also show that even in the presence 133 | of I and D locks, strict 2PL also orders the latter 4 pairs of actions to be the 134 | same as the order in some serial schedule. Think of an I (or D)lock under these 135 | circumstances as an exclusive lock, since an I(D) lock is not compatible with S 136 | and X locks anyway (ie. can’t get a S or X lock if another transaction has an I or 137 | D lock). So serializability is guaranteed. 138 | 139 | *** 140 | 141 | ### Exercise 16.6 142 | Answer the following questions: SQL supports four isolation-levels and t.wo 143 | access-modes, for a total of eight combinations of isolation-level and access-mode. Each 144 | combination impiicitly defines a class of transactions; the following questions refer to these 145 | eight classes: 146 | 1. Consider the four SQL isolation levels. Describe which of the plHmomena can occur at 147 | each of these isolation levels: dirty read, unrepeatable read, phantom problem. 148 | 2. For each of the four isolation levels, give examples of transactions that could be run 149 | safely at that level. 150 | :.3. Why does the access mode of a transaction matter? 151 | 152 | #### `Answer` 153 | 154 | *** 155 | 156 | ### Exercise 16.7 157 | Consider the university enrollment database schema: 158 | Student(snurn: integer, snarne: string, majoT: string, level: string, age: integer) 159 | Class(name: string, meets_at: time, Toom: string, fid"' integer) 160 | Enrolled(snum: integer, cname: string) 161 | Faculty(fid: integer, fname: string, deptid: integer) 162 | The meaning of these relations is straightforward; for example, Enrolled has one record per 163 | student-class pair such that the student is enrolled in the class. 164 | For each of the following transactions, state the SQL isolation level you would use and explain 165 | why you chose it. 166 | 1. Enroll a student identified by her snum into the class named 'Introduction to Database 167 | Systems'. 168 | 2. Change enrollment for a student identified by her snum from one class to another class, 169 | 3. Assign a new faculty member identified by his fid to the class with the least number of 170 | students. 171 | 4. For each class, show the number of students enrolled in the class. 172 | 173 | #### `Answer` 174 | 175 | *** 176 | 177 | ### Exercise 16.8 178 | Consider the following schema: 179 | Suppliers(sid: integer, sname: string, addTess: string) 180 | Parts(pid: integer, pname: string, coloT: string) 181 | Catalog(sid: integer, pid: integer, cost: real) 182 | The Catalog relation lists the prices charged for parts by Suppliers. 183 | For each of the following transactions, state the SQL isolation level that you would use and 184 | explain why you chose it. 185 | 1. A transaction that adds a new part to a supplier's catalog. 186 | 2. A transaction that increases the price that a supplier charges for a part. 187 | 3. A transaction that determines the total number of items for a given supplier. 188 | 4. A transaction that shows, for each part, the supplier that supplies the part at the lowest 189 | price. 190 | 191 | #### `Answer` 192 | 193 | *** 194 | 195 | ### Exercise 16.9 196 | Consider a database with the following schema: 197 | Suppliers(sid: integer, sname: string, addTess: string) 198 | Parts(pid: integer, pname: string, coloT: string) 199 | Catalog( sid: integer, pid: integer, cost: real) 200 | The Catalog relation lists the prices charged for parts by Suppliers. 201 | Consider three transactions 1'1,1'2, and 1'3; 1'1 always h8.o.'3 SQL isolation level SERIALIZABLE. 202 | We first run 1'1 concurrently with 1'2 and then we run 1'1 concurrently with 1'2 but we change 203 | the isolation level of 1'2 as specified below. Give a database instance and SQL statements for 204 | 1'1 and 1'2 such that result of running 1'2 with the first SQL isolation level is different from 205 | running 1'2 with the second SQL isolation level. Also specify the common schedule of 1'1 and 206 | 1'2 and explain why the results are different. 207 | 1. SERIALIZABLE versus REPEATABLE READ. 208 | 2. REPEATABLE READ versus READ COMMITTED. 209 | 3. READ COMMITTED versus READ UNCOMMITTED 210 | 211 | #### `Answer` 212 | 213 | *** 214 | -------------------------------------------------------------------------------- /Chapter 1 - Solutions.md: -------------------------------------------------------------------------------- 1 | # Database Management Systems - Chapter 1 Solutions 2 | 3 | ### Exercise 1.1 4 | 5 | Why would you choose a database system instead of simply storing data in 6 | operating system files? When would it make sense not to use a database system? 7 | 8 | ### `Answer` 9 | A database is an integrated collection of data, usually so large that it 10 | has to be stored on secondary storage devices such as disks or tapes. This data can 11 | be maintained as a collection of operating system files, or stored in a DBMS (database 12 | management system). The advantages of using a DBMS are: 13 | - Data independence and efficient access. Database application programs are independent 14 | of the details of data representation and storage. The conceptual and 15 | external schemas provide independence from physical storage decisions and logical 16 | design decisions respectively. In addition, a DBMS provides efficient storage and 17 | retrieval mechanisms, including support for very large files, index structures and 18 | query optimization. 19 | - Reduced application development time. Since the DBMS provides several important 20 | functions required by applications, such as concurrency control and crash 21 | recovery, high level query facilities, etc., only application-specific code needs to 22 | be written. Even this is facilitated by suites of application development tools 23 | available from vendors for many database management systems. 24 | - Data integrity and security. The view mechanism and the authorization facilities 25 | of a DBMS provide a powerful access control mechanism. Further, updates to the 26 | data that violate the semantics of the data can be detected and rejected by the 27 | DBMS if users specify the appropriate integrity constraints. 28 | - Data administration. By providing a common umbrella for a large collection of 29 | data that is shared by several users, a DBMS facilitates maintenance and data 30 | administration tasks. A good DBA can effectively shield end-users from the chores 31 | of fine-tuning the data representation, periodic back-ups etc. 32 | - Concurrent access and crash recovery. A DBMS supports the notion of a transaction, 33 | which is conceptually a single user’s sequential program. Users can write 34 | transactions as if their programs were running in isolation against the database. 35 | The DBMS executes the actions of transactions in an interleaved fashion to obtain 36 | good performance, but schedules them in such a way as to ensure that conflicting 37 | operations are not permitted to proceed concurrently. Further, the DBMS maintains 38 | a continuous log of the changes to the data, and if there is a system crash, 39 | it can restore the database to a transaction-consistent state. That is, the actions 40 | of incomplete transactions are undone, so that the database state reflects only the 41 | actions of completed transactions. Thus, if each complete transaction, executing 42 | alone, maintains the consistency criteria, then the database state after recovery 43 | from a crash is consistent. 44 | 45 | If these advantages are not important for the application at hand, using a collection of 46 | files may be a better solution because of the increased cost and overhead of purchasing 47 | and maintaining a DBMS. 48 | 49 | *** 50 | ### Exercise 1.2 51 | What is logical data independence and why is it important? 52 | 53 | ### `Answer` 54 | 55 | *** 56 | ### Exercise 1.3 57 | Explain the difference between logical and physical data independence. 58 | 59 | ### `Answer` 60 | 61 | Logical data independence means that users are shielded from changes 62 | in the logical structure of the data, while physical data independence insulates users 63 | from changes in the physical storage of the data. We saw an example of logical data 64 | independence in the answer to Exercise 1.2. Consider the Students relation from that 65 | example (and now assume that it is not replaced by the two smaller relations). We 66 | could choose to store Students tuples in a heap file, with a clustered index on the 67 | sname field. Alternatively, we could choose to store it with an index on the gpa field, 68 | or to create indexes on both fields, or to store it as a file sorted by gpa. These storage 69 | alternatives are not visible to users, except in terms of improved performance, since 70 | they simply see a relation as a set of tuples. This is what is meant by physical data 71 | independence 72 | 73 | *** 74 | ### Exercise 1.4 75 | Explain the difference between external, internal, and conceptual schemas. How are these different schema layers related to the concepts of logical and physical data independence? 76 | 77 | ### `Answer` 78 | 79 | *** 80 | ### Exercise 1.5 81 | What are the responsibilities of a DBA? If we assume that the DBA is never interested in running his or her own queries, does the DBA still need to understand query optimization? Why? 82 | 83 | ### `Answer` 84 | 85 | The DBA is responsible for: 86 | - Designing the logical and physical schemas, as well as widely-used portions of the 87 | external schema. 88 | - Security and authorization. 89 | - Data availability and recovery from failures. 90 | - Database tuning: The DBA is responsible for evolving the database, in particular 91 | the conceptual and physical schemas, to ensure adequate performance as user 92 | requirements change. 93 | 94 | A DBA needs to understand query optimization even if s/he is not interested in running 95 | his or her own queries because some of these responsibilities (database design 96 | and tuning) are related to query optimization. Unless the DBA understands the performance 97 | needs of widely used queries, and how the DBMS will optimize and execute 98 | these queries, good design and tuning decisions cannot be made. 99 | 100 | *** 101 | ### Exercise 1.6 102 | 103 | Scrooge McNugget wants to store information (names, addresses, descriptions of embarrassing moments, etc.) about the many ducks on his payroll. Not surprisingly, the volume of data compels him to buy a database system. To save money, he wants to buy one with the fewest possible features, and he plans to run it as a stand-alone application on his PC clone. Of course, Scrooge does not plan to share his list with anyone. Indicate which of the following DBMS features Scrooge should pay for; in each case, also indicate why Scrooge should (or should not) pay for that feature in the system he buys. 104 | 1. A security facility. 105 | 2. Concurrency control. 106 | 3. Crash recovery. 107 | 4. A view mechanism. 108 | 5. A query language. 109 | 110 | 111 | ### `Answer` 112 | 113 | *** 114 | ### Exercise 1.7 115 | Which of the following plays an important role in representing information about the real world in a database'? Explain briefly. 116 | 1. The data definition language. 117 | 2. The data manipulation language. 118 | 3. The buffer manager. 119 | 4. The data model. 120 | 121 | 122 | ### `Answer` 123 | 124 | Let us discuss the choices in turn. 125 | - The data definition language is important in representing information because it 126 | is used to describe external and logical schemas. 127 | - The data manipulation language is used to access and update data; it is not 128 | important for representing the data. (Of course, the data manipulation language 129 | must be aware of how data is represented, and reflects this in the constructs that 130 | it supports.) 131 | - The buffer manager is not very important for representation because it brings 132 | arbitrary disk pages into main memory, independent of any data representation. 133 | - The data model is fundamental to representing information. The data model 134 | determines what data representation mechanisms are supported by the DBMS. 135 | - The data definition language is just the specific set of language constructs available 136 | to describe an actual application’s data in terms of the data model. 137 | 138 | *** 139 | ### Exercise 1.8 140 | Describe the structure of a DBMS. If your operating system is upgraded to 141 | support some new functions on as files (e.g., the ability to force some sequence of bytes to 142 | disk), which layer(s) of the DBMS would you have to rewrite to take advantage of these new 143 | functions? 144 | 145 | 146 | ### `Answer` 147 | 148 | *** 149 | ### Exercise 1.9 150 | Answer the following questions: 151 | 1. What is a transaction? 152 | 2. Why does a DBMS interleave the actions of different transactions instead of executing 153 | transactions one after the other? 154 | 3. What must a user guarantee with respect to a transaction and database consistency? 155 | What should a DBMS guarantee with respect to concurrent execution of several transactions 156 | and database consistency'? 157 | 4. Explain the strict two-phase locking protocol. 158 | 5. What is the WAL property, and why is it important? 159 | 160 | ### `Answer` 161 | 162 | 1. A transaction is any one execution of a user program in a DBMS. This is the basic 163 | unit of change in a DBMS. 164 | 2. A DBMS is typically shared among many users. Transactions from these users 165 | can be interleaved to improve the execution time of users’ queries. By interleaving 166 | queries, users do not have to wait for other user’s transactions to complete 167 | fully before their own transaction begins. Without interleaving, if user A begins 168 | a transaction that will take 10 seconds to complete, and user B wants to begin 169 | a transaction, user B would have to wait an additional 10 seconds for user 170 | A’s transaction to complete before the database would begin processing user B’s 171 | request. 172 | 3. A user must guarantee that his or her transaction does not corrupt data or insert 173 | nonsense in the database. For example, in a banking database, a user must guarantee 174 | that a cash withdraw transaction accurately models the amount a person 175 | removes from his or her account. A database application would be worthless if 176 | a person removed 20 dollars from an ATM but the transaction set their balance 177 | to zero! A DBMS must guarantee that transactions are executed fully and independently 178 | of other transactions. An essential property of a DBMS is that a 179 | transaction should execute atomically, or as if it is the only transaction running. 180 | Also, transactions will either complete fully, or will be aborted and the database 181 | returned to it’s initial state. This ensures that the database remains consistent. 182 | 4. Strict two-phase locking uses shared and exclusive locks to protect data. A transaction 183 | must hold all the required locks before executing, and does not release any 184 | lock until the transaction has completely finished. 185 | 5. The WAL property affects the logging strategy in a DBMS. The WAL, WriteAhead 186 | Log, property states that each write action must be recorded in the log 187 | (on disk) before the corresponding change is reflected in the database itself. This 188 | protects the database from system crashes that happen during a transaction’s 189 | execution. By recording the change in a log before the change is truly made, the 190 | database knows to undo the changes to recover from a system crash. Otherwise, 191 | if the system crashes just after making the change in the database but before 192 | the database logs the change, then the database would not be able to detect his 193 | change during crash recovery. 194 | 195 | *** 196 | -------------------------------------------------------------------------------- /Chapter 2 - Solutions.md: -------------------------------------------------------------------------------- 1 | # Database Management Systems - Chapter 2 Solutions 2 | 3 | ### Exercise 2.1 4 | Explain the following terms briefly: attribute, domain, entity, relationship, entity set, relationship set, one-to-many relationship, many-to-many relationship, participation constraint, overlap constraint, covering constraint, weak entity set, aggregation, and role 5 | indicator. 6 | 7 | ### `Answer` 8 | 9 | - Attribute - a property or description of an entity. A toy department employee 10 | entity could have attributes describing the employee’s name, salary, and years of 11 | service. 12 | - Domain - a set of possible values for an attribute. 13 | - Entity - an object in the real world that is distinguishable from other objects such 14 | as the green dragon toy. 15 | - Relationship - an association among two or more entities. 16 | - Entity set - a collection of similar entities such as all of the toys in the toy department. 17 | - Relationship set - a collection of similar relationships 18 | - One-to-many relationship - a key constraint that indicates that one entity can be 19 | associated with many of another entity. An example of a one-to-many relationship 20 | is when an employee can work for only one department, and a department can 21 | have many employees. 22 | - Many-to-many relationship - a key constraint that indicates that many of one 23 | entity can be associated with many of another entity. An example of a manyto-many 24 | relationship is employees and their hobbies: a person can have many 25 | different hobbies, and many people can have the same hobby. 26 | 27 | *** 28 | 29 | 30 | ### Exercise 2.2 31 | A university database contains information about professors (identified by social security number, or SSN) and courses (identified by course id). Professors teach courses; each of the following situations concerns the Teaches relationship set. For each situation, draw an ER diagram that describes it (assuming no further constraints hold). 32 | 1. Professors can teach the same course in several semesters, and each offering must be recorded. 33 | 2. Professors can teach the same course in several semesters, and only the most recent such offering needs to be recorded. (Assume this condition applies in all subsequent questions. ) 34 | 3. Every professor must teach some course. 35 | 4. Every professor teaches exactly one course (no more, no less). 36 | 5. Every professor teaches exactly one course (no more, no less), and every course must be taught by some professor. 37 | 6. Now suppose that certain courses can be taught by a team of professors jointly, but it is possible that no one professor in a team can teach the course. Model this situation, introducing additional entity sets and relationship sets if necessary. 38 | 39 | 40 | ### `Answer` 41 | 42 | *** 43 | 44 | 45 | ### Exercise 2.3 46 | Consider the following information about a university database: 47 | 1. Professors have an SSN, a name, an age, a rank, and a research specialty. 48 | 2. Projects have a project number, a sponsor name (e.g., NSF), a starting date, an ending date, and a budget. 49 | 3. Graduate students have an SSN, a name, an age, and a degree program (e.g., M.S. or Ph.D.). 50 | 4. Each project is managed by one professor (known as the project's principal investigator). 51 | 5. Each project is worked on by one or more professors (known as the project's co-investigators). 52 | 6. Professors can manage and/or work on multiple projects. 53 | 7. Each project is worked on by one or more graduate students (known as the project's research assistants). 54 | 8. When graduate students >'lark on a project, a professor must supervise their work on the project. Graduate students can work on multiple projects, in which case they will have a (potentially different) supervisor for each one. 55 | 9. Departments have a department number, a department name, and a main office. 56 | 10. Departments have a professor (known as the chairman) who runs the department. 57 | 11. Professors work in one or more departments, and for each department that they work 58 | in, a time percentage is associated with their job. 59 | 12. Graduate students have one major department in which they are working on their degree. 60 | 13. Each graduate student has another, more senior graduate student (known as a student advisor) who advises him or her on what courses to take. 61 | 62 | Design and draw an ER diagram that captures the information about the university. Use only the basic ER model here; that is, entities, relationships, and attributes. Be sure to indicate any key and participation constraints. 63 | 64 | ### `Answer` 65 | 66 | *** 67 | ### Exercise 2.4 68 | A company database needs to store information about employees (identified by SSN, with salary and phone as attributes), departments (identified by DNA, with dname and budget as attributes), and children of employees (with name and age as attributes). Employees work in departments; each department is managed by an employee; a child must be identified uniquely by name when the parent (who is an employee; assume that only one parent works for the company) is known. We are not interested in information about a child once the parent leaves the company. 69 | 70 | Draw an ER diagram that captures this information. 71 | 72 | ### `Answer` 73 | 74 | *** 75 | 76 | 77 | ### Exercise 2.5 78 | Notown Records has decided to store information about musicians who perform on its albums (as well as other company data) in a database. The company has wisely chosen to hire you as a database designer (at your usual consulting fee of $2500/day). 79 | 1. Each musician that records at Notown has an SSN, a name, an address, and a phone number. Poorly paid musicians often share the same address, and no address has more than one phone. 80 | 2. Each instrument used in songs recorded at Notown has a name (e.g., guitar, synthesizer, flute) and a musical key (e.g., C, B-flat, E-flat). 81 | 3. Each album recorded on the Notown label has a title, a copyright date, a format (e.g., CD or MC), and an album identifier. 82 | 4. Each song recorded at Notown has a title and an author. 83 | 5. Each musician may play several instruments, and a given instrument may be played by several musicians. 84 | 6. Each album has a number of songs on it, but no song may appear on more than one album. 85 | 7. Each song is performed by one or more musicians, and a musician may perform a number of songs. 86 | 8. Each album has exactly one musician who acts as its producer. A musician may produce several albums, of course. 87 | 88 | Design a conceptual schema for Notown and draw an ER diagram for your schema. The preceding information describes the situation that the Notown database must model. Be sure to indicate all key and cardinality constraints and any assumptions you make. Identify any constraints you are unable to capture in the ER diagram and briefly explain why you could not express them. 89 | 90 | ### `Answer` 91 | 92 | *** 93 | ### Exercise 2.6 94 | Computer Sciences Department frequent fliers have been complaining to Dane County Airport officials about the poor organization at the airport. As a result, the officials decided that all information related to the airport should be organized using a DBMS, and you have been hired to design the database. Your first task is to organize the information about all the airplanes stationed and maintained at the airport. The relevant information is as follows: 95 | 1. Every airplane has a registration number, and each airplane is of a specific model. 96 | 2. The airport accommodates a number of airplane models, and each model is identified by a model number (e.g., DC-lO) and has a capacity and a weight. 97 | 3. A number of technicians work at the airport. You need to store the name, SSN, address, phone number, and salary of each technician. 98 | 4. Each technician is an expert on one or more plane model(s), and his or her expertise may overlap with that of other technicians. This information about technicians must also be recorded. 99 | 5. Traffic controllers must have an annual medical examination. For each traffic controller, you must store the date of the most recent exam. 100 | 6. All airport employees (including technicians) belong to a union. You must store the union membership number of each employee. You can assume that each employee is uniquely identified by a social security number. 101 | 7. The airport has a number of tests that are used periodically to ensure that airplanes are still airworthy. Each test has a Federal Aviation Administration (FAA) test number, a name, and a maximum possible score. 102 | 8. The FAA requires the airport to keep track of each time a given airplane is tested by a given technician using a given test. For each testing event, the information needed is the date, the number of hours the technician spent doing the test, and the score the airplane 103 | received on the test. 104 | 9. Draw an ER diagram for the airport database. Be sure to indicate the various attributes of each entity and relationship set; also specify the key and participation constraints for each relationship set. Specify any necessary overlap and covering constraints as well (in 105 | English). 106 | 10. The FAA passes a regulation that tests on a plane must be conducted by a technician who is an expert on that model. How would you express this constraint in the ER diagram? If you cannot express it, explain briefly. 107 | 108 | ### `Answer` 109 | 110 | *** 111 | 112 | ### Exercise 2.7 113 | The Prescriptions-R-X chain of pharmacies has offered to give you a free lifetime supply of medicine if you design its database. Given the rising cost of health care, you agree. Here's the information that you gather: 114 | 1. Patients are identified by an SSN, and their names, addresses, and ages must be recorded. 115 | 2. Doctors are identified by an SSN. For each doctor, the name, specialty, and years of experience must be recorded. 116 | 3. Each pharmaceutical company is identified by name and has a phone number. 117 | 4. For each drug, the trade name and formula must be recorded. Each drug is sold by a given pharmaceutical company, and the trade name identifies a drug uniquely from among the products of that company. If a pharmaceutical company is deleted, you need not keep track of its products any longer. 118 | 5. Each pharmacy has a name, address, and phone number. 119 | 6. Every patient has a primary physician. Every doctor has at least one patient. 120 | 7. Each pharmacy sells several drugs and has a price for each. A drug could be sold at several pharmacies, and the price could vary from one pharmacy to another. 121 | 8. Doctors prescribe drugs for patients. A doctor could prescribe one or more drugs for several patients, and a patient could obtain prescriptions from several doctors. Each prescription has a date and a quantity associated with it. You can assume that, if a doctor prescribes the same drug for the same patient more than once, only the last such prescription needs to be stored. 122 | 9. Pharmaceutical companies have long-term contracts with pharmacies. A pharmaceutical company can contract with several pharmacies, and a pharmacy can contract with several pharmaceutical companies. For each contract, you have to store a start date, an end date, and the text of the contract. 123 | 10. Pharmacies appoint a supervisor for each contract. There must always be a supervisor 124 | for each contract, but the contract supervisor can change over the lifetime of the contract. 125 | 126 | Questions - 127 | - Draw an ER diagram that captures the preceding information. Identify any constraints not captured by the ER diagram. 128 | - How would your design change if each drug must be sold at a fixed price by all pharmacies? 129 | - How would your design change if the design requirements change as follows: If a doctor prescribes the same drug for the same patient more than once, several such prescriptions may have to be stored. 130 | 131 | ### `Answer` 132 | 133 | *** 134 | ### Exercise 2.8 135 | Although you always wanted to be an artist, you ended up being an expert on databases because you love to cook data and you somehow confused database with data baste. Your old love is still there, however, so you set up a database company, ArtBase, that builds a 136 | product for art galleries. The core of this product is a database with a schema that captures all the information that galleries need to maintain. Galleries keep information about artists, their names (which are unique), birthplaces, age, and style of art. For each piece of artwork, the artist, the year it was made, its unique title, its type of art (e.g., painting, lithograph, sculpture, photograph), and its price must be stored. Pieces of artwork are also classified into 137 | groups of various kinds, for example, portraits, still lifes, works by Picasso, or works of the 19th century; a given piece may belong to more than one group. Each group is identified by a name (like those just given) that describes the group. Finally, galleries keep information 138 | about customers. For each customer, galleries keep that person's unique name, address, total amount of dollars spent in the gallery (very important!), and the artists and groups of art that the customer tends to like. 139 | Draw the ER diagram for the database. 140 | 141 | ### `Answer` 142 | 143 | *** 144 | ### Exercise 2.9 145 | Answer the following questions. 146 | - Explain the following terms briefly: UML, use case diagrams, statechart diagrams, class diagrams, database diagrams, component diagrams, and deployment diagrams. 147 | - Explain the relationship between ER diagrams and UML. 148 | ### `Answer` 149 | 150 | *** 151 | -------------------------------------------------------------------------------- /Chapter 5 - Solutions.md: -------------------------------------------------------------------------------- 1 | # Database Management Systems - Chapter 5 Solutions 2 | 3 | Online material is available for all exercises in this chapter on the book's webpage at - 4 | http://www.cs.wisc.edu/-dbbook 5 | This includes scripts to create tables for each exercise for use with Oracle, IBM DB2, Microsoft SQL Server, and MySQL. 6 | ### Exercise 5.1 7 | Consider the following relations: 8 | Student (snum: integer, sname: string, major: string, level: string, age: integer) 9 | Class( name: string, meets_at: time, room: string, fid: integer) 10 | Enrolled(snum: integer, cname: string) 11 | Faculty (fid: integer, fnarne: string, deptid: integer) 12 | 13 | The meaning of these relations is straightforward; for example, Enrolled has one record per student-class pair such that the student is enrolled in the class. 14 | 15 | Write the following queries in SQL. No duplicates should be printed in any of the answers. 16 | 1. Find the nari1es of all Juniors (level = JR) who are enrolled in a class taught by 1. Teach. 17 | 2. Find the age of the oldest student who is either a History major or enrolled in a course 18 | taught by I. Teach. 19 | 3. Find the names of all classes that either meet in room R128 or have five or more students 20 | enrolled. 21 | 4. Find the names of all students who are enrolled in two classes that meet at the same time. 22 | 5. Find the names of faculty members who teach in every room in which some class is taught. 23 | 6. Find the names of faculty members for whom the combined enrollment of the courses that they teach is less than five. 24 | 7. Print the level and the average age of students for that level, for each level. 25 | 8. Print the level and the average age of students for that level, for all levels except JR. 26 | 9. For each faculty member that has taught classes only in room R128, print the faculty member's name and the total number of classes she or he has taught. 27 | 10. Find the names of students enrolled in the maximum number of classes. 28 | 11. Find the names of students not enrolled in any class. 29 | 12. For each age value that appears in Students, find the level value that appears most often. For example, if there are more FR level students aged 18 than SR, JR, or SO students aged 18, you should print the pair (18, FR). 30 | 31 | #### `Answer` 32 | 33 | *** 34 | ### Exercise 5.2 35 | Consider the following schema: 36 | Suppliers( sid: integer, sname: string, address: string) 37 | Parts(pid: integer, pname: string, color: string) 38 | Catalog( sid: integer, pid: integer, cost: real) 39 | The Catalog relation lists the prices charged for parts by Suppliers. 40 | 41 | Write the following queries in SQL: 42 | 1. Find the pnames of parts for which there is some supplier. 43 | 2. Find the snames of suppliers who supply every part. 44 | 3. Find the snames of suppliers who supply every red part. 45 | 4. Find the pnames of parts supplied by Acme Widget Suppliers and no one else. 46 | 5. Find the sids of suppliers who charge more for some part than the average cost of that part (averaged over all the suppliers who supply that part). 47 | 6. For each part, find the sname of the supplier who charges the most for that part. 48 | 7. Find the sids of suppliers who supply only red parts. 49 | 8. Find the sids of suppliers who supply a red part and a green part. 50 | 9. Find the sids of suppliers who supply a red part or a green part. 51 | 10. For every supplier that only supplies green parts, print the name of the supplier and the total number of parts that she supplies. 52 | 11. For every supplier that supplies a green part and a reel part, print the name and price of the most expensive part that she supplies. 53 | 54 | #### `Answer` 55 | 56 | *** 57 | ### Exercise 5.3 58 | The following relations keep track of airline flight information: 59 | Flights(.flno: integer, from: string, to: string, distance: integer, 60 | departs: time, arrives: time, price: integer) 61 | Aircraft( aid: integer, aname: string, cruisingrange: integer) 62 | Certified( eid: integer, aid: integer) 63 | Employees( eid: integer I ename: string, salary: integer) 64 | 65 | Note that the Employees relation describes pilots and other kinds of employees as well; every pilot is certified for some aircraft, and only pilots are certified to fly. Write each of the following queries in SQL. (Additional queries using the same schema are listed in the exercises for Chapter 4) 66 | 67 | 1. Find the names of aircraft such that all pilots certified to operate them earn more than $80,000. 68 | 2. For each pilot who is certified for more than three aircraft, find the eid and the maximum cruisingrange of the aircraft for which she or he is certified. 69 | 3. Find the names of pilots whose salary is less than the price of the cheapest route from Los Angeles to Honolulu. 70 | 4. For all aircraft with cruisingrange over 1000 miles, find the name of the aircraft and the average salary of all pilots certified for this aircraft. 71 | 5. Find the names of pilots certified for some Boeing aircraft. 72 | 6. Find the aids of all aircraft that can be used on routes from Los Angeles to Chicago. 73 | 7. Identify the routes that can be piloted by every pilot who makes more than $100,000. 74 | 8. Print the enames of pilots who can operate planes with cruisingrange greater than 3000 miles but are not certified on any Boeing aircraft. 75 | 9. A customer wants to travel from Madison to New York with no more than two changes of flight. List the choice of departure times from Madison if the customer wants to arrive in New York by 6 p.m. 76 | 10. Compute the difference between the average salary of a pilot and the average salary of all employees (including pilots). 77 | 11. Print the name and salary of every nonpilot whose salary is more than the average salary for pilots. 78 | 12. Print the names of employees who are certified only on aircrafts with cruising range longer than 1000 miles. 79 | 13. Print the names of employees who are certified only on aircrafts with cruising range longer than 1000 miles, but on at least two such aircrafts. 80 | 14. Print the names of employees who are certified only on aircrafts with cruising range longer than 1000 miles and who are certified on some Boeing aircraft. 81 | 82 | #### `Answer` 83 | 84 | *** 85 | ### Exercise 5.4 86 | Consider the following relational schema. An employee can work in more than one department; the pct_time field of the Works relation shows the percentage of time that a given employee works in a given department. 87 | 88 | Emp(eid: integer, ename: string, age: integer, salary: real) 89 | Works(eid: integer, did: integer, pct_time: integer) 90 | Dept(did.· integer, budget: real, managerid: integer) 91 | 92 | Write the following queries in SQL: 93 | 1. Print the names and ages of each employee who works in both the Hardware department 94 | and the Software department. 95 | 2. For each department with more than 20 full-time-equivalent employees (i.e., where the part-time and full-time employees add up to at least that many full-time employees), print the did together with the number of employees that work in that department. 96 | 3. Print the name of each employee whose salary exceeds the budget of all of the departments that he or she works in. 97 | 4. Find the managerids of managers who manage only departments with budgets greater than $1 million. 98 | 5. Find the enames of managers who manage the departments with the largest budgets. 99 | 6. If a manager manages more than one department, he or she controls the sum of all the budgets for those departments. Find the managerids of managers who control more than $5 million. 100 | 7. Find the managerids of managers who control the largest amounts. 101 | 8. Find the enames of managers who manage only departments with budgets larger than $1 million, but at least one department with budget less than $5 million. 102 | 103 | #### `Answer` 104 | 105 | *** 106 | ### Exercise 5.5 107 | Consider the instance of the Sailors relation shown in Figure 5.22. 108 | 1. Write SQL queries to compute the average rating, using AVG; the sum of the ratings, using SUM; and the number of ratings, using COUNT. 109 | 2. If you divide the sum just computed by the count, would the result be the same as the average? How would your answer change if these steps were carried out with respect to the age field instead of rating? 110 | 3. Consider the following query: Find the names of sailors with a higher rating than all sailors with age < 21. The following two SQL queries attempt to obtain the answer to this question. Do they both compute the result? If not, explain why. Under what conditions would they compute the same result? 111 | 112 | SELECT S.sname 113 | FROM Sailors S 114 | WHERE NOT EXISTS ( SELECT * 115 | FROM Sailors S2 116 | WHERE S2.age < 21 117 | AND S.rating <= S2.rating ) 118 | 119 | SELECT * 120 | FROM Sailors S 121 | WHERE S.rating > ANY 122 | SELECT S2.rating 123 | FROM Sailors S2 124 | WHERE S2.age < 21 125 | 126 | 127 | 4. Consider the instance of Sailors shown in Figure 5.22. Let us define instance Sl of Sailors to consist of the first two tuples, instance S2 to be the last two tuples, and S to be the given instance. 128 | 129 | (a) Show the left outer join of S with itself, with the join condition being sid=sid. 130 | (b) Show the right outer join of S ,vith itself, with the join condition being sid=sid. 131 | (c) Show the full outer join of S with itself, with the join condition being Sid=sid. 132 | (d) Show the left outer join of Sl with S2, with the join condition being sid=sid. 133 | (e) Show the right outer join of Sl with S2, with the join condition being sid=sid. 134 | (f) Show the full outer join of 81 with S2, with the join condition being sid=sid. 135 | 136 | #### `Answer` 137 | 138 | *** 139 | ### Exercise 5.6 140 | Answer the following questions: 141 | 1. Explain the term impedance mismatch in the context of embedding SQL commands in a host language such as C. 142 | 2. How can the value of a host language variable be passed to an embedded SQL command? 143 | 3. Explain the WHENEVER command's use in error and exception handling. 144 | 4. Explain the need for cursors. 145 | 5. Give an example of a situation that calls for the use of embedded SQL; that is, interactive use of SQL commands is not enough, and some host language capabilities are needed. 146 | 6. Write a C program with embedded SQL commands to address your example in the previous answer. 147 | 7. Write a C program with embedded SQL commands to find the standard deviation of sailors' ages. 148 | 8. Extend the previous program to find all sailors whose age is within one standard deviation of the average age of all sailors. 149 | 9. Explain how you would write a C program to compute the transitive closure of a graph, represented as an 8QL relation Edges(jrom, to), using embedded SQL commands. (You need not write the program, just explain the main points to be dealt with.) 150 | 10. Explain the following terms with respect to cursors: 'updatability, sensitivity, and scrollability. 151 | 11. Define a cursor on the Sailors relation that is updatable, scrollable, and returns answers sorted by age. Which fields of Sailors can such a cursor not update? Why? 152 | 12. Give an example of a situation that calls for dynamic 8QL; that is, even embedded SQL is not sufficient. 153 | 154 | #### `Answer` 155 | 156 | *** 157 | ### Exercise 5.7 158 | Consider the following relational schema and briefly answer the questions that follow: 159 | Emp (eid: integer, cname: string, age: integer, salary: real) 160 | Works (eid: integer, did: integer, pet-time: integer) 161 | Dept (did.' integer, budget: real, managerid: integer) 162 | 1. Define a table constraint on Emp that will ensure that ever)' employee makes at least $10,000. 163 | 2. Define a table constraint on Dept that will ensure that all managers have age> 30. 164 | 3. Define an assertion on Dept that will ensure that all managers have age> 30. Compare this assertion with the equivalent table constraint. Explain which is better. 165 | 4. Write SQL statements to delete all information about employees whose salaries exceed that of the manager of one or more departments that they work in. Be sure to ensure that all the relevant integrity constraints are satisfied after your updates. 166 | 167 | #### `Answer` 168 | 169 | *** 170 | ### Exercise 5.8 171 | Consider the following relations: 172 | 173 | Student (snum: integer, sname: string, major: string, level: string, age: integer) 174 | Class(name: string, meets_at: time, room: string, fid: integer) 175 | Enrolled(snurn: integer, cnarne: string) 176 | Faculty(fid: integer, fnarne: string, deptid: integer) 177 | 178 | The meaning of these relations is straightforward; for example, Enrolled has one record per student-class pair such that the student is enrolled in the class. 179 | 1. Write the SQL statements required to create these relations, including appropriate versions of all primary and foreign key integrity constraints. 180 | 2. Express each of the following integrity constraints in SQL unless it is implied by the primary and foreign key constraint; if so, explain how it is implied. If the constraint cannot be expressed in SQL, say so. For each constraint, state what operations (inserts, 181 | deletes, and updates on specific relations) must be monitored to enforce the constraint. 182 | 1. Every class has a minimum enrollment of 5 students and a maximum enrollment of 30 students. 183 | 2. At least one dass meets in each room. 184 | 3. Every faculty member must teach at least two courses. 185 | 4. Only faculty in the department with deptid=33 teach more than three courses. 186 | 5. Every student must be enrolled in the course called Mathl0l. 187 | 6. The room in which the earliest scheduled class (i.e., the class with the smallest meets_at value) meets should not be the same as the room in which the latest scheduled class meets. 188 | 7. Two classes cannot meet in the same room at the same time. 189 | 8. The department with the most faculty members must have fewer than twice the number of faculty members in the department with the fewest faculty members. 190 | 9. No department can have more than 10 faculty members. 191 | 10. A student cannot add more than two courses at a time (i.e., in a single update). 192 | 11. The number of CS majors must be more than the number of Math majors. 193 | 12. The number of distinct courses in which CS majors are enrolled is greater than the number of distinct courses in which Math majors are enrolled. 194 | 13. The total enrollment in courses taught by faculty in the department with deptid is greater than the number of Math majors. 195 | 14. There mUst be at least one CS major if there are any students whatsoever. 196 | 15. Faculty members from different departments cannot teach in the same room. 197 | 198 | 199 | #### `Answer` 200 | 201 | *** 202 | ### Exercise 5.9 203 | Discuss the strengths and weaknesses of the trigger mechanism. Contrast triggers with other integrity constraints supported by SQL. 204 | 205 | #### `Answer` 206 | 207 | *** 208 | ### Exercise 5.10 209 | Consider the following relational schema. An employee can work in more than one department; the pct_time field of the Works relation shows the percentage of time that a given employee works in a given department. 210 | 211 | Emp (eid: integer, ename: string, age: integer, salary: real) 212 | Works (eid: integer, did: integer, pct_time: integer) 213 | Dept (did: integer, budget: real, managerid: integer) 214 | 215 | Write SQL-92 integrity constraints (domain, key, foreign key, or CHECK constraints; or assertions) or SQL:1999 triggers to ensure each of the following requirements, considered independently. 216 | 1. Employees must make a minimum salary of $1000. 217 | 2. Every manager must be also be an employee. 218 | 3. The total percentage of all appointments for an employee must be under 100%. 219 | 4. A manager must always have a higher salary than any employee that he or she manages. 220 | 5. Whenever an employee is given a raise, the manager's salary must be increased to be at least as much. 221 | 6. Whenever an employee is given a raise, the manager's salary must be increased to be at least as much. Further, whenever an employee is given a raise, the department's budget must be increased. 222 | 223 | 224 | #### `Answer` 225 | 226 | *** 227 | -------------------------------------------------------------------------------- /Chapter 4 - Solutions.md: -------------------------------------------------------------------------------- 1 | # Database Management Systems - Chapter 4 Solutions 2 | 3 | ### Exercise 4.1 4 | Explain the statement that relational algebra operators can be composed. Why is the ability to compose operators important? 5 | #### `Answer` 6 | Every operator in relational algebra accepts one or more relation instances as arguments and the result is always an relation instance. So the argument 7 | of one operator could be the result of another operator. This is important because, 8 | this makes it easy to write complex queries by simply composing the relational algebra 9 | operators. 10 | 11 | *** 12 | ### Exercise 4.2 13 | Given two relations R1 and R2, where R1 contains N1 tuples, R2 contains N2 tuples, and N2 > N1 > 0, give the minimum and maximum possible sizes (in tuples) for the resulting relation produced by each of the following relational algebra expressions. In each case, state any assumptions about the schemas for R1 and R2 needed to make the expression meaningful: 14 | 1. R1 U R2, 15 | 2. R1 n R2, 16 | 3. R1 ~ R2, 17 | 4. R1 x R2, 18 | 5. (Ta=5(R1), 19 | 6. 7Ta(R1), and 20 | 7. R1/R2 21 | 22 | #### `Answer` 23 | 24 | *** 25 | ### Exercise 4.3 26 | Consider the following schema: 27 | Suppliers (sid: integer, sname: string, address: string) 28 | Parts (pid: integer, pname: string, color: string) 29 | Catalog (sid: integer, pid: integer, cost: real) 30 | 31 | The key fields are underlined, and the domain of each field is listed after the field name. Therefore sid is the key for Suppliers, pid is the key for Parts, and sid and pid together form the key for Catalog. The Catalog relation lists the prices charged for parts by Suppliers. Write 32 | the following queries in relational algebra, tuple relational calculus, and domain relational calculus: 33 | 1. Find the narnes of suppliers who supply some red part. 34 | 2. Find the sids of suppliers who supply some red or green part. 35 | 3. Find the sids of suppliers who supply some red part or are at 221 Packer Ave. 36 | 4. Find the sids of suppliers who supply some rcd part and some green part. 37 | 5. Find the sids of suppliers who supply every part. 38 | 6. Find the sids of suppliers who supply every red part. 39 | 7. Find the sids of suppliers who supply every red or green part. 40 | 8. Find the sids of suppliers who supply every red part or supply every green part. 41 | 9. Find pairs of sids such that the supplier with the first sid charges more for some part than the supplier with the second sid. 42 | 10. Find the pids of parts supplied by at least two different suppliers. 43 | 11. Find the pids of the most expensive parts supplied by suppliers named Yosemite Sham. 44 | 12. Find the pids of parts supplied by every supplier at less than $200. (If any supplier either does not supply the part or charges more than $200 for it, the part is not selected.) 45 | 46 | #### `Answer` 47 | In the answers below RA refers to Relational Algebra, TRC refers to 48 | Tuple Relational Calculus and DRC refers to Domain Relational Calculus. 49 | 50 | 1. - **RA** 51 | π sname(π sid((π pid σ (color = 'red') Parts) X Catalog) X Suppliers) 52 | - **TRC** 53 | {T | ∃T 1 ∈ Suppliers(∃X ∈ Parts(X.color = 'red' ∧ ∃Y ∈ Catalog 54 | (Y.pid = X.pid ∧ Y.sid = T 1.sid)) ∧ T.sname = T 1.sname)} 55 | - **DRC** 56 | { | ∈ Suppliers ∧ ∃P, Q, R( ∈ Parts 57 | ∧ (R = 'red') ∧ ∃I, J, K( ∈ Catalog ∧ J = P ∧ I = X))} 58 | - **SQL** 59 | SELECT S.sname 60 | FROM Suppliers S, Parts P, Catalog C 61 | WHERE P.color=’red’ AND C.pid=P.pid AND C.sid=S.sid 62 | 63 | 2. - **RA** 64 | π sid(π pid(σ (color='red' v color='green') Parts) X catalog) 65 | - **TRC** 66 | {T | ∃T 1 ∈ Catalog(∃X ∈ P arts((X.color = ‘red ∨ X.color = ‘green 67 | ) 68 | ∧X.pid = T 1.pid) ∧ T.sid = T 1.sid)} 69 | - **DRC** 70 | {X|X, Y, Z ∈ Catalog ∧ ∃A, B, C(A, B, C ∈ P arts 71 | ∧(C = red ∨ C = green 72 | ) ∧ A = Y )} 73 | - **SQL** 74 | SELECT C.sid 75 | FROM Catalog C, Parts P 76 | WHERE (P.color = ‘red’ OR P.color = ‘green’) 77 | AND P.pid = C.pid 78 | 79 | 3. - **RA** 80 | ρ(R1, πsid((πpidσcolor=redP arts)  Catalog)) 81 | ρ(R2, πsidσaddress=221P ackerStreetSuppliers) 82 | R1 ∪ R2 83 | - **TRC** 84 | {T | ∃T 1 ∈ Catalog(∃X ∈ P arts(X.color = ‘red ∧ X.pid = T 1.pid) 85 | ∧T.sid = T 1.sid) 86 | ∨∃T 2 ∈ Suppliers(T 2.address = 221P ackerStreet ∧ T.sid = T 2.sid)} 87 | - **DRC** 88 | {X|X, Y, Z ∈ Catalog ∧ ∃A, B, C(A, B, C ∈ P arts 89 | ∧C = red ∧ A = Y ) 90 | ∨∃P, Q(X, P, Q ∈ Suppliers ∧ Q = 221P ackerStreet 91 | )} 92 | - **SQL** 93 | SELECT S.sid 94 | FROM Suppliers S 95 | WHERE S.address = ‘221 Packer street’ 96 | OR S.sid IN ( SELECT C.sid 97 | FROM Parts P, Catalog C 98 | WHERE P.color=’red’ AND P.pid = C.pid ) 99 | 100 | 4. - **RA** 101 | ρ(R1, πsid((πpidσcolor=redP arts)  Catalog)) 102 | ρ(R2, πsid((πpidσcolor=greenP arts)  Catalog)) 103 | R1 ∩ R2 104 | - **TRC** 105 | {T | ∃T 1 ∈ Catalog(∃X ∈ P arts(X.color = ‘red ∧ X.pid = T 1.pid) 106 | ∧∃T 2 ∈ Catalog(∃Y ∈ P arts(Y.color = green ∧ Y.pid = T 2.pid) 107 | ∧T 2.sid = T 1.sid) ∧ T.sid = T 1.sid)} 108 | - **DRC** 109 | {X|X, Y, Z ∈ Catalog ∧ ∃A, B, C(A, B, C ∈ P arts 110 | ∧C = red ∧ A = Y ) 111 | ∧∃P, Q, R(P, Q, R ∈ Catalog ∧ ∃E, F, G(E, F, G ∈ P arts 112 | ∧G = green ∧ E = Q) ∧ P = X)} 113 | - **SQL** 114 | SELECT C.sid 115 | FROM Parts P, Catalog C 116 | WHERE P.color = ‘red’ AND P.pid = C.pid 117 | AND EXISTS ( SELECT P2.pid 118 | FROM Parts P2, Catalog C2 119 | WHERE P2.color = ‘green’ AND C2.sid = C.sid 120 | AND P2.pid = C2.pid ) 121 | 122 | 5. - **RA** 123 | (πsid,pidCatalog)/(πpidP arts) 124 | - **TRC** 125 | {T | ∃T 1 ∈ Catalog(∀X ∈ P arts(∃T 2 ∈ Catalog 126 | (T 2.pid = X.pid ∧ T 2.sid = T 1.sid)) ∧ T.sid = T 1.sid)} 127 | - **DRC** 128 | {X|X, Y, Z ∈ Catalog ∧ ∀A, B, C ∈ P arts 129 | (∃P, Q, R ∈ Catalog(Q = A ∧ P = X))} 130 | - **SQL** 131 | SELECT C.sid 132 | FROM Catalog C 133 | WHERE NOT EXISTS (SELECT P.pid 134 | FROM Parts P 135 | WHERE NOT EXISTS (SELECT C1.sid 136 | FROM Catalog C1 137 | WHERE C1.sid = C.sid 138 | AND C1.pid = P.pid)) 139 | 140 | 6. - **RA** 141 | (πsid,pidCatalog)/(πpidσcolor=redP arts) 142 | - **TRC** 143 | {T | ∃T 1 ∈ Catalog(∀X ∈ P arts(X.color 144 | = ‘red 145 | ∨∃T 2 ∈ Catalog(T 2.pid = X.pid ∧ T 2.sid = T 1.sid)) 146 | ∧T.sid = T 1.sid)} 147 | - **DRC** 148 | {X|X, Y, Z ∈ Catalog ∧ ∀A, B, C ∈ P arts 149 | (C 150 | = ‘red ∨ ∃P, Q, R ∈ Catalog(Q = A ∧ P = X))} 151 | - **SQL** 152 | SELECT C.sid 153 | FROM Catalog C 154 | WHERE NOT EXISTS (SELECT P.pid 155 | FROM Parts P 156 | WHERE P.color = ‘red’ 157 | AND (NOT EXISTS (SELECT C1.sid 158 | FROM Catalog C1 159 | WHERE C1.sid = C.sid AND 160 | C1.pid = P.pid))) 161 | 162 | 7. - **RA** 163 | (πsid,pidCatalog)/(πpidσcolor=red∨color=greenP arts) 164 | TRC 165 | {T | ∃T 1 ∈ Catalog(∀X ∈ P arts((X.color 166 | = ‘red 167 | ∧X.color 168 | = ‘green 169 | ) ∨ ∃T 2 ∈ Catalog 170 | (T 2.pid = X.pid ∧ T 2.sid = T 1.sid)) ∧ T.sid = T 1.sid)} 171 | - **DRC** 172 | {X|X, Y, Z ∈ Catalog ∧ ∀A, B, C ∈ P arts 173 | ((C 174 | = ‘red ∧ C 175 | = ‘green 176 | ) ∨ ∃P, Q, R ∈ Catalog 177 | (Q = A ∧ P = X))} 178 | - **SQL** 179 | SELECT C.sid 180 | FROM Catalog C 181 | WHERE NOT EXISTS (SELECT P.pid 182 | FROM Parts P 183 | WHERE (P.color = ‘red’ OR P.color = ‘green’) 184 | AND (NOT EXISTS (SELECT C1.sid 185 | FROM Catalog C1 186 | WHERE C1.sid = C.sid AND 187 | C1.pid = P.pid))) 188 | 189 | 8. - **RA** 190 | ρ(R1,((πsid,pidCatalog)/(πpidσcolor=redP arts))) 191 | ρ(R2,((πsid,pidCatalog)/(πpidσcolor=greenP arts))) 192 | R1 ∪ R2 193 | - **TRC** 194 | {T | ∃T 1 ∈ Catalog((∀X ∈ P arts 195 | (X.color 196 | = ‘red ∨ ∃Y ∈ Catalog(Y.pid = X.pid ∧ Y.sid = T 1.sid)) 197 | ∨∀Z ∈ P arts(Z.color 198 | = ‘green ∨ ∃P ∈ Catalog 199 | (P.pid = Z.pid ∧ P.sid = T 1.sid))) ∧ T.sid = T 1.sid)} 200 | - **DRC** 201 | {X|X, Y, Z ∈ Catalog ∧ (∀A, B, C ∈ P arts 202 | (C 203 | = ‘red ∨ ∃P, Q, R ∈ Catalog(Q = A ∧ P = X)) 204 | ∨∀U, V, W ∈ P arts(W 205 | = ‘green ∨ M,N,L ∈ Catalog 206 | (N = U ∧ M = X)))} 207 | - **SQL** 208 | SELECT C.sid 209 | FROM Catalog C 210 | WHERE (NOT EXISTS (SELECT P.pid 211 | FROM Parts P 212 | WHERE P.color = ‘red’ AND 213 | (NOT EXISTS (SELECT C1.sid 214 | FROM Catalog C1 215 | WHERE C1.sid = C.sid AND 216 | C1.pid = P.pid)))) 217 | OR ( NOT EXISTS (SELECT P1.pid 218 | FROM Parts P1 219 | WHERE P1.color = ‘green’ AND 220 | (NOT EXISTS (SELECT C2.sid 221 | FROM Catalog C2 222 | WHERE C2.sid = C.sid AND 223 | C2.pid = P1.pid)))) 224 | 225 | 9. - **RA** 226 | ρ(R1, Catalog) 227 | ρ(R2, Catalog) 228 | πR1.sid,R2.sid(σR1.pid=R2.pid∧R1.sid=R2.sid∧R1.cost>R2.cost(R1 × R2)) 229 | - **TRC** 230 | {T | ∃T 1 ∈ Catalog(∃T 2 ∈ Catalog 231 | (T 2.pid = T 1.pid ∧ T 2.sid 232 | = T 1.sid 233 | ∧T 2.cost < T 1.cost ∧ T.sid2 = T 2.sid) 234 | ∧T.sid1 = T 1.sid)} 235 | - **DRC** 236 | {X, P|X, Y, Z ∈ Catalog ∧ ∃P, Q, R 237 | (P, Q, R ∈ Catalog ∧ Q = Y ∧ P 238 | = X ∧ R C2.cost 245 | 246 | 10. - **RA** 247 | ρ(R1, Catalog) 248 | ρ(R2, Catalog) 249 | πR1.pidσR1.pid=R2.pid∧R1.sid=R2.sid(R1 × R2) 250 | 251 | - **TRC** 252 | {T | ∃T 1 ∈ Catalog(∃T 2 ∈ Catalog 253 | (T 2.pid = T 1.pid ∧ T 2.sid 254 | = T 1.sid) 255 | ∧T.pid = T 1.pid)} 256 | - **DRC** 257 | {X|X, Y, Z ∈ Catalog ∧ ∃A, B, C 258 | (A, B, C ∈ Catalog ∧ B = Y ∧ A 259 | = X)} 260 | - **SQL** 261 | SELECT C.pid 262 | FROM Catalog C 263 | WHERE EXISTS (SELECT C1.sid 264 | FROM Catalog C1 265 | WHERE C1.pid = C.pid AND C1.sid 266 | = C.sid ) 267 | 268 | 11. - **RA** 269 | ρ(R1, πsidσsname=Y osemiteShamSuppliers) 270 | ρ(R2, R1  Catalog) 271 | ρ(R3, R2) 272 | ρ(R4(1 → sid, 2 → pid, 3 → cost), σR3.cost T 1.cost))) ∧ T.pid = T 1.pid) 279 | - **DRC** 280 | {Y |X, Y, Z ∈ Catalog ∧ ∃A, B, C 281 | (A, B, C ∈ Suppliers ∧ C = Y osemiteSham ∧ A = X) 282 | ∧¬(∃P, Q, R(P, Q, R ∈ Suppliers ∧ R = Y osemiteSham 283 | ∧∃I, J, K(I, J, K ∈ Catalog(I = P ∧ K>Z))))} 284 | - **SQL** 285 | SELECT C.pid 286 | FROM Catalog C, Suppliers S 287 | WHERE S.sname = ‘Yosemite Sham’ AND C.sid = S.sid 288 | AND C.cost ≥ ALL (Select C2.cost 289 | FROM Catalog C2, Suppliers S2 290 | WHERE S2.sname = ‘Yosemite Sham’ 291 | AND C2.sid = S2.sid) 292 | 293 | *** 294 | ### Exercise 4.4 295 | Consider the Supplier-Parts-Catalog schema from the previous question. State what the following queries compute: 296 | 1. π sname (π sid( σ color='red' Parts) X (σ cost < 100 Catalog) X Suppliers) 297 | 2. π sname (1f S id ( σ color='red' Parts) X ( σ cost < 100 Catalog) X Suppliers)) 298 | 3. (π sname ((σ color'='red' Parts) [X] ( σ cost < 100 Catalog) X Suppliers)) ∩ (π sname(( σ color = 'green' Parts) X (σ cost < 100 Catalog ) X Suppliers) 299 | 4. (π sid((color='red' Parts) X ( σ cost < 100 Catalog) X Suppliers)) n 300 | ( π sid ((σ color='green' Parts) X ( σ cost < 100 Catalog) X Suppliers)) 301 | (π sid,sname ((σ color='green' Parts) X (σ cost < 100 Catalog) X Suppliers))) 302 | 303 | 304 | #### `Answer` 305 | 306 | *** 307 | ### Exercise 4.5 308 | Consider the following relations containing airline flight information: 309 | 310 | Flights (fino: integer, from: string, to: string, distance: integer, departs: time, arrives: time) 311 | Aircraft( aid: integer, aname: string, cTuisingrange: integer) 312 | Certified( eid: integer, aid: integer) 313 | Employees( eid: integer, ename: string, salary: integer) 314 | 315 | Note that the Employees relation describes pilots and other kinds of employees as well; every pilot is certified for some aircraft (otherwise, he or she would not qualify as a pilot), and only 316 | pilots are certified to fly. 317 | 318 | Write the following queries in relational algebra, tuple relational calculus, and domain relational calculus. Note that some of these queries may not be expressible in relational algebra (and, therefore, also not expressible in tuple and domain relational calculus). For such queries, informally explain why they cannot be expressed. (See the exercises at the end of Chapter 5 for additional queries over the airline schema.) 319 | 1. Find the eids of pilots certified for some Boeing aircraft. 320 | 2. Find the names of pilots certified for some Boeing aircraft. 321 | ~). 322 | 3. Find the aids of all aircraft that. can be used on non-stop flights from Bonn to Madras. 323 | 4. Identify the flights that can be piloted by every pilot whose salary is more than $100,000. 324 | 5. Find the names of pilots who can operate planes with a range greater than 3,000 miles 325 | but are not certified on any Boeing aircraft. 326 | 6. Find the eids of employees who make the highest salary. 327 | 7. Find the eids of employees who make the second highest salary. 328 | 8. Find the eids of employees who are certified for the largest number of aircraft. 329 | 9. Find the eids of employees who are certified for exactly three aircraft. 330 | 10. Find the total amount paid to employees as salaries. 331 | 11. Is there a sequence of flights from Madison to Timbuktu? Each flight in the sequence is required to depart from the city that is the destination of the previous flight; the first flight must leave Madison, the last flight must reach Timbuktu, and there is no restriction 332 | on the number of intermediate flights. Your query must determine whether a sequence of flights from Madison to Timbuktu exists for any input Flights relation instance. 333 | 334 | #### `Answer` 335 | 1. - **RA** 336 | πeid(σaname=‘Boeing (Aircraf t  Certif ied)) 337 | - **TRC** 338 | {C.eid | C ∈ Certif ied ∧ 339 | ∃A ∈ Aircraf t(A.aid = C.aid ∧ A.aname = ‘Boeing 340 | )} 341 | - **DRC** 342 | {Ceid|Ceid, Caid ∈ Certif ied ∧ 343 | ∃Aid, AN, AR(Aid, AN, AR ∈ Aircraf t 344 | ∧Aid = Caid ∧ AN = ‘Boeing 345 | )} 346 | - **SQL** 347 | SELECT C.eid 348 | FROM Aircraft A, Certified C 349 | WHERE A.aid = C.aid AND A.aname = ‘Boeing’ 350 | 2. - **RA** 351 | πename(σaname=‘Boeing (Aircraf t  Certif ied  Employees)) 352 | - **TRC** 353 | {E.ename | E ∈ Employees ∧ ∃C ∈ Certif ied 354 | (∃A ∈ Aircraf t(A.aid = C.aid ∧ A.aname = ‘Boeing ∧ E.eid = C.eid))} 355 | - **DRC** 356 | {EN|Eid, EN, ES ∈ Employees∧ 357 | ∃Ceid, Caid(Ceid, Caid ∈ Certif ied∧ 358 | ∃Aid, AN, AR(Aid, AN, AR ∈ Aircraf t∧ 359 | Aid = Caid ∧ AN = ‘Boeing ∧ Eid = Ceid)} 360 | - **SQL** 361 | SELECT E.ename 362 | FROM Aircraft A, Certified C, Employees E 363 | WHERE A.aid = C.aid AND A.aname = ‘Boeing’ AND E.eid = C.eid 364 | 3. - **RA** 365 | ρ(BonnT oM adrid, σf rom=‘Bonn∧to=‘Madrid (Flights)) 366 | πaid(σcruisingrange>distance(Aircraf t × BonnT oM adrid)) 367 | - **TRC** 368 | {A.aid | A ∈ Aircraf t ∧ ∃F ∈ Flights 369 | (F.from = ‘Bonn ∧ F.to = ‘M adrid ∧ A.cruisingrange > F.distance)} 370 | - **DRC** 371 | {Aid | Aid, AN, AR ∈ Aircraf t∧ 372 | (∃F N, F F, F T, FDi, FDe, F A(F N, F F, F T, FDi, FDe, F A ∈ Flights∧ 373 | F F = ‘Bonn ∧ F T = ‘M adrid ∧ F Di < AR))} 374 | - **SQL** 375 | SELECT A.aid 376 | FROM Aircraft A, Flights F 377 | WHERE F.from = ‘Bonn’ AND F.to = ‘Madrid’ AND 378 | A.cruisingrange > F.distance 379 | 4. - **RA** 380 | πf lno(σdistance100,000(Flights  Aircraf t  381 | Certif ied  Employees))) 382 | - **TRC** {F.flno | F ∈ Flights ∧ ∃A ∈ Aircraf t∃C ∈ Certif ied 383 | ∃E ∈ Employees(A.cruisingrange > F.distance ∧ E.salary > 100, 000∧ 384 | A.aid = C. 385 | 40 Chapter 4 386 | 5. - **RA** ρ(R1, πeid(σcruisingrange>3000(Aircraf t  Certif ied))) 387 | πename(Employees  (R1 − πeid(σaname=‘Boeing (Aircraf t  Certif ied)))) 388 | - **TRC** 389 | {E.ename | E ∈ Employees ∧ ∃C ∈ Certif ied(∃A ∈ Aircraf t 390 | (A.aid = C.aid ∧ E.eid = C.eid ∧ A.cruisingrange > 3000))∧ 391 | ¬(∃C2 ∈ Certif ied(∃A2 ∈ Aircraf t(A2.aname = ‘Boeing ∧ C2.aid = 392 | A2.aid ∧ C2.eid = E.eid)))} 393 | - **DRC** 394 | {EN|Eid, EN, ES ∈ Employees∧ 395 | ∃Ceid, Caid(Ceid, Caid ∈ Certif ied∧ 396 | ∃Aid, AN, AR(Aid, AN, AR ∈ Aircraf t∧ 397 | Aid = Caid ∧ Eid = Ceid ∧ AR > 3000))∧ 398 | ¬(∃Aid2, AN2, AR2(Aid2, AN2, AR2 ∈ Aircraf t∧ 399 | ∃Ceid2, Caid2(Ceid2, Caid2 ∈ Certif ied 400 | ∧Aid2 = Caid2 ∧ Eid = Ceid2 ∧ AN2=‘Boeing 401 | )))} 402 | - **SQL** 403 | SELECT E.ename 404 | FROM Certified C, Employees E, Aircraft A 405 | WHERE A.aid = C.aid AND E.eid = C.eid AND A.cruisingrange > 3000 406 | AND E.eid NOT IN ( SELECT C2.eid 407 | FROM Certified C2, Aircraft A2 408 | WHERE C2.aid = A2.aid AND A2.aname = ‘Boeing’ ) 409 | 6. - **RA** 410 | The approach to take is first find all the employees who do not have the 411 | highest salary. Subtract these from the original list of employees and what 412 | is left is the highest paid employees. 413 | ρ(E1, Employees) 414 | ρ(E2, Employees) 415 | ρ(E3, πE2.eid(E1 E1.salary>E2.salary E2) 416 | (πeidE1) − E3 417 | - **TRC** 418 | {E1.eid | E1 ∈ Employees∧¬(∃E2 ∈ Employees(E2.salary > E1.salary))} 419 | - **DRC** 420 | {Eid1|Eid1,EN1,ES1 ∈ Employees∧ 421 | ¬(∃Eid2,EN2,ES2(Eid2,EN2,ES2 ∈ Employees ∧ ES2 > ES1))} 422 | - **SQL** 423 | SELECT E.eid 424 | FROM Employees E 425 | WHERE E.salary = ( Select MAX (E2.salary) 426 | FROM Employees E2 ) 427 | 7. - **RA** 428 | The approach taken is similar to the solution for the previous exercise. First 429 | find all the employees who do not have the highest salary. Remove these from 430 | the original list of employees and what is left is the highest paid employees. 431 | Remove the highest paid employees from the original list. What is left is the 432 | second highest paid employees together with the rest of the employees. Then 433 | find the highest paid employees of this new list. This is the list of the second 434 | highest paid employees. 435 | ρ(E1, Employees) 436 | ρ(E2, Employees) 437 | ρ(E3, πE2.eid(E1 E1.salary>E2.salary E2) 438 | ρ(E4, E2  E3) 439 | ρ(E5, E2  E3) 440 | ρ(E6, πE5.eid(E4 E1.salary>E5.salary E5) 441 | (πeidE3) − E6 442 | - **TRC** 443 | {E1.eid | E1 ∈ Employees ∧ ∃E2 ∈ Employees(E2.salary > E1.salary 444 | ∧¬(∃E3 ∈ Employees(E3.salary > E2.salary)))} 445 | - **DRC** 446 | {Eid1|Eid1,EN1,ES1 ∈ Employees∧ 447 | ∃Eid2,EN2,ES2(Eid2,EN2,ES2 ∈ Employees(ES2 > ES1) 448 | ∧¬(∃Eid3,EN3,ES3(Eid3,EN3,ES3 ∈ Employees(ES3 > ES2))))} 449 | - **SQL** 450 | SELECT E.eid 451 | FROM Employees E 452 | WHERE E.salary = (SELECT MAX (E2.salary) 453 | FROM Employees E2 454 | WHERE E2.salary 455 | = (SELECT MAX (E3.salary) 456 | FROM Employees E3 )) 457 | 42 Chapter 4 458 | 8. This cannot be expressed in relational algebra (or calculus) because there is no 459 | operator to count, and this query requires the ability to count up to a number 460 | that depends on the data. The query can however be expressed in SQL as follows: 461 | SELECT Temp.eid 462 | FROM ( SELECT C.eid AS eid, COUNT (C.aid) AS cnt, 463 | FROM Certified C 464 | GROUP BY C.eid) AS Temp 465 | WHERE Temp.cnt = ( SELECT MAX (Temp.cnt) 466 | FROM Temp) 467 | 9. - **RA** 468 | The approach behind this query is to first find the employees who are certified 469 | for at least three aircraft (they appear at least three times in the Certified 470 | relation). Then find the employees who are certified for at least four aircraft. 471 | Subtract the second from the first and what is left is the employees who are 472 | certified for exactly three aircraft. 473 | ρ(R1, Certif ied) 474 | ρ(R2, Certif ied) 475 | ρ(R3, Certif ied) 476 | ρ(R4, Certif ied) 477 | ρ(R5, πeid(σ(R1.eid=R2.eid=R3.eid)∧(R1.aid=R2.aid=R3.aid) (R1 × R2 × R3))) 478 | ρ(R6, πeid(σ(R1.eid=R2.eid=R3.eid=R4.eid)∧(R1.aid=R2.aid=R3.aid=R4.aid) 479 | (R1 × R2 × R3 × R4))) 480 | R5 − R6 481 | - **TRC** 482 | {C1.eid | C1 ∈ Certif ied ∧ ∃C2 ∈ Certif ied(∃C3 ∈ Certif ied 483 | (C1.eid = C2.eid ∧ C2.eid = C3.eid∧ 484 | C1.aid 485 | = C2.aid ∧ C2.aid 486 | = C3.aid ∧ C3.aid 487 | = C1.aid∧ 488 | ¬(∃C4 ∈ Certif ied 489 | (C3.eid = C4.eid ∧ C1.aid 490 | = C4.aid∧ 491 | C2.aid 492 | = C4.aid ∧ C3.aid 493 | = C4.aid))))} 494 | - **DRC** 495 | {CE1|CE1,CA1 ∈ Certif ied∧ 496 | ∃CE2,CA2(CE2,CA2 ∈ Certif ied∧ 497 | ∃CE3,CA3(CE3,CA3 ∈ Certif ied∧ 498 | (CE1 = CE2 ∧ CE2 = CE3∧ 499 | CA1 500 | = CA2 ∧ CA2 501 | = CA3 ∧ CA3 502 | = CA1∧ 503 | ¬(∃CE4,CA4(CE4,CA4 ∈ Certif ied∧ 504 | (CE3 = CE4 ∧ CA1 505 | = CA4∧ 506 | CA2 507 | = CA4 ∧ CA3 508 | = CA4))))} 509 | - **SQL** 510 | SELECT C1.eid 511 | FROM Certified C1, Certified C2, Certified C3 512 | WHERE (C1.eid = C2.eid AND C2.eid = C3.eid AND 513 | C1.aid 514 | = C2.aid AND C2.aid 515 | = C3.aid AND C3.aid 516 | = C1.aid) 517 | EXCEPT 518 | SELECT C4.eid 519 | FROM Certified C4, Certified C5, Certified C6, Certified C7, 520 | WHERE (C4.eid = C5.eid AND C5.eid = C6.eid AND C6.eid = C7.eid AND 521 | C4.aid 522 | = C5.aid AND C4.aid 523 | = C6.aid AND C4.aid 524 | = C7.aid AND 525 | C5.aid 526 | = C6.aid AND C5.aid 527 | = C7.aid AND C6.aid 528 | = C7.aid ) 529 | This could also be done in SQL using COUNT. 530 | 10. This cannot be expressed in relational algebra (or calculus) because there is no 531 | operator to sum values. The query can however be expressed in SQL as follows: 532 | SELECT SUM (E.salaries) 533 | FROM Employees E 534 | 11. This cannot be expressed in relational algebra or relational calculus or SQL. The 535 | problem is that there is no restriction on the number of intermediate flights. All 536 | of the query methods could find if there was a flight directly from Madison to 537 | Timbuktu and if there was a sequence of two flights that started in Madison and 538 | ended in Timbuktu. They could even find a sequence of n flights that started in 539 | Madison and ended in Timbuktu as long as there is a static (i.e., data-independent) 540 | upper bound on the number of intermediate flights. (For large n, this would of 541 | course be long and impractical, but at least possible.) In this query, however, the 542 | upper bound is not static but dynamic (based upon the set of tuples in the Flights 543 | relation). 544 | In summary, if we had a static upper bound (say k), we could write an algebra 545 | or SQL query that repeatedly computes (upto k) joins on the Flights relation. If 546 | the upper bound is dynamic, then we cannot write such a query because k is not 547 | known when writing the query 548 | 549 | *** 550 | ### Exercise 4.6 551 | What is relational completeness? If a query language is relationally complete, can you write any desired query in that language? 552 | 553 | #### `Answer` 554 | 555 | *** 556 | ### Exercise 4.7 557 | What is an unsafe query? Give an example and explain why it is important 558 | to disallow such queries. 559 | 560 | #### `Answer` 561 | An unsafe query is a query in relational calculus that has an infinite 562 | number of results. An example of such a query is: 563 | {S | ¬(S ∈ Sailors)} 564 | The query is for all things that are not sailors which of course is everything else. Clearly 565 | there is an infinite number of answers, and this query is unsafe. It is important to 566 | disallow unsafe queries because we want to be able to get back to users with a list of 567 | all the answers to a query after a finite amount of time. 568 | 569 | 570 | *** 571 | -------------------------------------------------------------------------------- /Chapter 3 - Solutions.md: -------------------------------------------------------------------------------- 1 | # Database Management Systems - Chapter 3 Solutions 2 | 3 | ### Exercise 3.1 4 | Define the following terms: relation schema, relational database schema, domain, relation instance, relation cardinality, and relation degree. 5 | 6 | ### `Answer` 7 | A relation schema can be thought of as the basic information describing 8 | a table or relation. This includes a set of column names, the data types associated 9 | with each column, and the name associated with the entire table. For example, a 10 | relation schema for the relation called Students could be expressed using the following 11 | representation: 12 | 13 | Students(sid: string, name: string, login: string, 14 | age: integer, gpa: real) 15 | 16 | There are five fields or columns, with names and types as shown above. 17 | 18 | A relational database schema is a collection of relation schemas, describing one or more 19 | relations. 20 | 21 | Domain is synonymous with data type. Attributes can be thought of as columns in a 22 | table. Therefore, an attribute domain refers to the data type associated with a column. 23 | 24 | A relation instance is a set of tuples (also known as rows or records) that each conform 25 | to the schema of the relation. 26 | 27 | The relation cardinality is the number of tuples in the relation. 28 | The relation degree is the number of fields (or columns) in the relation. 29 | 30 | A relation schema can be thought of as the basic information describing 31 | a table or relation. This includes a set of column names, the data types associated 32 | with each column, and the name associated with the entire table. For example, a 33 | relation schema for the relation called Students could be expressed using the following 34 | representation: 35 | 36 | Students(sid: string, name: string, login: string, 37 | age: integer, gpa: real) 38 | 39 | There are five fields or columns, with names and types as shown above. 40 | 41 | A relational database schema is a collection of relation schemas, describing one or more 42 | relations. 43 | 44 | Domain is synonymous with data type. Attributes can be thought of as columns in a 45 | table. Therefore, an attribute domain refers to the data type associated with a column. 46 | 47 | A relation instance is a set of tuples (also known as rows or records) that each conform 48 | to the schema of the relation. 49 | 50 | The relation cardinality is the number of tuples in the relation. 51 | 52 | The relation degree is the number of fields (or columns) in the relation. 53 | 54 | *** 55 | ### Exercise 3.2 56 | How many distinct tuples are in a relation instance with cardinality 22? 57 | 58 | ### `Answer` 59 | 60 | *** 61 | ### Exercise 3.3 62 | Does the relational model, as seen by an SQL query writer, provide physical and logical data independence? Explain. 63 | 64 | ### `Answer` 65 | The user of SQL has no idea how the data is physically represented in the 66 | machine. He or she relies entirely on the relation abstraction for querying. Physical 67 | data independence is therefore assured. Since a user can define views, logical data 68 | independence can also be achieved by using view definitions to hide changes in the 69 | conceptual schema. 70 | 71 | *** 72 | ### Exercise 3.4 73 | What is the difference between a candidate key and the primary key for a given relation? What is a superkey? 74 | 75 | 76 | ### `Answer` 77 | 78 | *** 79 | ### Exercise 3.5 80 | Consider the instance of the Students relation shown in Figure 3.1. 81 | 1. Give an example of an attribute (or set of attributes) that you can deduce is not a candidate key, based on this instance being legal. 82 | 2. Is there any example of an attribute (or set of attributes) that you can deduce is a candidate key, based on this instance being legal? 83 | 84 | ### `Answer` 85 | Examples of non-candidate keys include the following: {name}, {age}. 86 | (Note that {gpa} can not be declared as a non-candidate key from this evidence alone 87 | even though common sense tells us that clearly more than one student could have the 88 | same grade point average.) 89 | You cannot determine a key of a relation given only one instance of the relation. The 90 | fact that the instance is “legal” is immaterial. A candidate key, as defined here, is a 91 | key, not something that only might be a key. The instance shown is just one possible 92 | “snapshot” of the relation. At other times, the same relation may have an instance (or 93 | snapshot) that contains a totally different set of tuples, and we cannot make predictions 94 | about those instances based only upon the instance that we are given. 95 | 96 | *** 97 | ### Exercise 3.6 98 | What is a foreign key constraint? Why are such constraints important? What is referential integrity? 99 | 100 | ### `Answer` 101 | 102 | *** 103 | ### Exercise 3.7 104 | Consider the relations Students, Faculty, Courses, Rooms, Enrolled, Teaches, and Meets_in defined in Section 1.5.2. 105 | 1. List all the foreign key constraints among these relations. 106 | 2. Give an example of a (plausible) constraint involving one or more of these relations that is not a primary key or foreign key constraint. 107 | 108 | ### `Answer` 109 | There is no reason for a foreign key constraint (FKC) on the Students, 110 | Faculty, Courses, or Rooms relations. These are the most basic relations and must be 111 | free-standing. Special care must be given to entering data into these base relations. 112 | In the Enrolled relation, sid and cid should both have FKCs placed on them. (Real 113 | students must be enrolled in real courses.) Also, since real teachers must teach real 114 | courses, both the f id and the cid fields in the Teaches relation should have FKCs. 115 | Finally, Meets In should place FKCs on both the cid and rno fields. 116 | It would probably be wise to enforce a few other constraints on this DBMS: the length 117 | of sid, cid, and f id could be standardized; checksums could be added to these identification numbers; limits could be placed on the size of the numbers entered into the 118 | credits, capacity, and salary fields; an enumerated type should be assigned to the grade 119 | field (preventing a student from receiving a grade of G, among other things); etc. 120 | 121 | There is no reason for a foreign key constraint (FKC) on the Students, 122 | Faculty, Courses, or Rooms relations. These are the most basic relations and must be 123 | free-standing. Special care must be given to entering data into these base relations. 124 | In the Enrolled relation, sid and cid should both have FKCs placed on them. (Real 125 | students must be enrolled in real courses.) Also, since real teachers must teach real 126 | courses, both the f id and the cid fields in the Teaches relation should have FKCs. 127 | Finally, Meets In should place FKCs on both the cid and rno fields. 128 | It would probably be wise to enforce a few other constraints on this DBMS: the length 129 | of sid, cid, and f id could be standardized; checksums could be added to these identification 130 | numbers; limits could be placed on the size of the numbers entered into the 131 | credits, capacity, and salary fields; an enumerated type should be assigned to the grade 132 | field (preventing a student from receiving a grade of G, among other things); etc. 133 | 134 | *** 135 | ### Exercise 3.8 136 | Answer each of the following questions briefly. The questions are based On the following relational schema: 137 | Emp (eid: integer, ename: string, age: integer, sala1l1: real) 138 | Works (eid: integer, did: integer, peLtime: integer) 139 | Dept (did: integer, dname: string, budget: real, managerid: integer) 140 | 141 | 1. Give an example of a foreign key constraint that involves the Dept relation. What are the options for enforcing this constraint when a user attempts to delete a Dept tuple? 142 | 2. Write the SQL statements required to create the preceding relations, including appropriate versions of all primary and foreign key integrity constraints. 143 | 3. Define the Dept relation in SQL so that every department is guaranteed to have a manager. 144 | 4. Write an SQL statement to add John Doe as an employee with eid = 101, age = 32 and salary = 15,000. 145 | 5. Write an SQL statement to give every employee a 10 percent raise. 146 | 6. Write an SQL statement to delete the Toy department. Given the referential integrity constraints you chose for this schema, explain what happens when this statement is executed. 147 | 148 | ### `Answer` 149 | 150 | *** 151 | ### Exercise 3.9 152 | Consider the SQL query whose answer is shown in Figure 3.6. 153 | 1. Modify this query so that only the login column is included in the answer. 154 | 2. If the clause WHERE S.gpa >= 2 is added to the original query, what is the set of tuples in the answer? 155 | 156 | ### `Answer` 157 | 158 | 1. Only login is included in the answer: 159 | 160 | SELECT S.login 161 | FROM Students S 162 | WHERE S.age < 18 163 | 164 | 2. The answer tuple for Madayan is omitted then. 165 | 166 | *** 167 | ### Exercise 3.10 168 | Explain why the addition of NOT NULL constraints to the SQL definition of the Manages relation (in Section 3.5.3) would not enforce the constraint that each department must have a manager. What, if anything, is achieved by requiring that the S8n field of Manages be non-null? 169 | 170 | ### `Answer` 171 | 172 | *** 173 | ### Exercise 3.11 174 | Suppose that we have a ternary relationship R between entity sets A, B, 175 | and C such that A has a key constraint and total participation and B has a key constraint; these are the only constraints. A has attributes al and a2, with al being the key; Band C are similar. R has no descriptive attributes. 176 | Write SQL statements that create tables corresponding to this information so &s to capture as many of the constraints as possible. If 177 | you cannot capture some constraint, explain why. 178 | 179 | ### `Answer` 180 | The following SQL statements create the corresponding relations. 181 | 182 | CREATE TABLE A ( a1 CHAR(10), 183 | a2 CHAR(10), 184 | b1 CHAR(10), 185 | c1 CHAR(10), 186 | PRIMARY KEY (a1), 187 | UNIQUE (b1), 188 | FOREIGN KEY (b1) REFERENCES B, 189 | FOREIGN KEY (c1) REFERENCES C ) 190 | CREATE TABLE B ( b1 CHAR(10), 191 | b2 CHAR(10), 192 | PRIMARY KEY (b1) ) 193 | CREATE TABLE C ( b1 CHAR(10), 194 | c2 CHAR(10), 195 | PRIMARY KEY (c1) ) 196 | 197 | The first SQL statement folds the relationship R into table A and thereby guarantees 198 | the participation constraint. 199 | 200 | The following SQL statements create the corresponding relations. 201 | CREATE TABLE A ( a1 CHAR(10), 202 | a2 CHAR(10), 203 | b1 CHAR(10), 204 | c1 CHAR(10), 205 | PRIMARY KEY (a1), 206 | UNIQUE (b1), 207 | FOREIGN KEY (b1) REFERENCES B, 208 | FOREIGN KEY (c1) REFERENCES C ) 209 | CREATE TABLE B ( b1 CHAR(10), 210 | b2 CHAR(10), 211 | PRIMARY KEY (b1) ) 212 | CREATE TABLE C ( b1 CHAR(10), 213 | c2 CHAR(10), 214 | PRIMARY KEY (c1) ) 215 | 216 | The first SQL statement folds the relationship R into table A and thereby guarantees 217 | the participation constraint. 218 | 219 | *** 220 | ### Exercise 3.12 221 | Consider the scenario from Exercise 2.2, where you designed an ER diagram for a university database. Write SQL statements to create the corresponding relations and capture as many of the constraints as possible. If you cannot, capture some constraints, explain 222 | why. 223 | 224 | ### `Answer` 225 | 226 | *** 227 | ### Exercise 3.13 228 | Consider the university database from Exercise 2.3 and the ER diagram you designed. Write SQL statements to create the corresponding relations and capture as many of the constraints as possible. If you cannot capture some constraints, explain why. 229 | 230 | ### `Answer` 231 | The following SQL statements create the corresponding relations. 232 | 1. CREATE TABLE Professors ( prof ssn CHAR(10), 233 | name CHAR(64), 234 | age INTEGER, 235 | rank INTEGER, 236 | speciality CHAR(64), 237 | PRIMARY KEY (prof ssn) ) 238 | 2. CREATE TABLE Depts ( dno INTEGER, 239 | dname CHAR(64), 240 | office CHAR(10), 241 | PRIMARY KEY (dno) ) 242 | 3. CREATE TABLE Runs ( dno INTEGER, 243 | prof ssn CHAR(10), 244 | PRIMARY KEY ( dno, prof ssn), 245 | FOREIGN KEY (prof ssn) REFERENCES Professors, 246 | FOREIGN KEY (dno) REFERENCES Depts ) 247 | 4. CREATE TABLE Work Dept ( dno INTEGER, 248 | prof ssn CHAR(10), 249 | pc time INTEGER, 250 | PRIMARY KEY (dno, prof ssn), 251 | FOREIGN KEY (prof ssn) REFERENCES Professors, 252 | FOREIGN KEY (dno) REFERENCES Depts ) 253 | Observe that we would need check constraints or assertions in SQL to enforce the 254 | rule that Professors work in at least one department. 255 | 5. CREATE TABLE Project ( pid INTEGER, 256 | sponsor CHAR(32), 257 | start date DATE, 258 | end date DATE, 259 | budget FLOAT, 260 | PRIMARY KEY (pid) ) 261 | 6. CREATE TABLE Graduates ( grad ssn CHAR(10), 262 | age INTEGER, 263 | name CHAR(64), 264 | deg prog CHAR(32), 265 | major INTEGER, 266 | PRIMARY KEY (grad ssn), 267 | FOREIGN KEY (major) REFERENCES Depts ) 268 | Note that the Major table is not necessary since each Graduate has only one major 269 | and so this can be an attribute in the Graduates table. 270 | 7. CREATE TABLE Advisor ( senior ssn CHAR(10), 271 | grad ssn CHAR(10), 272 | PRIMARY KEY (senior ssn, grad ssn), 273 | FOREIGN KEY (senior ssn) 274 | REFERENCES Graduates (grad ssn), 275 | FOREIGN KEY (grad ssn) REFERENCES Graduates ) 276 | 8. CREATE TABLE Manages ( pid INTEGER, 277 | prof ssn CHAR(10), 278 | PRIMARY KEY (pid, prof ssn), 279 | FOREIGN KEY (prof ssn) REFERENCES Professors, 280 | FOREIGN KEY (pid) REFERENCES Projects ) 281 | 9. CREATE TABLE Work In ( pid INTEGER, 282 | prof ssn CHAR(10), 283 | PRIMARY KEY (pid, prof ssn), 284 | FOREIGN KEY (prof ssn) REFERENCES Professors, 285 | FOREIGN KEY (pid) REFERENCES Projects ) 286 | Observe that we cannot enforce the participation constraint for Projects in the 287 | Work In table without check constraints or assertions in SQL. 288 | 289 | 10. CREATE TABLE Supervises ( prof ssn CHAR(10), 290 | grad ssn CHAR(10), 291 | pid INTEGER, 292 | PRIMARY KEY (prof ssn, grad ssn, pid), 293 | FOREIGN KEY (prof ssn) REFERENCES Professors, 294 | FOREIGN KEY (grad ssn) REFERENCES Graduates, 295 | FOREIGN KEY (pid) REFERENCES Projects ) 296 | 297 | Note that we do not need an explicit table for the Work Proj relation since every time a Graduate works on a Project, he or she must have a Supervisor. 298 | *** 299 | ### Exercise 3.14 300 | Consider the scenario from Exercise 2.4, where you designed am ER diagram for a company database. Write SQL statements to create the corresponding relations and capture as many of the constraints as possible. If you cannot capture some constraints, explain why. 301 | 302 | ### `Answer` 303 | 304 | *** 305 | ### Exercise 3.15 306 | Consider the Notown database from Exercise 2.5. You have decided to recommend that Notown use a relational database system to store company data. Show the SQL statements for creating relations corresponding to the entity sets and relationship sets in your design. Identify any constraints in the ER diagram that you are unable to capture in 307 | the SQL statements and briefly explain why you could not express them. 308 | 309 | ### `Answer` 310 | The following SQL statements create the corresponding relations. 311 | 1. CREATE TABLE Musicians ( ssn CHAR(10), 312 | name CHAR(30), 313 | PRIMARY KEY (ssn)) 314 | 2. CREATE TABLE Instruments ( instrId CHAR(10), 315 | dname CHAR(30), 316 | key CHAR(5), 317 | PRIMARY KEY (instrId)) 318 | 3. CREATE TABLE Plays ( ssn CHAR(10), 319 | instrId INTEGER, 320 | PRIMARY KEY (ssn, instrId), 321 | FOREIGN KEY (ssn) REFERENCES Musicians, 322 | FOREIGN KEY (instrId) REFERENCES Instruments ) 323 | 4. CREATE TABLE Songs Appears ( songId INTEGER, 324 | author CHAR(30), 325 | title CHAR(30), 326 | albumIdentifier INTEGER NOT NULL, 327 | PRIMARY KEY (songId), 328 | FOREIGN KEY (albumIdentifier) 329 | References Album Producer, 330 | 5. CREATE TABLE Telephone Home ( phone CHAR(11), 331 | address CHAR(30), 332 | PRIMARY KEY (phone), 333 | FOREIGN KEY (address) REFERENCES Place, 334 | 6. CREATE TABLE Lives ( ssn CHAR(10), 335 | phone CHAR(11), 336 | address CHAR(30), 337 | PRIMARY KEY (ssn, address), 338 | FOREIGN KEY (phone, address) 339 | References Telephone Home, 340 | FOREIGN KEY (ssn) REFERENCES Musicians ) 341 | 7. CREATE TABLE Place ( address CHAR(30) ) 342 | 8. CREATE TABLE Perform ( songId INTEGER, 343 | ssn CHAR(10), 344 | PRIMARY KEY (ssn, songId), 345 | FOREIGN KEY (songId) REFERENCES Songs, 346 | FOREIGN KEY (ssn) REFERENCES Musicians ) 347 | 9. CREATE TABLE Album Producer ( albumIdentifier INTEGER, 348 | ssn CHAR(10), 349 | copyrightDate DATE, 350 | speed INTEGER, 351 | title CHAR(30), 352 | PRIMARY KEY (albumIdentifier), 353 | FOREIGN KEY (ssn) REFERENCES Musicians ) 354 | 355 | 1. CREATE TABLE Musicians ( ssn CHAR(10), 356 | name CHAR(30), 357 | PRIMARY KEY (ssn)) 358 | 2. CREATE TABLE Instruments ( instrId CHAR(10), 359 | dname CHAR(30), 360 | key CHAR(5), 361 | PRIMARY KEY (instrId)) 362 | 3. CREATE TABLE Plays ( ssn CHAR(10), 363 | instrId INTEGER, 364 | PRIMARY KEY (ssn, instrId), 365 | FOREIGN KEY (ssn) REFERENCES Musicians, 366 | FOREIGN KEY (instrId) REFERENCES Instruments ) 367 | 4. CREATE TABLE Songs Appears ( songId INTEGER, 368 | author CHAR(30), 369 | title CHAR(30), 370 | albumIdentifier INTEGER NOT NULL, 371 | PRIMARY KEY (songId), 372 | FOREIGN KEY (albumIdentifier) 373 | References Album Producer, 374 | 5. CREATE TABLE Telephone Home ( phone CHAR(11), 375 | address CHAR(30), 376 | PRIMARY KEY (phone), 377 | FOREIGN KEY (address) REFERENCES Place, 378 | 6. CREATE TABLE Lives ( ssn CHAR(10), 379 | phone CHAR(11), 380 | address CHAR(30), 381 | PRIMARY KEY (ssn, address), 382 | FOREIGN KEY (phone, address) 383 | References Telephone Home, 384 | FOREIGN KEY (ssn) REFERENCES Musicians ) 385 | 7. CREATE TABLE Place ( address CHAR(30) ) 386 | 8. CREATE TABLE Perform ( songId INTEGER, 387 | ssn CHAR(10), 388 | PRIMARY KEY (ssn, songId), 389 | FOREIGN KEY (songId) REFERENCES Songs, 390 | FOREIGN KEY (ssn) REFERENCES Musicians ) 391 | 9. CREATE TABLE Album Producer ( albumIdentifier INTEGER, 392 | ssn CHAR(10), 393 | copyrightDate DATE, 394 | speed INTEGER, 395 | title CHAR(30), 396 | PRIMARY KEY (albumIdentifier), 397 | FOREIGN KEY (ssn) REFERENCES Musicians ) 398 | 399 | *** 400 | ### Exercise 3.16 401 | Translate your ER diagram from Exercise 2.6 into a relational schema, and show the SQL statements needed to create the relations, using only key and null constraints. If your translation cannot capture any constraints in the ER diagram, explain why. 402 | In Exercise 2.6, you also modified the ER diagram to include the constraint that tests on a plane must be conducted by a technician who is an expert on that model. Can you modify the SQL statements defining the relations obtained by mapping the ER diagram to check this 403 | constraint? 404 | 405 | ### `Answer` 406 | 407 | *** 408 | ### Exercise 3.17 409 | Consider the ER diagram that you designed for the Prescriptions-R-X chain of pharmacies in Exercise 2.7. Define relations corresponding to the entity sets and relationship sets in your design using SQL. 410 | 411 | ### `Answer` 412 | The statements to create tables corresponding to entity sets Doctor, 413 | Pharmacy, and Pharm co are straightforward and omitted. The other required tables 414 | can be created as follows: 415 | 1. CREATE TABLE Pri Phy Patient ( ssn CHAR(11), 416 | name CHAR(20), 417 | age INTEGER, 418 | address CHAR(20), 419 | phy ssn CHAR(11), 420 | PRIMARY KEY (ssn), 421 | FOREIGN KEY (phy ssn) REFERENCES Doctor ) 422 | 2. CREATE TABLE Prescription ( ssn CHAR(11), 423 | phy ssn CHAR(11), 424 | date CHAR(11), 425 | quantity INTEGER, 426 | trade name CHAR(20), 427 | pharm id CHAR(11), 428 | PRIMARY KEY (ssn, phy ssn), 429 | FOREIGN KEY (ssn) REFERENCES Patient, 430 | FOREIGN KEY (phy ssn) REFERENCES Doctor, 431 | FOREIGN KEY (trade name, pharm id) 432 | References Make Drug) 433 | 3. CREATE TABLE Make Drug (trade name CHAR(20), 434 | pharm id CHAR(11), 435 | PRIMARY KEY (trade name, pharm id), 436 | FOREIGN KEY (trade name) REFERENCES Drug, 437 | FOREIGN KEY (pharm id) REFERENCES Pharm co) 438 | 4. CREATE TABLE Sell ( price INTEGER, 439 | name CHAR(10), 440 | trade name CHAR(10), 441 | PRIMARY KEY (name, trade name), 442 | FOREIGN KEY (name) REFERENCES Pharmacy, 443 | FOREIGN KEY (trade name) REFERENCES Drug) 444 | 5. CREATE TABLE Contract ( name CHAR(20), 445 | pharm id CHAR(11), 446 | start date CHAR(11), 447 | end date CHAR(11), 448 | text CHAR(10000), 449 | supervisor CHAR(20), 450 | PRIMARY KEY (name, pharm id), 451 | FOREIGN KEY (name) REFERENCES Pharmacy, 452 | FOREIGN KEY (pharm id) REFERENCES Pharm co) 453 | 454 | The statements to create tables corresponding to entity sets Doctor, 455 | Pharmacy, and Pharm co are straightforward and omitted. The other required tables 456 | can be created as follows: 457 | 1. CREATE TABLE Pri Phy Patient ( ssn CHAR(11), 458 | name CHAR(20), 459 | age INTEGER, 460 | address CHAR(20), 461 | phy ssn CHAR(11), 462 | PRIMARY KEY (ssn), 463 | FOREIGN KEY (phy ssn) REFERENCES Doctor ) 464 | 2. CREATE TABLE Prescription ( ssn CHAR(11), 465 | phy ssn CHAR(11), 466 | date CHAR(11), 467 | quantity INTEGER, 468 | trade name CHAR(20), 469 | pharm id CHAR(11), 470 | PRIMARY KEY (ssn, phy ssn), 471 | FOREIGN KEY (ssn) REFERENCES Patient, 472 | FOREIGN KEY (phy ssn) REFERENCES Doctor, 473 | FOREIGN KEY (trade name, pharm id) 474 | References Make Drug) 475 | 3. CREATE TABLE Make Drug (trade name CHAR(20), 476 | pharm id CHAR(11), 477 | PRIMARY KEY (trade name, pharm id), 478 | FOREIGN KEY (trade name) REFERENCES Drug, 479 | FOREIGN KEY (pharm id) REFERENCES Pharm co) 480 | 4. CREATE TABLE Sell ( price INTEGER, 481 | name CHAR(10), 482 | trade name CHAR(10), 483 | PRIMARY KEY (name, trade name), 484 | FOREIGN KEY (name) REFERENCES Pharmacy, 485 | FOREIGN KEY (trade name) REFERENCES Drug) 486 | 5. CREATE TABLE Contract ( name CHAR(20), 487 | pharm id CHAR(11), 488 | start date CHAR(11), 489 | end date CHAR(11), 490 | text CHAR(10000), 491 | supervisor CHAR(20), 492 | PRIMARY KEY (name, pharm id), 493 | FOREIGN KEY (name) REFERENCES Pharmacy, 494 | FOREIGN KEY (pharm id) REFERENCES Pharm co) 495 | 496 | *** 497 | ### Exercise 3.18 498 | Write SQL statements to create the corresponding relations to the ER diagram you designed for Exercise 2.8. If your translation cannot capture any constraints in the ER diagram, explain why. 499 | 500 | ### `Answer` 501 | 502 | *** 503 | ### Exercise 3.19 504 | Briefly answer the following questions based on this schema: 505 | Emp (eid: integer, ename: string, age: integer, salary: real) 506 | Works (eid: integer, did: integer, peLtime: integer) 507 | Dept (did: integer, budget: real, managerid: integer) 508 | 1. Suppose you have a view SeniorEmp defined as follows: 509 | CREATE VIEW SeniorEmp (sname, sage, salary) 510 | AS SELECT E.ename, Kage, E.salary 511 | FROM Emp E 512 | WHERE Kage > 50 513 | Explain what the system will do to process the following query: 514 | SELECT S.sname 515 | FROM SeniorEmp S 516 | WHERE S.salary > 100,000 517 | 2. Give an example of a view on Emp that could be automatically updated by updating Emp. 518 | 3. Give an example of a view on Emp that would be impossible to update (automatically) and explain why your example presents the update problem that it does. 519 | 520 | ### `Answer` 521 | The answer to each question is given below. 522 | 1. The system will do the following: 523 | SELECT S.name 524 | FROM ( SELECT E.ename AS name, E.age, E.salary 525 | FROM Emp E 526 | WHERE E.age > 50 ) AS S 527 | WHERE S.salary > 100000 528 | 2. The following view on Emp can be updated automatically by updating Emp: 529 | CREATE VIEW SeniorEmp (eid, name, age, salary) 530 | AS SELECT E.eid, E.ename, E.age, E.salary 531 | FROM Emp E 532 | WHERE E.age > 50 533 | 3. The following view cannot be updated automatically because it is not clear which 534 | employee records will be affected by a given update: 535 | CREATE VIEW AvgSalaryByAge (age, avgSalary) 536 | AS SELECT E.eid, AVG (E.salary) 537 | FROM Emp E 538 | GROUP BY E.age 539 | 540 | 541 | 542 | *** 543 | ### Exercise 3.20 544 | Consider the following schema: 545 | Suppliers (sid: integer, sname: string, address: string) 546 | Parts (pid: integer, pname: string, color: string) 547 | Catalog(sid: integer, pid: integer, cost: real) 548 | 549 | The Catalog relation lists the prices charged for parts by Suppliers. Answer the following questions: 550 | 1. Give an example of an updatable view involving one relation. 551 | 2. Give an example of an updatable view involving two relations. 552 | 3. Give an example of an insertable-into view that is updatable. 553 | 4. Give an example of an insertable-into view that is not updatable. 554 | 555 | ### `Answer` 556 | 557 | 1. The system will do the following: 558 | SELECT S.name 559 | FROM ( SELECT E.ename AS name, E.age, E.salary 560 | FROM Emp E 561 | WHERE E.age > 50 ) AS S 562 | WHERE S.salary > 100000 563 | 2. The following view on Emp can be updated automatically by updating Emp: 564 | CREATE VIEW SeniorEmp (eid, name, age, salary) 565 | AS SELECT E.eid, E.ename, E.age, E.salary 566 | FROM Emp E 567 | WHERE E.age > 50 568 | 3. The following view cannot be updated automatically because it is not clear which 569 | employee records will be affected by a given update: 570 | CREATE VIEW AvgSalaryByAge (age, avgSalary) 571 | AS SELECT E.eid, AVG (E.salary) 572 | FROM Emp E 573 | GROUP BY E.age 574 | 575 | *** 576 | --------------------------------------------------------------------------------