Professional Documents
Culture Documents
Objectives
Describe constraints
Create and maintain constraints
-2
-3
Constraint Guidelines
Name a constraint or the Oracle server generates a
name by using the SYS_Cn format.
-4
Defining Constraints
-5
Defining Constraints
Column constraint level
column
column [CONSTRAINT
[CONSTRAINT constraint_name]
constraint_name] constraint_type,
constraint_type,
-6
-7
NOT NULL
constraint
-8
System
named
User
named
EMPLOYEES
INSERT INTO
Allowed
Not allowed:
already exists
-9
-10
Not allowed
(Null value)
Not allowed
(50 already exists)
-11
INSERT INTO
-12
PRIMARY
KEY
EMPLOYEES
FOREIGN
KEY
INSERT INTO
Not allowed
(9 does not
exist)
Allowed
-13
-14
-15
-16
ALTER
ALTER TABLE
TABLE table
table
ADD
ADD [CONSTRAINT
[CONSTRAINT constraint]
constraint] type
type (column);
(column);
-17
Adding a Constraint
-18
Dropping a Constraint
Remove the manager constraint from the
EMPLOYEES table.
ALTER
employees
ALTER TABLE
TABLE
employees
DROP
DROP CONSTRAINT
CONSTRAINT emp_manager_fk;
emp_manager_fk;
Table
Table altered.
altered.
ALTER
ALTER TABLE
TABLE departments
departments
DROP
DROP PRIMARY
PRIMARY KEY
KEY CASCADE;
CASCADE;
Table
Table altered.
altered.
-19
Disabling Constraints
Execute the DISABLE clause of the ALTER TABLE
statement to deactivate an integrity constraint.
ALTER
employees
ALTER TABLE
TABLE
employees
DISABLE
DISABLE CONSTRAINT
CONSTRAINT emp_emp_id_pk
emp_emp_id_pk CASCADE;
CASCADE;
Table
Table altered.
altered.
-20
Enabling Constraints
Activate an integrity constraint currently disabled
ALTER
ALTER TABLE
TABLE
ENABLE
ENABLE CONSTRAINT
CONSTRAINT
Table
Table altered.
altered.
employees
employees
emp_emp_id_pk;
emp_emp_id_pk;
-21
Cascading Constraints
The CASCADE CONSTRAINTS clause is used along
with the DROP COLUMN clause.
-22
Cascading Constraints
Example:
ALTER
ALTER TABLE
TABLE test1
test1
DROP
DROP (pk)
(pk) CASCADE
CASCADE CONSTRAINTS;
CONSTRAINTS;
Table
Table altered.
altered.
ALTER
ALTER TABLE
TABLE test1
test1
DROP
DROP (pk,
(pk, fk,
fk, col1)
col1) CASCADE
CASCADE CONSTRAINTS;
CONSTRAINTS;
Table
Table altered.
altered.
-23
Viewing Constraints
-24
constraint_name, constraint_type,
search_condition
user_constraints
table_name = 'EMPLOYEES';
-25
constraint_name, column_name
user_cons_columns
table_name = 'EMPLOYEES';
Summary
In this lesson, you should have learned how to create
constraints.
Types of constraints:
NOT NULL
UNIQUE
PRIMARY KEY
FOREIGN KEY
CHECK
-26
Practice 10 Overview
This practice covers the following topics:
-27
-30