Data Modelling and Databases Exercise Session 7: Integrity Constraints 1
Database Design Textual Description Complete Design ER Diagram Relational Schema Conceptual Modeling Logical Modeling Physical Modeling SQL (DDL) Integrity Constraint 2
Integrity Constraints How can we define rules in a DB? 1. Attribute Constraints: intra-table rules 2. Referential Constraints: inter-table rules 3. Triggers: more complex rules Constraints are used to limit the type of data that can be inserted into a table. There exist two ways to specify constraints on a relation: When a table is created: CREATE TABLE After the table is created: ALTER TABLE 3
Integrity Constraints Name NOT NULL UNIQUE PRIMARY KEY FOREIGN KEY CHECK DEFAULT Description This enforces a field to always contain a value. This constraint uniquely identifies each record in a table. An attribute that is unique can be null. A primary key uniquely identifies each record in a table (= UNIQUE + NOT NULL), each table can have only one primary key. A foreign key is a reference that points from one table to the primary key of another table. It prevents actions that would destroy links between tables. [Referential Integrity Constraints] This constraint is used to limit the value range that can be placed in a column. CHECK can limit the values in certain columns based on values in other columns It is used to insert a default value into a column. The default value will be added to all new records, if no other value is specified. 4
Intra-Table Constraints Example: CREATE TABLE User ( id int(11) PRIMARY KEY, group varchar(10) DEFAULT guest', username varchar(20) UNIQUE, password varchar(30) NOT NULL, email varchar(30) CHECK (email LIKE %@% ), ) CONSTRAINT chk_group CHECK group IN ( user, admin, guest ) ALTER TABLE User ADD CONSTRAINT uni_email UNIQUE (email) Use CONSTRAINT statement to name your constraints (optional) 5
Referential Constraints A foreign key is a referential constraint between two tables. This is reference that points from one (or more) attribute(s) to the primary key of another table. A foreign key value must point to an existing tuple. If this is not the case, its value must be NULL, i.e. a FK value cannot point to a non-existing row! uid username 1 jribon 3 prtrem 4 lucasa cid uid comment 1 1 Hello World! 2 1 Students are smart. 3 2 I love DB!! 6
Inter-Table Constraints Example: CREATE TABLE User ( id int(11) PRIMARY KEY, group_id int(11) FOREIGN KEY (group_id) REFERENCES Group(id), username varchar(20), password varchar(30) ) CREATE TABLE Group ( id int(11) PRIMARY KEY, name varchar(10) ) 7
Referential Actions Referential actions ensure data integrity if a row (in a referenced table) is deleted or updated. This defines what should happens if we want to delete (or update) a row that is still referenced in another table. uid username 1 jribon 2 prtrem 3 lucasa? cid uid comment 1 1 Hello World! 2 1 Students are smart. 3 2 To be or not to be. Referenced Table Referencing Table 8
Referential Actions SQL-2003 specifies 5 different referential actions: Name CASCADE RESTRICT NO ACTION SET NULL SET DEFAULT Description The respective rows of the referencing table are deleted or updated together with the referenced row. A row cannot be deleted or updated as long as there is a reference to it from a referencing table. Integrity constraint is checked before executing the statement. [Pessimistic: expect violation] As RESTRICT, but integrity constraint is checked at the end of the UPDATE or DELETE statement. [Optimistic: assume no violation] The foreign key value (in the referencing row) is set to NULL when the referenced row is updated or deleted. Note: foreign key must be null-able. Similar to SET NULL, the foreign key is set to the column s default value when the referenced row is updated or deleted. 9
Inter-Table Constraints Example: CREATE TABLE User ( id int(11) PRIMARY KEY, group_id int(11), username varchar(20), password varchar(30) ) ALTER TABLE User ADD CONSTRAINT `fk_user2group` FOREIGN KEY (group_id) REFERENCES Group(id), ON DELETE RESTRICT ON UPDATE CASCADE; Foreign Key constraint with referential actions 10
Question 1.1 Which one does not allow NULL to be inserted? o UNIQUE o NOT NULL o FOREIGN KEY o PRIMARY KEY o CHECK 11
Question 1.1 Which one does not allow NULL to be inserted? o UNIQUE -> In SQL all NULLs are unique NOT NULL o FOREIGN KEY -> Allowed to be NULL PRIMARY KEY -> Can t be NULL CHECK -> Any NULL will cause it to fail 12
Question 1.2 True or False? 13
Question 1.2 True or False? 14
Question 2.1 Schema: Initial contents: What happens when we run the following queries? 15
Question 2.1 Schema: Initial contents: 16
Question 2.1 Schema: Initial contents: Allowed! Not allowed! id 2 already exists. 17
Question 2.1 Schema: Initial contents: 18
Question 2.1 Schema: Initial contents: Allowed! Allowed! 19
Question 2.1 Schema: Initial contents: 20
Question 2.1 Schema: Initial contents: Not allowed! ON DELETE NO ACTION Allowed! ON UPDATE SET NULL 21
Question 2.1 Schema: Initial contents: 22
Question 2.1 Schema: Initial contents: Allowed! ON DELETE CASCADE 23
Question 4 Which statements are true? 24
Question 4 Which statements are true? 25
Question 5 How can we add the following constraints to the schema? 1. The start date of salary has to be before the end date. 2. The end date in dept_emp table is either NULL (meaning sometime future) or is in the past. 3. One employee can only be the manager of one department at a time. This implies that the date entries in dept_manager table can not have overlaps for a certain employee. 26
Question 5 1. The start date of salary has to be before the end date. 27
Question 5 2. The end date in dept_emp table is either NULL (meaning sometime future) or is in the past. 28
Question 5 3. One employee can only be the manager of one department at a time. 29