Você está na página 1de 25

11

Including Constraints
Objectives

After
After completing
completing this
this lesson,
lesson, you
you should
should
be
be able
able to
to do
do the
the following:
following:
•• Describe
Describe constraints
constraints
•• Create
Create and
and maintain
maintain constraints
constraints

11-2
What Are Constraints?
•• Constraints
Constraints enforce
enforce rules
rules at
at the
the table
table level.
level.
•• Constraints
Constraints prevent
prevent the
the deletion
deletion of
of aa table
table if
if
there
there are
are dependencies.
dependencies.
•• The
The following
following constraint
constraint types
types are
are valid
valid inin
Oracle:
Oracle:
–– NOT
NOT NULL
NULL
–– UNIQUE
UNIQUE
–– PRIMARY
PRIMARY KEYKEY
–– FOREIGN
FOREIGN KEYKEY
–– CHECK
CHECK
11-3
Constraint Guidelines
•• Name
Name aa constraint
constraint or
or the
the Oracle
Oracle Server
Server will
will
generate
generate aa name
name by
by using
using the
the SYS_Cn
SYS_Cn
format.
format.
•• Create
Create aa constraint:
constraint:
–– At
At the
the same
same time
time as
as the
the table
table is
is created
created
–– After
After the
the table
table has
has been
been created
created
•• Define
Define aa constraint
constraint at
at the
the column
column oror table
table
level.
level.
•• View
View aa constraint
constraint in
in the
the data
data dictionary.
dictionary.
11-4
Defining Constraints
CREATE TABLE [schema.]table
(column datatype [DEFAULT expr]
[column_constraint],
...
[table_constraint][,...]);

CREATE TABLE emp(


empno NUMBER(4),
ename VARCHAR2(10),
...
deptno NUMBER(7,2) NOT NULL,
CONSTRAINT emp_empno_pk
PRIMARY KEY (EMPNO));

11-5
Defining Constraints

•• Column
Column constraint
constraint level
level
column
column [CONSTRAINT
[CONSTRAINT constraint_name]
constraint_name] constraint_type,
constraint_type,

•• Table
Table constraint
constraint level
level
column,...
column,...
[CONSTRAINT
[CONSTRAINT constraint_name]
constraint_name] constraint_type
constraint_type
(column,
(column, ...),
...),

11-6
The NOT NULL Constraint
Ensures
Ensures that
that null
null values
values are
are not
not permitted
permitted
for
for the
the column
column
EMP
EMPNO ENAME JOB ... COMM DEPTNO

7839 KING PRESIDENT 10


7698 BLAKE MANAGER 30
7782 CLARK MANAGER 10
7566 JONES MANAGER 20
...

NOT NULL constraint Absence of NOT NULL NOT NULL constraint


(no row can contain constraint
a null value for (any row can contain
this column) null for this column)

11-7
The NOT NULL Constraint
Defined
Defined at
at the
the column
column level
level
SQL> CREATE TABLE emp(
2 empno NUMBER(4),
3 ename VARCHAR2(10) NOT NULL,
4 job VARCHAR2(9),
5 mgr NUMBER(4),
6 hiredate DATE,
7 sal NUMBER(7,2),
8 comm NUMBER(7,2),
9 deptno NUMBER(7,2) NOT NULL);

11-8
The UNIQUE Key Constraint
UNIQUE key constraint
DEPT
DEPTNO DNAME LOC
------ ---------- --------
10 ACCOUNTING NEW YORK
20 RESEARCH DALLAS
30 SALES CHICAGO
40 OPERATIONS BOSTON

Insert into Not allowed


