Integrity 5/5/2019 See scm-intranet
Objectives To develop an understanding of integrity and integrity constraints. To develop a means of expressing constraints 5/5/2019 See scm-intranet
Integrity Constraints Constraints allow only valid combinations of data in database. Need to ensure data manipulation operations are correct. Constraints can be enforced by the DBMS, application program, or user. Universally applied to all users only when defined in DBMS logical schema. 5/5/2019 See scm-intranet
Therefore designer must be able to DBMS should prevent invalid operations. or carry out compensatory actions to mitigate effects of operation. Therefore designer must be able to identify and reject offending operations or identify and implement a compensatory operation to ensure database left in a valid state. 5/5/2019 See scm-intranet
Referential Integrity No primary key attribute(s) should have a null value or hold duplicate values. If a base relation includes a foreign key then every value of the foreign key must be equal to a value of a primary key in a relation. or be wholly null. 5/5/2019 See scm-intranet
SQL-92 e.g.may have this clause for table ‘employee’ for instance- FOREIGN KEY(branch_no) REFERENCES branch ; clause in CREATE and ALTER statements. INSERT/UPDATE rejected if attempt made to create a foreign key value in base relation holding foreign key without a matching candidate in base relation with primary key. E.g. cannot insert a branch_no until that value exists in primary key of table branch 5/5/2019 See scm-intranet
Referential Action FOREIGN KEY clause has ON UPDATE and ON DELETE subclauses- referential action. E.g. FOREIGN KEY (staff_no) REFERENCES staff ON DELETE SET NULL; 5/5/2019 See scm-intranet
Referential Action Possibilities CASCADE- delete from primary key table and matching rows in foreign key table. Deleted rows may have a candidate key used as a foreign key in another table so FOREIGN KEY rules for this may be triggered. RESTRICT-can only delete from primary key table after all matching foreign key references deleted SET NULL/DEFAULT- delete row from primary key table and set foreign key values to NULL or default value. NO ACTION- rejects delete operation. Similar for Update operations 5/5/2019 See scm-intranet
Example Create table Car (reg_no char(8), model varchar(20), engine_size integer, constraint car_key primary key (reg_no) constraint is_costed_at foreign key (engine_size) references hire-rate, constraint is_of_type foreign key (model) references type_of_car… Assumes tables hire_rate and type_of_car exist 5/5/2019 See scm-intranet
What happens to Car if an engine_size is removed/updated in Hire_rate? Is_costed_at Car Type_of_car Hire_Rate Is_of_type 0..* 1..1 Car (registration_no, model, engine_size) Type_of_car (model, make) Hire_rate (engine_size, daily_hire_rate) When considering referential action the questions to ask here would be- What happens to Car if an engine_size is removed/updated in Hire_rate? What happens to Car if a Model is removed/updated in Type_of_Car? What happens to foreign key values in a primary/foreign key relationship if the target (primary key) of a foreign key reference is removed/updated? 5/5/2019 See scm-intranet
uses RESTRICT referential action unless programmed otherwise. SQL Server 7 uses RESTRICT referential action unless programmed otherwise. This means that data/tables can only be deleted/dropped in order i.e. data/tables at many ends (foreign key end ) of relationships first. SQL Server 2000 CASCADE and NO ACTION available explicitly. RESTRICT used if no referential action specified explicitly Cascade does not apply to DROP so order of dropping tables still applies 5/5/2019 See scm-intranet
Dialogue above exists for client-side databases With Server-side referential integrity defined via the SQL schema 5/5/2019 See scm-intranet
Equivalent dialogue server-side 5/5/2019 See scm-intranet
Summary Data Integrity is a necessary feature of any database system. Referential integrity rules are necessary in relational database systems. SQL provides a means of specifying foreign keys and the associated referential actions 5/5/2019 See scm-intranet