Constraints
Constraints
Constraints
Including Constraints
Copyright
Objectives
After completing this lesson, you should
be able to do the following:
Describe constraints
Create and maintain constraints
11-2
Copyright
Copyright
Constraint Guidelines
Name a constraint or the Oracle Server will
generate a name by using the SYS_Cn
format.
Create a constraint:
At the same time as the table is created
After the table has been created
Copyright
Defining Constraints
CREATE TABLE [schema.]table
(column datatype [DEFAULT expr]
[column_constraint],
...
[table_constraint][,...]);
11-5
Copyright
Defining Constraints
Column constraint level
column
column [CONSTRAINT
[CONSTRAINT constraint_name]
constraint_name] constraint_type,
constraint_type,
11-6
Copyright
KING
BLAKE
CLARK
JONES
Copyright
JOB
...
COMM
PRESIDENT
MANAGER
MANAGER
MANAGER
DEPTNO
10
30
10
20
11-8
Copyright
emp(
NUMBER(4),
VARCHAR2(10) NOT NULL,
VARCHAR2(9),
NUMBER(4),
DATE,
NUMBER(7,2),
NUMBER(7,2),
NUMBER(7,2) NOT NULL);
LOC
-------NEW YORK
DALLAS
CHICAGO
BOSTON
Insert into
11-9
50 SALES
DETROIT
Not allowed
(DNAMESALES
(DNAME
already exists)
60
BOSTON
Allowed
Copyright
11-10
Copyright
dept(
NUMBER(2),
VARCHAR2(14),
VARCHAR2(13),
dept_dname_uk UNIQUE(dname));
PRIMARY KEY
DNAME
---------ACCOUNTING
RESEARCH
SALES
OPERATIONS
LOC
-------NEW YORK
DALLAS
CHICAGO
BOSTON
Insert into
20 MARKETING
FINANCE
11-11
Copyright
DALLAS
NEW YORK
Not allowed
(DEPTNO
(DEPTNO 20 already
exists)
Not allowed
(DEPTNO is null)
11-12
Copyright
dept(
NUMBER(2),
VARCHAR2(14),
VARCHAR2(13),
dept_dname_uk UNIQUE (dname),
dept_deptno_pk PRIMARY KEY(deptno));
DEPTNO
-----10
20
...
DNAME
---------ACCOUNTING
RESEARCH
LOC
-------NEW YORK
DALLAS
EMP
EMPNO ENAME
7839 KING
7698 BLAKE
...
JOB
...
COMM
PRESIDENT
MANAGER
DEPTNO
10
30
Insert into
7571 FORD
7571 FORD
11-13
MANAGER
MANAGER
Copyright
FOREIGN
KEY
...
...
200
200
9
20
Not allowed
(DEPTNO
(DEPTNO 9
does not exist
in the DEPT
table)
Allowed
11-14
Copyright
Copyright
Adding a Constraint
ALTER
ALTER TABLE
TABLE table
table
ADD
ADD [CONSTRAINT
[CONSTRAINT constraint]
constraint] type
type (column);
(column);
11-17
Copyright
Adding a Constraint
Add a FOREIGN KEY constraint to the
EMP table indicating that a manager must
already exist as a valid employee in the
EMP table.
SQL> ALTER TABLE
emp
2 ADD CONSTRAINT emp_mgr_fk
3
FOREIGN KEY(mgr) REFERENCES emp(empno);
Table altered.
11-18
Copyright
Dropping a Constraint
Remove the manager constraint from
the EMP table.
SQL>
emp
SQL> ALTER
ALTER TABLE
TABLE
emp
22 DROP
DROP CONSTRAINT
CONSTRAINT emp_mgr_fk;
emp_mgr_fk;
Table
Table altered.
altered.
Copyright
Disabling Constraints
Execute the DISABLE clause of the
ALTER TABLE statement to deactivate
an integrity constraint.
Apply the CASCADE option to disable
dependent integrity constraints.
SQL>
SQL> ALTER
ALTER TABLE
TABLE
22 DISABLE
DISABLE CONSTRAINT
CONSTRAINT
Table
Table altered.
altered.
11-20
Copyright
emp
emp
emp_empno_pk
emp_empno_pk CASCADE;
CASCADE;
Enabling Constraints
Activate an integrity constraint currently
disabled in the table definition by using
the ENABLE clause.
SQL>
SQL> ALTER
ALTER TABLE
TABLE
22 ENABLE
ENABLE CONSTRAINT
CONSTRAINT
Table
Table altered.
altered.
emp
emp
emp_empno_pk;
emp_empno_pk;
Copyright
Viewing Constraints
Query the USER_CONSTRAINTS table to
view all constraint definitions and names.
SQL>
2
3
4
CONSTRAINT_NAME
CONSTRAINT_NAME
----------------------------------------------SYS_C00674
SYS_C00674
SYS_C00675
SYS_C00675
EMP_EMPNO_PK
EMP_EMPNO_PK
...
...
11-22
Copyright
CC
-CC
CC
PP
SEARCH_CONDITION
SEARCH_CONDITION
------------------------------------------------EMPNO
EMPNO IS
IS NOT
NOT NULL
NULL
DEPTNO
DEPTNO IS
IS NOT
NOT NULL
NULL
constraint_name, column_name
user_cons_columns
table_name = 'EMP';
CONSTRAINT_NAME
CONSTRAINT_NAME
------------------------------------------------EMP_DEPTNO_FK
EMP_DEPTNO_FK
EMP_EMPNO_PK
EMP_EMPNO_PK
EMP_MGR_FK
EMP_MGR_FK
SYS_C00674
SYS_C00674
SYS_C00675
SYS_C00675
11-23
Copyright
COLUMN_NAME
COLUMN_NAME
------------------------------------------DEPTNO
DEPTNO
EMPNO
EMPNO
MGR
MGR
EMPNO
EMPNO
DEPTNO
DEPTNO
Summary
Create the following types of constraints:
NOT NULL
UNIQUE
PRIMARY KEY
FOREIGN KEY
CHECK
Copyright
Practice Overview
Adding constraints to existing tables
Adding more columns to a table
Displaying information in data dictionary views
11-25
Copyright