CS10610 Databases: Practical 4 2008-2009 Objectives: To set up relationships between tables. To understand referential integrity (definition on Blackboard; Practicals -> Ref Integrity). (a) Use the numbered points of information (requirement specification) to draw an Entity-Relationship (ER) diagram for a system to hold information on Oscar awards. Your E-R diagram should show ENTITIES, RELATIONSHIPS AND PK FIELDS ONLY in addition to the justification for each side of each relationship by adding comments to the diagram. The diagram has been started using the points below 1. A film has many roles. 2. Each role is cast in 1 film 3. Each role is nominated for many awards 4: Each award has many roles nominated to receive it. The year of nomination is recorded and Point 1 whether or not it was successful. Has Film filmId {PK} ..1 Role ..* Cast In Point 4 Point 4 Point 2 ..* Nominated to Year, Successful? Award shortCat {PK} ..* Nominated for Point 3 Study the above example. Copy down the E-R diagram and using the information below, add to it. The slides from the lectures are available on the web page to help you if needed. 5. 6. 7. 8. An Actor accepts many roles. The actor’s name is recorded and their union number (unique). Each role is given to only 1 actor For each role, the role name is recorded. Will role name be unique? If not, it should NOT be added to your E-R diagram. A film may be nominated for more than one award. For each film, the film ID (unique), title and director is recorded. The year of nomination and whether or not it was successful is recorded. An award has many films nominated to receive it. The category of each award is recorded, and the short category (unique). Convert your E-R diagram into a list of tables following the rules given in the lecture slides, starting with the entities involved in any one-to-many relationships. Use the convention below: TableName {primaryKey, field1, field2, foreign key*} Remember that not all tables will have a foreign key (the linking field). Make sure that the primary keys and foreign keys are indicated in your list of tables as shown above. SIGN OFF POINT (1 of 3): E-R diagram Get this work signed off. htp Updated 13/08/2008 Page 1 of 4 CS10610 Databases: Practical 4 2008-2009 (b) Go to the CS106 page on Blackboard and copy the database oscars_prac4.accdb from the Practicals folder to the d:\docs directory. Open the database oscars_prac4 on the d drive in Access. Read through Tables Guide to table relationships. Read the introduction and the sections on Types of table relationships. Have a look at the relationships window of the oscars_prac4 database (Database Tools tab). Use the lecture notes on the web to help answer the following questions: Are the foreign keys shown in your E-R diagram? ............................................................................................................................................ Where do the foreign keys first appear in the exercise? ............................................................................................................................................ Why do they appear only at this point? ............................................................................................................................................ ............................................................................................................................................ Are the foreign keys shown in the relationships window? ............................................................................................................................................ IMPORTANT ASSESSMENT INFORMATION Note that the Access relationships window is NOT an E-R diagram. The relationships have already been implemented and it shows the foreign keys. Drawing a diagram such as this in an exam would lose you a lot of marks. E-R diagrams only show the primary keys. They are NOT the same as class diagrams in Java. A printout of the relationships window will NOT be acceptable as an E-R diagram in the assignment. You should submit a true E-R diagram such as those shown in the lecture notes or as illustrated in exercise a). (c) Your objective now is to amend the design to record the cinema at which some of the films are currently being shown. Use the following information to update your E-R diagram from a). Note particularly the assumptions (not realistic but they keep the practical shorter!) A cinema shows only 1 film. A film can be showing at many cinemas. For each cinema the cinema name and town are recorded. You may assume that the cinema, town combination is unique. Amend your list of tables to include the new information. (d) Create a new table in the Oscars database to hold the cinema information. The Film ID field (foreign key) should be of type number and Field Size long integer. (It must match the Film ID field in the Films and Directors table; matching fields must be of the same data type, except that a long integer number is allowed as a match for an Autonumber.) When you have designed your table and set the primary key, save the design and close the table. You will not be able to create a relationship while the table is open. htp Updated 13/08/2008 Page 2 of 4 CS10610 Databases: Practical 4 2008-2009 The FilmsAndDirectors table shows an acceptable use of an autonumber. All films have some kind of identifying number. The film table merely uses the autonumber facility as a tool for entering data. Using an Autonumber in your cinema table would not be acceptable as there is already a unique field combination (cinema and town) (e) Read through Tables Guide to table relationships Create a table relationship. Now open the Relationships Window if not visible. Add your new table to the window. Set up an appropriate relationship between your new table and the Films and Directors table. (f) Read through Tables Guide to table relationships Understanding referential integrity. You should Enforce Referential Integrity now, before you enter any data. Don’t set the Cascade Update … and Cascade Delete… checkboxes at this point. (g) Add a few records to your new table. Use your imagination for cinemas if you wish, but the Film ID should come from those in the Films and Directors table. Your records should illustrate the fact that a film can be shown at many cinemas. (h) Start a new query in design view, adding the cinema and the FilmsAndDirectors table. Notice that the link that you set up in the relationships window between these two tables is automatically added in the query window. Design a new query that will show the filmID, title, director, cinema and town for a cinema of the user's choice. Inspect the results. The system uses the data contained in the foreign key - primary key (FilmId) fields to look up the appropriate data from the FilmsAndDirectors table SIGN OFF POINT (2 of 3): Parts (b)-(h) You may continue to work on until a demonstrator becomes free. (i) Close your database file and copy it onto the M drive. Then complete the final section UNDERSTANDING REFERENTIAL INTEGRITY (j) A database has integrity when you can trust the data within it. The following experiment is to illustrate how referential integrity works and to show the reason for some common problems. Work in pairs for this exercise but you should both note down the answers to the questions. Start a new blank database. Create two tables, Branch {branchCode, branchName} Staff { staffNo, surname, branchCode* } (branchCode is a single character text field) htp Don’t set any primary keys yet Updated 13/08/2008 Page 3 of 4 CS10610 Databases: Practical 4 2008-2009 Try to set up a relationship between the two tables using branchCode, enforcing referential integrity. What happens? ......................................................................................................... Why? ........................................................................................................... Set branchCode as a primary key on Branch and close the table. Now set up a relationship between Branch and Staff, using the branchCode field, enforcing referential integrity Open both tables. Add these records to the branch table: branchCode, branchName. A Aber B Bangor C Cardiff D Swansea Now try to allocate a member of staff to a non-existent branch by adding the following record to the Staff table staffNo surname branchCode 1 Bloggs E What happens ........................................................................................................... why? ........................................................................................................... Add the following valid records to the Staff table. staffNo surname branchCode 1 Bloggs A 2 Evans B 3 Jones A 4 Smith C Can you delete Smith’s record? ........................................................ Why? ................................................................................................ Can you delete the Bangor record from the branch table? ............... Why? ................................................................................................. Can you delete the Swansea record from the Branch table? ............ Is it referred to by records in the staff table? ................................... SIGN OFF POINT (3 of 3): Referential integrity exercise Once the work is signed off, close your database file and copy it onto the M drive. htp Updated 13/08/2008 Page 4 of 4