DATABASE DESIGN - 1DL400 Spring 2014 2014-01-21 A course on modern database systems http://www.it.uu.se/research/group/udbl/kurser/dbii_vt14/integrity.pdf Tore Risch Uppsala Database Laboratory Department of Information Technology, Uppsala University, Uppsala, Sweden 2014-01-28 1
Database Integrity Constraints (Elmasri/Navathe ch. 5.2 and 8.2) (Padron-McCarthy/Risch ch 12 and 14) Tore Risch Department of Information Technology Uppsala University, Uppsala, Sweden 2014-01-28 2
Integrity constraints (for relational database schemas) Integrity constraints guard against accidental insertion of illegal or inconsistent data into the database, by ensuring that authorized changes to the database do not result in a loss of data consistency. Different kinds of integrity constraints supported in modern RDBMS: 1. Domain constraints (domain = data type of column in table) attribute values for attribute A shall be atomic values from dom(a), c.f. user defined atomic datatype 2. Key constraint candidate keys for a relation must be unique. The key identifies uniquely an entity (object). 2014-01-28 3
Integrity constraints (for relational database schemas) 3. Entity integrity constraint no primary key is allowed to have a null value (the primary key identifies entities/objects) 4. Referential integrity constraint a tuple that refers to another tuple in another relation must refer to an existing tuple (no dangling references to other entities) 5. Semantic integrity constraint e.g. an employee s total work time per week can not exceed 40 hours for all projects taken all together. 2014-01-28 4
Domain constraints Domains created by the: CREATE DOMAIN domain-name [AS] data type E.g. CREATE DOMAIN ssn_type [AS] CHAR(9); Domain constraints are the most elementary form of integrity constraint. The DBMS tests values inserted into the database to ensure that the inserted values do not violate domain restrictions. 2014-01-28 5
Domain constraints cont The check clause in SQL-92 permits domains to have restricted values: For example, the following check clause ensures that hourly-wage values are always greater than 9: create domain hourly-wage numeric(5,2) constraint valuetest check (value >= 9.00) The domain hourly-wage is declared to be a decimal number with 5 digits, 2 of which are after the decimal point The domain has a constraint that ensures that hourly-wage is greater than 9.00. The constraint check is optional and very useful to check integrity constraintys on inserted values in rows. The atomic constraint check is very efficient! 2014-01-28 6
Key constraint & Entity integrity constraint Key constraint - candidate keys for a relation must be unique Dnumber INT PRIMARY KEY; alternative: PRIMARY KEY(Dnumber); Dname VARCHAR(15) UNIQUE; alternative: UNIQUE(Dname); Entity integrity constraint - no primary key is allowed to have a null value Implicitly imposed by the PRIMARY KEY clause. Dname VARCHAR(15) NOT NULL; 2014-01-28 7
Referential integrity in SQL - example create table customer (customer-name char(20) not null, customer-street char(30), customer-city char(30), primary key (customer-name)) create table branch (branch-name char(15) not null, branch-city char(30), assets integer, primary key (branch-name)) 2014-01-28 8
Referential integrity in SQL - example cont d create table account (account-number char(10) not null, branch-name char(15), balance integer, primary key (account-number), foreign key (branch-name) references branch) create table depositor (customer-name char(20) not null, account-number char(10) not null, primary key (customer-name,account-number), foreign key (account-number) references account, foreign key (customer-name) references customer) 2014-01-28 9
Cascading actions in SQL create table account... foreign key (branch-name) references branch on delete cascade on update cascade...) If a tuple in branch is deleted (updated), there is a tuple in account that will also be deleted (updated), i.e. the delete (update) cascades. In addition to cascade there are options of set default and set null. 2014-01-28 10
Cascading actions in SQL cont d If there is a chain of foreign-key dependencies across multiple relations, with on delete cascade specified for each dependency, a deletion or update at one end of the chain can propagate across the entire chain. If a cascading update or delete causes a constraint violation that cannot be handled by a further cascading operation, the system aborts the transaction. As a result, all the changes caused by the transaction and its cascading actions are undone. 2014-01-28 11
Constraints on tuples using check conditions Tuple-based (c.f. domain based) constraints can be defined by a check(p) clause, where P is a predicate E.g. declare and ensure that the values of assets are non-negative for the branch table. create table branch (branch_name char(15) primary key, branch_city char(30), assets integer, check (assets >= 0)) 2014-01-28 12
Assertions An assertion is a predicate expressing a condition that we wish the database always to satisfy. An assertion in SQL-92 takes the form: create assertion <assertion-name> check <predicate> When an modification (insert/delete/update)of the db is made, the system tests it for validity. This testing may introduce a significant amount of overhead; hence assertions should be used with great care. 2014-01-28 13
Assertion example The sum of all loan amounts for each branch must be less than the sum of all account balances at the branch. create assertion sum-constraint check (not exists (select * from branch b where (select sum(amount) from loan where loan.branch-name = b.branch-name) >= (select sum(amount) from account where account.branch-name = b.branch-name))) 2014-01-28 14
Another assertion example Every loan has at least one borrower who maintains an account with a minimum balance of $1000.00. create assertion balance-constraint check (not exists (select * from loan where not exists (select * from borrower, depositor, account where loan.loan-number = borrower.loan-number and borrower.customer-name = depositor.customer-name and depositor.account-number = account.account-number and account.balance >= 1000))) 2014-01-28 15