Professional Documents
Culture Documents
Chapter 08-1 Database Constraints Part 1
Chapter 08-1 Database Constraints Part 1
Chapter 8
Business Rules become Database Constraints
• Business rules are determined during data modeling, usually the requirements analysis phase
– Structural business rules become database constraints attached to columns in tables
– Procedural business rules must be programmed
• Database constraints are developed from business rules during the relational model
– Restrictions on data in the database
– Sometimes referred to as integrity constraints
2
Database Constraints
• No constraints are required, although most tables contain a
primary key
• As many constraints as necessary are specified for a table to
ensure data integrity
• Once a constraint has been defined for a table, the constraint is
enforced for all database updates
Constraint Types
4
Data Type/Length Constraint
• Restricts the type and length of data in a column
5
Column Optionality (NOT NULL) Constraint
• Indicates the optionality of the column data
6
Inserting Rows with NULL Values
7
Verify NULL values from INSERTs
8
UPDATE Statement
Set Null-Capable Column to NULL
9
DEFAULT Value
• A column in a table can be given a default value
• Prevents NULL values from entering a columns if a row is
inserted without a specified value
• Default value can be a literal value, an expression, or a SQL
function, such as CURRENT_DATE and USER, but the value
cannot be the name of another column
• Default value must match the data type of the column
10
DEFAULT Value
11
DEFAULT Value
12
CURRENT_DATE
13
UPDATE Statement
Set Column to Default Value
14
Constraint Names
• Most constraints should be given a name
– NULL & DEFAULT constraints may be the only exception
– A data type is assigned to a column and is not assigned a constraint
name
• If a name is not specified, the DBMS generates an ambiguous
name that is difficult to understand and locate
15
Suffix for Constraint Names
Constraint Suffix
UNIQUE _uk
CHECK _ck
16
Existence Integrity
PRIMARY KEY Constraint
• A primary key serves as the unique identifier for rows in the
table
• A table can not have more than one (1) primary key constraint
• Composite primary keys – a primary key that is composed of
more than one column
• Primary key column(s) cannot contain NULL
• With a primary key constraint, the DBMS blocks any attempt to
insert or update a row that would cause two rows in the same
table to have identical values for their primary key column
17
Define Primary Key Constraint
• Primary Key constrains can be defined:
– At the column level with the CREATE TABLE command
– At the table level with the CREATE TABLE command
– Using the ALTER TABLE command
18
Primary Key Constraint
Column Level
• Not recommended
– DBMS creates a constraint name that is cryptic
19
Primary Key Constraint
Table Level - No Constraint Name
• Not recommended
– DBMS creates a constraint name that is cryptic
23
Adding a Constraint – Before Data
• Table is created
• Constraints are added before data is inserted into table
• Constraints are enabled immediately
– When data is inserted into table
DBMS verifies data against constraints
Any data that does not pass constraint is rejected
24
Adding a Constraint – After Data
• Table is created
• Data is inserted into table
• Constraints are added after data is inserted
– Constraints are enabled only if all existing rows in the table satisfy the
constraint
– If the current data in the table does not satify the constraint, the DBMS
will reject the new constraint
25
Candidate Key Integrity
UNIQUE Constraint
• Candidate keys - More than one possible set of columns that
may meet the criteria for a primary key
• UNIQUE keyword
• Can be NULL
• Similar to the primary key constraint, the DBMS blocks any
attempt to insert or update a row that would cause two rows in
the same table to have identical, non-null values for the column
listed as a unique constraint
26
Candidate Key Integrity
UNIQUE Constraint
• Candidate keys become UNIQUE key constraints
CREATE TABLE employees
( employee_id INTEGER NOT NULL,
first_name VARCHAR(15) NOT NULL,
middle_initial VARCHAR(1) NOT NULL,
last_name VARCHAR(15) NOT NULL,
soc_sec_nbr INTEGER NOT NULL, -- Mandatory
CONSTRAINT employees_pk
PRIMARY KEY(employee_id) );
ALTER TABLE employees
ADD CONSTRAINT employees_soc_sec_nbr_uk
UNIQUE ( soc_sec_nbr );
27
Candidate Key Integrity
UNIQUE Constraint
• Candidate keys become UNIQUE key constraints
CREATE TABLE orders
( order_no INTEGER NOT NULL,
order_date DATE NOT NULL,
ship_date DATE,
order_total DEC( 7, 2 ) NOT NULL,
crd_authoridy_id INTEGER, -- Optional allowed with UNIQUE constraint
customer_id INTEGER NOT NULL,
CONSTRAINT orders_pk
PRIMARY KEY( order_no) );
ALTER TABLE ORDERS
ADD CONSTRAINT orders_crd_authoridy_id_uk 28
UNIQUE ( crd_authoridy_id );
UNIQUE Constraint Example
• Note that the crd_authority_id column is defined as NULL
capable
• Allowing the crd_authority_id column to be NULL and
specifying the orders_ crd_authority_id_uk constraint together
enforces a business rule that some orders (e.g., those paid by
cash) may exist without a credit authorization number, but any
order that does have a credit authorization number must have a
unique value
29
Referential Integrity
FOREIGN KEY Constraint
• Identifies a relationship between two tables
• The relationship between rows in two tables is expressed by a
FOREIGN KEY in the dependent (child) table that is identical to a
primary key value in some row in the parent table
• The concept of referential integrity states that a row containing
the foreign key may not be added to the table unless a matching
value exists in the primary key column of the parent table
30
FOREIGN KEY Constraint
• For example:
– Rows in the ORDERS table are generally related to rows in the
CUSTOMERS table
– It might be valid for a row in the CUSTOMERS table to exist without any
corresponding rows in the ORDERS table
– It would be invalid for rows in the ORDERS table to not have a
reference to a valid CUSTOMERS row
– The purpose of specifying a foreign key constraint is to ensure that the
ORDERS table never has a row with a (non-null) value in the
customer_id column that has no matching CUSTOMERS row
31
FOREIGN KEY Constraint
CUSTOMERS(customer_id, name, …)
ORDERS(customer_id, product_id, quantity, …)
PRODUCTS(product_id, description, …)
32
FOREIGN KEY Constraint
ALTER TABLE employees
ADD CONSTRAINT employees_work_department_fk
FOREIGN KEY ( work_department )
REFERENCES departments( department_id );
33
FOREIGN KEY Constraint
• Specifies that the work_department column
in the EMPLOYEES table is a foreign key that
references the department_id primary key
column in the DEPARTMENTS table
• Foreign Key constraint name:
– Table name - employees
– Column_name – work_department
– Suffix _fk
34
FOREIGN KEY Constraint – WORK DEPT should be
INTEGER
• The DBMS does not allow an application to insert a
new row into the EMPLOYEES table unless the row's
work_department column contains the value of
some existing department_id value in the
DEPARTMENTS table
35
FOREIGN KEY Constraint
• A foreign key constraint can specify the same table for the
child and parent tables
• Consider an EMPLOYEES table with an employee_id primary
key column and a manager_id column that holds the
employee ID for the person's manager
36
FOREIGN KEY Constraint
CREATE TABLE EMPLOYEES
( employee_id int NOT NULL,
manager_id int,
other column definitions ... ,
PRIMARY KEY ( employee_id ),
CONSTRAINT employees_manager_id_fk
FOREIGN KEY ( manager_id )
REFERENCES EMPLOYEES ( employee_id )
37
Constraint Unit Testing
38
39