CS27020: Modelling Persistent Data WORKSHEET: INTEGRITY CONTROL IN RELATIONAL DATABASES Time allowed: 40 minutes Calculators are not allowed in this worksheet. Answer all questions 1. Briefly explain what is meant by each of the following terms. Use suitable examples to illustrate your explanations. Enterprise rule (also known as business rule) Data integrity Integrity constraint Enterprise rule: empirical constraint on real world entities and attributes. Examples: Each pallet contains 5184 bottles (a real example, referring to supplies of an antiviral); a student is normally allowed at most two attempts at a module examination. Data integrity: the data in a database models the real world; the database corresponds to reality. For example, a person has exactly one date of birth. Integrity constraint: a constraint on the values or combinations of values that are allowed to be entered into a database. For example, no two distinct vehicles are allowed to have the same vehicle identification number is a constraint that allows vehicle identification number to identify a particular vehicle record. Page 1 of 7
2. Briefly explain the terms domain, candidate key and foreign key as used in the relational data model. A domain is the set of possible values for an attribute; it is the type of the attribute. In a relational database, a domain is the set of possible values for the cells in a column of a table. A candidate key is a collection of attributes whose combined values are different for each tuple in a relation. A candidate key is also minimal in the sense that no subset of the candidate key will identify tuples in this way. A foreign key is a collection of attributes from one relation that constitutes a candidate key for another relation. The values of the foreign key attributes in the first relation must also be present in some tuple of the second relation. 3. As well as domains, candidate keys and foreign keys, a database management system must provide other kinds of integrity constraints. Why are other kinds of constraint needed? Give an example of a constraint that cannot be modelled using domains, candidate keys and foreign keys. Other kinds of constraint are needed because domains and key constraints are not sufficient to capture all the different kinds of enterprise rule that need to be modelled in a database. For example, the constraint every sheep farmer owns at least one sheep cannot be represented using domains, foreign keys and candidate keys. In general, minimal cardinality constraints (1.. cardinalities) demand more than domains and foreign and candidate keys. 4. What facilities might be provided by a relational database management systems to uphold such constraints? Facilities provided by relational database management systems include checks, assertions, functions, procedures and triggers. 5. Integrity constraints can be enforced in application code, or by the database management system. Why is it usually better to have the database management system enforce integrity constraints? When constraints are enforced by applications, every application that can modify the table(s) in question must implement them. It can be difficult to ensure that constraints are enforced when applications are written over a long period of time. Page 2 of 7
6. A role playing game includes a table of data about characters. The table has an attribute kind. The values that can be given to kind are monster, wizard, hero, seer. a) Write SQL statements to enforce this in three different ways: (i) using an enumerated data type; CREATE TYPE character_kind AS ENUM ( monster, wizard, hero, seer ); CREATE TABLE character (..., kind character_kind, etc ); (ii) using a check constraint; CREATE TABLE character (..., kind text CHECK (kind in ( monster, wizard, hero, seer )), etc ); (iii) using a foreign key reference. CREATE TABLE character_kind ( kind text primary key ); INSERT INTO character_kind (kind) VALUES (monster), (wizard), ( hero), ( seer ); CREATE TABLE character (..., kind text REFERENCES character_kind(kind) ); Page 3 of 7
b) What are the relative merits of these three approaches? Points including the following are likely: Enumerated type and foreign key reference allow constraint to be implemented once, and reused in many tables. Both these approaches allow a simple query to display values to be entered into the table via form widget. However, acceptable values are not immediately visible in the table definition when either of these approaches is used. Cannot use native string operators such as like with enumerated types (this restriction is not true of every DBMS.) Using a foreign key reference facilitates modification of the list of acceptable values. A check constraint is immediately visible in a table definition. Values shown in the check constraint can be used with native string operators. A check constraint is not available for use in other tables. Page 4 of 7
7. In a supermarket chain, Bubble Beans, popular with toast lovers, are only moved between the warehouse and the stores on pallets of 400 cartons. A simplified version of the warehouse stock relation is as follows: warehouse stock{ item code:integer, item name:text, quantity:integer} where item code is the primary key. Write an SQL definition for this table. Your definition should support the rules if two items have the same item name, then they also have the same item code, and quantity is always a multiple of 400 if item name is Bubble Beans. CREATE TABLE warehouse_stock( item_code integer PRIMARY KEY, item_name text, quantity integer, UNIQUE (item_code,item_name), CONSTRAINT valid_quantity CHECK ((NOT(item_name LIKE Bubble BeanS )) OR (mod(quantity,400) = 0))); Page 5 of 7
8. Llandwp Music Club caters for for singing ensembles. Each singer sings with at least one and at most three ensembles. Each ensemble has at least two and at most twenty singers. These enterprise rules are illustrated in the diagram: Singer Group 2..20 1..3 a) Suppose application code is to be written to create and delete singer and ensemble records, and to move singers from ensemble to ensemble. List the checks that the application code would need to include in order to enforce the enterprise rules shown in the diagram. A good answer will include points like these: when creating a singer, also place the singer in a ensemble; before adding a singer to a ensemble, check whether or not the singer is already a member of three ensembles and that the ensemble has not already got twenty singers; before deletinging a singer, check that this will not cause an ensemble to be reduced to fewer than two singers; when creating a new ensemble, add at least two singers to the ensemble; before deleting an ensemble, remove all the singers from that ensemble; this may involve deleting singers, if they are not members of a second ensemble; when moving a singer from one ensemble to another, check that the first ensemble is not left with fewer than two singers, and the second will not have more than twenty singers. Page 6 of 7
b) Outline how you would enforce the enterprise rules using SQL functions and triggers. Assume that a database has been created that includes three tables: one for the singers, one for the ensembles and one for the relationship between singers and ensembles. There is no need to write the procedural SQL code; an outline in English is sufficient. A good answer should identify the necessary triggers and functions. If the following points were fleshed out they would form a very good answer. Functions to check the cardinality constraints; Functions to create and delete new singers and ensembles; A function to change the relationship between singers and ensembles; Triggers that execute the functions to check the cardinality constraints when singers are added or deleted, or moved between ensembles, or when ensembles are created or deleted. Page 7 of 7