Chapter 07 - 1 Database Constraints Part 1
Chapter 07 - 1 Database Constraints Part 1
Chapter 7
Business Rules
• One of the primary goals of data modeling is to ensure that all pieces of data that are required to
run a business are recognized
• Business rules become constraints when database is created
• Structural business rules indicate:
– The type of data to be stored
– How the information elements interrelate
– Rules implemented in an ERD
• Procedural Rules:
– Workflow processes
– Business processes
– Rules Implemented by programming
2
Documenting Rules
• In the process of developing a logical data model with an ER
Diagram, not all business rules can be modeled. Some rules
must be implemented by programming:
– Any employee whose overtime exceeds 10 hours per week must be
paid 1.5 times the hourly rate
3
Database Constraints
• Sometimes referred to as integrity constraints or integrity rules
• Database constraints are restrictions on the contents of the
database or on database operations
• Constraints are the result of determining the business rules
during the requirements analysis phase
4
Database Constraints
• Constraints provide a way to guarantee that:
– Rows in a table have valid primary and unique key values
– Rows in a dependent table have valid foreign key values that
reference rows in a parent table
– Individual column values are valid
5
Database Constraints
• As many constraints as necessary are specified for a table to
ensure data are acceptable and accurate
• None of the constraints are required, although most tables
contain a primary key
• Once a constraint has been defined for a table, the constraint is
enforced for all database updates
Constraint Categories
7
Constraint Names
• All constraints should be given a name
– NULL & DEFAULT constraints may be the only exception
• If a name is not specified, the DBMS generates an ambiguous
name that is difficult to understand and locate
8
Suffix for Constraint Names
Constraint Suffix
UNIQUE _uk
CHECK _ck
9
Data Type Constraint
• Restricts contents of the column data
10
NULL Constraint
• Indicates the optionality of the column data
11
Inserting Rows with NULL Values
12
Verify NULL values from INSERTs
13
UPDATE Statement
Set Null-Capable Column to NULL
14
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
15
DEFAULT Value
16
DEFAULT Value
17
CURRENT_DATE
18
UPDATE Statement
Set Column to Default Value
19
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
20
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
21
Primary Key Constraint
Column Level
• Not recommended
– DBMS creates a constraint name that is cryptic
22
Primary Key Constraint
Table Level - No Constraint Name
• Not recommended
– DBMS creates a constraint name that is cryptic
26
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
27
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
28
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
29
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 );
30
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 31
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
32
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
33
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
34
FOREIGN KEY Constraint
• For example:
– The relationship between ORDERS and CUSTOMERS is expressed by a
foreign key in the ORDERS table that contains a value identical to a
primary key value in a row in the CUSTOMERS table
– For each row in the ORDERS table, the customer_id column should
contain the same value as the customer_id column of some
CUSTOMERS row because this value tells which customer placed the
order
35
FOREIGN KEY Constraint
CUSTOMERS(customer_id, name, …)
ORDERS(customer_id, product_id, quantity, …)
PRODUCTS(product_id, description, …)
36
FOREIGN KEY Constraint
ALTER TABLE employees
ADD CONSTRAINT employees_work_department_fk
FOREIGN KEY ( work_department )
REFERENCES departments( department_id );
37
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
38
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
39
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
40
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 )
41
Constraint Unit Testing
42
43