Database Management - 2013 Model Answers 1. a. A cyclic relationship type (also called recursive) is a relationship type between two occurrences of the same entity type. With each entity type in a cyclic relationship type we associate a role, represented by labels on lines in an ERD. Name Address NI# Person Phones husband wife Married Roles are specified by husband and wife labels. The attributes of the table for entity type E should be those of E with the same primary key. The attributes of the table for relationship type R should be the role names, along with any other attributes for the relationship type. The primary key should be the composite key comprising both role attributes. COIY028H6 Page 1 of 9 c Birkbeck College 2013
b. A relation r over R satisfies X Y if whenever two rows in r have the same X-value they also have the same Y -value. An FD X Y holds on relation schema R if every allowable (legal) relation r over R satisfies X Y. Two FDs which are satisfied in the example relation are from Title Year, Year Title, Genre Title, and Genre Year. Two FDs which are violated by the example relation are from Title Genre, Title StarName, StarName Title, StarName Genre, StarName Year, Year Genre, Year StarName, Genre StarName, StarName, Year Genre, Title, Year Genre, and Title, Year, Genre StarName. The only FD which might hold in the example relation schema is Title Year. However, this assumes that there are no remakes of films with the same title. COIY028H6 Page 2 of 9 c Birkbeck College 2013
c. In embedded SQL, a preprocessor turns SQL statements into procedure calls that fit with the surrounding host-language code. All embedded SQL statements begin with EXEC SQL, so the preprocessor can find them easily. To connect SQL and the host-language program, the two parts must share some variables. In SQL, the shared variables must be preceded by a colon. They may be used as constants provided by the host-language program. They may get values from SQL statements and pass those values to the host-language program. In the host language, shared variables behave like any other variable. Example: EXEC SQL BEGIN DECLARE SECTION; char thebar[21], thebeer[21]; float theprice; EXEC SQL END DECLARE SECTION; /* obtain values for thebar and thebeer */ EXEC SQL SELECT price INTO :theprice FROM Sells WHERE bar = :thebar AND beer = :thebeer; /* do something with theprice */ [Total 33 marks] COIY028H6 Page 3 of 9 c Birkbeck College 2013
2. a. In the foreign key definition, we can specify either ON DELETE CASCADE ON UPDATE CASCADE for actions on the referenced relation to cascade to the referencing relation, or ON DELETE SET NULL ON UPDATE SET NULL for the referencing relation s values to be set to null. Cascading deletes or updates is appropriate when changes to an entity need to be propagated throughout the database. Setting values to null is appropriate when we want the referencing entities to become independent after updates. b. (AB) + = ABCD (C) + = CD (BD) + = ABCD (E) + = CDE One BCNF decomposition would be as follows. Decompose ABCDE into CD and ABCE based on the FD C D since C is not a superkey. Then decompose ADCE into ABC and ABE based on AB C since AB is not a superkey. The final decomposition is thus CD, ABC and ABE. c. i. A domain dependency is a constraint on the allowed values for some attribute. For example, age might be restricted to being between 18 and 75. ii. An ISA relationship type in the ER model allows for modelling of generalisation or specialisation hierarchies. For example, one can state that student ISA person. Attributes are inherited down the hierarchy. iii. A natural join operation is a join over relations with common attribute names. The join condition is automatically taken to be checking for equality between pairs of attivute values with the same name. COIY028H6 Page 4 of 9 c Birkbeck College 2013
iv. Dependency preservation refers to whether or not a decomposition allows for all original functional dependencies to be checked by the database system by considering only each relation individually, i.e., no joins are needed to check whether or not dependencies are preserved when applying an update. v. Stored procedures correspond to code in a specialized language which is stored in the database itself (e.g., PSM, PL/SQL). PSM, or persistent stored modules, allows us to store procedures as database schema elements. PSM is a mixture of conventional statements (if, while, etc.) and SQL. Lets us do things we cannot do in SQL alone. We can CREATE PROCEDUREs, e.g., and then CALL them from within SQL queries. [Total 33 marks] COIY028H6 Page 5 of 9 c Birkbeck College 2013
3. a. The primary key for R1 would be A. The primary key for R2 would be AC. There would be a foreign key constraint on A in R2 referencing R1. The A value in relation R1 might be updated without the A value in R2 being similarly updated. A decomposition of R(A,B,C) into R1(A,B) and R3(B,C) would be lossy, i.e., the join of R1 and R3 might return tuples not in the original R from which R1 and R3 were decomposed. b. The inner join would produce person location name location Alice Cambridge St John s Chop House Cambridge Carol Cambridge St John s Chop House Cambridge Rows from person with value Cambridge for location join with rows from restaurant with value Cambridge for location. Rows with a null value for location do not join because null is not equal to null. In addition, location Hitchin does not appear in the person table, so does not join with any row. Left-outer join would include a row with person Bob and nulls for all other attributes. Right-outer join would include rows for MacDonald s and Pizza Express with nulls for person and (first) location. COIY028H6 Page 6 of 9 c Birkbeck College 2013
c. First normal form means that each attribute value in a relation should be atomic as far as the database system is concerned. Object-relational systems allow attributes to be defined using array types and multiset types. An example is given below, where the authors attribute can contain up to 10 strings, and keywords is a multiset of strings. CREATE TYPE Book AS ( title varchar(20), authors varchar(20) array [10], keywords varchar(20) multiset) NOT FINAL; [Total 33 marks] COIY028H6 Page 7 of 9 c Birkbeck College 2013
Page 8 of 5 4. a. i. A key is given by {Track, Artist, Role}. This functionally determines all attributes because Track TrackTitle, Track Album, and Album AlbumTitle. No subset of these attributes determines the full set. ii. The three FDs form a canonical cover. Each then forms a relation schema as follows: (Track, TrackTitle), (Album, AlbumTitle) and (Track, Album). Then we add another relation schema which forms a key, namely, (Track, Artist, Role). b. Transactions handle the problem that multiple programs accessing the database might try to modify the same data at the same time, leading to unpredictable results. Dirty data refers to data written by a transaction that has not yet committed. A dirty read operation is a read of dirty data written by another transaction. The risk is that the transaction that wrote the data may abort which would result in non-serializable behaviour. This may or may not be a problem for the reading transaction. If it performing some operation which not not require precise accuracy at that particular moment in time, it would not be a problem. c. Updating a database through a view can be problematic because such an update may be ambiguous, that is, it may not be obvious what updates to apply to the tables on which the view is defined. An SQL view is said to be updatable if the query defining the view satisfies the following: The FROM clause has only one database relation. The SELECT clause contains only attribute names of the relation (no expressions, aggregates, or DISTINCT). Any attribute not listed in the SELECT clause can be set to null. There is no GROUP BY clause. [Total 33 marks]
c Birkbeck College 2010 COIY028U END OF PAPER COIY028H6 Page 9 of 9 c Birkbeck College 2013