Including Constraints
10
Copyright © Oracle Corporation, 2001. All rights reserved.
10-1
Copyright © Oracle Corporation, 2001. All rights reserved.
Objectives
After completing this lesson, you should be able to
do the following:
10-2
Copyright © Oracle Corporation, 2001. All rights reserved.
What are Constraints?
10-3
Copyright © Oracle Corporation, 2001. All rights reserved.
Constraint Guidelines
10-4
Copyright © Oracle Corporation, 2001. All rights reserved.
Defining Constraints
CREATE TABLE [schema.]table
(column datatype [DEFAULT expr]
[column_constraint],
...
[table_constraint][,...]);
CREATE TABLE employees(
employee_id NUMBER(6),
first_name VARCHAR2(20),
...
job_id VARCHAR2(10) NOT NULL,
CONSTRAINT emp_emp_id_pk
PRIMARY KEY (EMPLOYEE_ID));
10-5
Copyright © Oracle Corporation, 2001. All rights reserved.
Defining Constraints
column [CONSTRAINT constraint_name] constraint_type,
column,...
[CONSTRAINT constraint_name] constraint_type
(column, ...),
10-6
Copyright © Oracle Corporation, 2001. All rights reserved.
The NOT NULL Constraint
Ensures that null values are not permitted for the
column:
NOT NULL constraint
(No row can contain�a null value for�this column.)
Absence of NOT NULL constraint
(Any row can contain�null for this column.)
NOT NULL
constraint
…
10-7
Copyright © Oracle Corporation, 2001. All rights reserved.
The NOT NULL Constraint
Is defined at the column level:
CREATE TABLE employees(
employee_id NUMBER(6),
last_name VARCHAR2(25) NOT NULL,
salary NUMBER(8,2),
commission_pct NUMBER(2,2),
hire_date DATE
CONSTRAINT emp_hire_date_nn
NOT NULL,
...
System
named
User
named
10-8
Copyright © Oracle Corporation, 2001. All rights reserved.
The UNIQUE Constraint
EMPLOYEES
UNIQUE constraint
INSERT INTO
Not allowed: already exists
Allowed
…
10-9
Copyright © Oracle Corporation, 2001. All rights reserved.
The UNIQUE Constraint
Defined at either the table level or the column level:
CREATE TABLE employees(
employee_id NUMBER(6),
last_name VARCHAR2(25) NOT NULL,
email VARCHAR2(25),
salary NUMBER(8,2),
commission_pct NUMBER(2,2),
hire_date DATE NOT NULL,
...
CONSTRAINT emp_email_uk UNIQUE(email));
10-10
Copyright © Oracle Corporation, 2001. All rights reserved.
The PRIMARY KEY Constraint
DEPARTMENTS
PRIMARY KEY
INSERT INTO
Not allowed
(Null value)
Not allowed
(50 already exists)
…
10-11
Copyright © Oracle Corporation, 2001. All rights reserved.
The PRIMARY KEY Constraint
Defined at either the table level or the column level:
CREATE TABLE departments(
department_id NUMBER(4),
department_name VARCHAR2(30)
CONSTRAINT dept_name_nn NOT NULL,
manager_id NUMBER(6),
location_id NUMBER(4),
CONSTRAINT dept_id_pk PRIMARY KEY(department_id));
10-12
Copyright © Oracle Corporation, 2001. All rights reserved.
The FOREIGN KEY Constraint
DEPARTMENTS
EMPLOYEES
FOREIGN�KEY
INSERT INTO
Not allowed�(9 does not exist)
Allowed
PRIMARY�KEY
…
…
10-13
Copyright © Oracle Corporation, 2001. All rights reserved.
The FOREIGN KEY Constraint
Defined at either the table level or the column level:
CREATE TABLE employees(
employee_id NUMBER(6),
last_name VARCHAR2(25) NOT NULL,
email VARCHAR2(25),
salary NUMBER(8,2),
commission_pct NUMBER(2,2),
hire_date DATE NOT NULL,
...
department_id NUMBER(4),
CONSTRAINT emp_dept_fk FOREIGN KEY (department_id)
REFERENCES departments(department_id),
CONSTRAINT emp_email_uk UNIQUE(email));
10-14
Copyright © Oracle Corporation, 2001. All rights reserved.
FOREIGN KEY Constraint �Keywords
10-15
Copyright © Oracle Corporation, 2001. All rights reserved.
The CHECK Constraint
..., salary NUMBER(2)
CONSTRAINT emp_salary_min
CHECK (salary > 0),...
10-16
Copyright © Oracle Corporation, 2001. All rights reserved.
Adding a Constraint Syntax
Use the ALTER TABLE statement to:
ALTER TABLE table
ADD [CONSTRAINT constraint] type (column);
10-17
Copyright © Oracle Corporation, 2001. All rights reserved.
Adding a Constraint
Add a FOREIGN KEY constraint to the EMPLOYEES
table indicating that a manager must already exist as
a valid employee in the EMPLOYEES table.
ALTER TABLE employees
ADD CONSTRAINT emp_manager_fk
FOREIGN KEY(manager_id)
REFERENCES employees(employee_id);
Table altered.
10-18
Copyright © Oracle Corporation, 2001. All rights reserved.
Dropping a Constraint
ALTER TABLE employees
DROP CONSTRAINT emp_manager_fk;
Table altered.
ALTER TABLE departments
DROP PRIMARY KEY CASCADE;
Table altered.
10-19
Copyright © Oracle Corporation, 2001. All rights reserved.
Disabling Constraints
ALTER TABLE employees
DISABLE CONSTRAINT emp_emp_id_pk CASCADE;
Table altered.
10-20
Copyright © Oracle Corporation, 2001. All rights reserved.
Enabling Constraints
ALTER TABLE employees
ENABLE CONSTRAINT emp_emp_id_pk;
Table altered.
10-21
Copyright © Oracle Corporation, 2001. All rights reserved.
Cascading Constraints
10-22
Copyright © Oracle Corporation, 2001. All rights reserved.
Cascading Constraints
Example:
ALTER TABLE test1
DROP (pk) CASCADE CONSTRAINTS;
Table altered.
ALTER TABLE test1
DROP (pk, fk, col1) CASCADE CONSTRAINTS;
Table altered.
10-23
Copyright © Oracle Corporation, 2001. All rights reserved.
Viewing Constraints
Query the USER_CONSTRAINTS table to view all
constraint definitions and names.
SELECT constraint_name, constraint_type,
search_condition
FROM user_constraints
WHERE table_name = 'EMPLOYEES';
…
10-24
Copyright © Oracle Corporation, 2001. All rights reserved.
Viewing the Columns Associated with Constraints
View the columns associated with the constraint
names in the USER_CONS_COLUMNS view.
SELECT constraint_name, column_name
FROM user_cons_columns
WHERE table_name = 'EMPLOYEES';
…
10-25
Copyright © Oracle Corporation, 2001. All rights reserved.
Summary
In this lesson, you should have learned how to create
constraints.
10-26
Copyright © Oracle Corporation, 2001. All rights reserved.
Practice 10 Overview
This practice covers the following topics:
10-27
Copyright © Oracle Corporation, 2001. All rights reserved.
10-28
Copyright © Oracle Corporation, 2001. All rights reserved.
10-29
Copyright © Oracle Corporation, 2001. All rights reserved.
10-30
Copyright © Oracle Corporation, 2001. All rights reserved.