50 SALES DETROIT (DNAME−
(DNAME SALES
already exists)
60 BOSTON Allowed

11-9
The UNIQUE Key Constraint
Defined
Defined at
at either
either the
the table
table level
level or
or the
the column
column
level
level

SQL> CREATE TABLE dept(


2 deptno NUMBER(2),
3 dname VARCHAR2(14),
4 loc VARCHAR2(13),
5 CONSTRAINT dept_dname_uk UNIQUE(dname));

11-10
The PRIMARY KEY Constraint
PRIMARY KEY
DEPT
DEPTNO DNAME LOC
------ ---------- --------
10 ACCOUNTING NEW YORK
20 RESEARCH DALLAS
30 SALES CHICAGO
40 OPERATIONS BOSTON

Insert into Not allowed


20 MARKETING DALLAS (DEPTNO−
(DEPTNO 20 already
exists)
FINANCE NEW YORK Not allowed
(DEPTNO is null)

11-11
The PRIMARY KEY Constraint
Defined
Defined at
at either
either the
the table
table level
level or
or the
the column
column
level
level
SQL> CREATE TABLE dept(
2 deptno NUMBER(2),
3 dname VARCHAR2(14),
4 loc VARCHAR2(13),
5 CONSTRAINT dept_dname_uk UNIQUE (dname),
6 CONSTRAINT dept_deptno_pk PRIMARY KEY(deptno));

11-12
The FOREIGN KEY Constraint
DEPT
PRIMARY DEPTNO DNAME LOC
KEY ------ ---------- --------
10 ACCOUNTING NEW YORK
20 RESEARCH DALLAS
...
EMP
EMPNO ENAME JOB ... COMM DEPTNO FOREIGN
KEY
7839 KING PRESIDENT 10
7698 BLAKE MANAGER 30
... Not allowed
(DEPTNO 9
Insert into does not exist
in the DEPT
7571 FORD MANAGER ... 200 9 table)
7571 FORD MANAGER ... 200 20 Allowed

11-13
The FOREIGN KEY Constraint
Defined
Defined at
at either
either the
the table
table level
level or
or the
the
column
column level
level
SQL> CREATE TABLE emp(
2 empno NUMBER(4),
3 ename VARCHAR2(10) NOT NULL,
4 job VARCHAR2(9),
5 mgr NUMBER(4),
6 hiredate DATE,
7 sal NUMBER(7,2),
8 comm NUMBER(7,2),
9 deptno NUMBER(7,2) NOT NULL,
10 CONSTRAINT emp_deptno_fk FOREIGN KEY (deptno)
11 REFERENCES dept (deptno));

11-14
FOREIGN KEY Constraint
Keywords
•• FOREIGN
FOREIGN KEY
KEY
Defines
Defines the
the column
column in in the
the child
child table
table at
at
the
the table
table constraint
constraint level
level
•• REFERENCES
REFERENCES
Identifies
Identifies the
the table
table and
and column
column inin the
the
parent
parent table
table
•• ON
ON DELETE
DELETE CASCADE
CASCADE
Allows
Allows deletion
deletion inin the
the parent
parent table
table and
and
deletion
deletion ofof the
the dependent
dependent rows
rows in
in the
the
11-15
child
child table
table
The CHECK Constraint
•• Defines
Defines aa condition
condition that
that each
each row
row must
must
satisfy
satisfy
•• Expressions
Expressions thatthat are
are not
not allowed:
allowed:
–– References
References toto CURRVAL,
CURRVAL, NEXTVAL,
NEXTVAL,
LEVEL,
LEVEL, and
and ROWNUM
ROWNUM pseudocolumns
pseudocolumns
–– Calls
Calls to
to SYSDATE,
SYSDATE, UID,UID, USER,
USER, and
and
USERENV
USERENV functions
functions
–– Queries
Queries that
that refer
refer to
to other
other values
values in
in other
other
rows
rows
..., deptno NUMBER(2),
CONSTRAINT emp_deptno_ck
CHECK (DEPTNO BETWEEN 10 AND 99),...

11-16
Adding a Constraint

ALTER
ALTER TABLE
TABLE table
table
ADD
ADD [CONSTRAINT
[CONSTRAINT constraint]
constraint] type
type (column);
(column);

•• Add
Add or
or drop,
drop, but
but not
not modify,
modify, aa constraint
constraint
•• Enable
Enable or
or disable
disable constraints
constraints
•• Add
Add aa NOT
NOT NULL
NULL constraint
constraint by
by using
using the
the
MODIFY
MODIFY clause
clause

11-17
Adding a Constraint
Add
Add aa FOREIGN
FOREIGN KEY KEY constraint
constraint to
to the
the
EMP
EMP table
table indicating
indicating that
that aa manager
manager must
must
already
already exist
exist as
as aa valid
valid employee
employee in in the
the
EMP
EMP table.
table.
SQL> ALTER TABLE emp
2 ADD CONSTRAINT emp_mgr_fk
3 FOREIGN KEY(mgr) REFERENCES emp(empno);
Table altered.

11-18
Dropping a Constraint
•• Remove
Remove the
the manager
manager constraint
constraint from
from
the
the EMP
EMP table.
table.
SQL>
SQL> ALTER
ALTER TABLE
TABLE emp
emp
22 DROP
DROP CONSTRAINT
CONSTRAINT emp_mgr_fk;
emp_mgr_fk;
Table
Table altered.
altered.

•• Remove
Remove the
the PRIMARY
PRIMARY KEYKEY constraint
constraint
on
on the
the DEPT
DEPT table
table and
and drop
drop the
the
associated
associated FOREIGN
FOREIGN KEYKEY constraint
constraint on
on
the
the EMP.DEPTNO
EMP.DEPTNO column.
column.
SQL>
SQL> ALTER
ALTER TABLE
TABLE dept
dept
22 DROP
DROP PRIMARY
PRIMARY KEY
KEY CASCADE;
CASCADE;
Table
Table altered.
altered.

11-19
Disabling Constraints
•• Execute
Execute the
the DISABLE
DISABLE clause
clause of
of the
the
ALTER
ALTER TABLE
TABLE statement
statement toto deactivate
deactivate
an
an integrity
integrity constraint.
constraint.
•• Apply
Apply the
the CASCADE
CASCADE option
option to
to disable
disable
dependent
dependent integrity
integrity constraints.
constraints.
SQL>
SQL> ALTER
ALTER TABLE
TABLE emp
emp
22 DISABLE
DISABLE CONSTRAINT
CONSTRAINT emp_empno_pk
emp_empno_pk CASCADE;
CASCADE;
Table
Table altered.
altered.

11-20
Enabling Constraints
•• Activate
Activate an
an integrity
integrity constraint
constraint currently
currently
disabled
disabled in
in the
the table
table definition
definition by
by using
using
the
the ENABLE
ENABLE clause.
clause.
SQL>
SQL> ALTER
ALTER TABLE
TABLE emp
emp
22 ENABLE
ENABLE CONSTRAINT
CONSTRAINT emp_empno_pk;
emp_empno_pk;
Table
Table altered.
altered.

•• A
A UNIQUE
UNIQUE or or PRIMARY
PRIMARY KEY KEY index
index is
is
automatically
automatically created
created if
if you
you enable
enable aa
UNIQUE
UNIQUE key
key or
or PRIMARY
PRIMARY KEY KEY
constraint.
constraint.

11-21
Viewing Constraints

Query
Query the
the USER_CONSTRAINTS
USER_CONSTRAINTS table table to
to
view
view all
all constraint
constraint definitions
definitions and
and names.
names.
SQL> SELECT constraint_name, constraint_type,
2 search_condition
3 FROM user_constraints
4 WHERE table_name = 'EMP';

CONSTRAINT_NAME
CONSTRAINT_NAME CC SEARCH_CONDITION
SEARCH_CONDITION
------------------------
------------------------ -- -------------------------
-------------------------
SYS_C00674
SYS_C00674 CC EMPNO
EMPNO IS
IS NOT
NOT NULL
NULL
SYS_C00675
SYS_C00675 CC DEPTNO
DEPTNO IS
IS NOT
NOT NULL
NULL
EMP_EMPNO_PK
EMP_EMPNO_PK PP
...
...

11-22
Viewing the Columns
Associated with Constraints
View
View the
the columns
columns associated
associated with
with the
the
constraint
constraint names
names in
in the
the
USER_CONS_COLUMNS
USER_CONS_COLUMNS view. view.
SQL> SELECT constraint_name, column_name
2 FROM user_cons_columns
3 WHERE table_name = 'EMP';

CONSTRAINT_NAME
CONSTRAINT_NAME COLUMN_NAME
COLUMN_NAME
-------------------------
------------------------- ----------------------
----------------------
EMP_DEPTNO_FK
EMP_DEPTNO_FK DEPTNO
DEPTNO
EMP_EMPNO_PK
EMP_EMPNO_PK EMPNO
EMPNO
EMP_MGR_FK
EMP_MGR_FK MGR
MGR
SYS_C00674
SYS_C00674 EMPNO
EMPNO
SYS_C00675
SYS_C00675 DEPTNO
DEPTNO
11-23
Summary
•• Create
Create the
the following
following types
types of
of constraints:
constraints:
–– NOT
NOT NULL
NULL
–– UNIQUE
UNIQUE
–– PRIMARY
PRIMARY KEY
KEY
–– FOREIGN
FOREIGN KEY
KEY
–– CHECK
CHECK
•• Query
Query the
the USER_CONSTRAINTS
USER_CONSTRAINTS table table to
to
view
view all
all constraint
constraint definitions
definitions and
and names.
names.

11-24
Practice Overview
Adding
Addingconstraints
constraintsto
toexisting
existingtables
tables
• •Adding
Adding more columns toaatable
more columns to table
• •Displaying
Displayinginformation
informationinindata
datadictionary
dictionaryviews
views
••

11-25