
- •Objectivesj i
- •What Are Constraints?i
- •Constrainti Guidelinesi li
- •Definingi i Constraintsi
- •Definingi i Constraintsi
- •The NOT NULL Constrainti
- •The NOT NULL Constrainti
- •The UNIQUEI Key Constrainti
- •The UNIQUEI Key Constrainti
- •The PRIMARYI KEY Constrainti
- •The PRIMARYI KEY Constrainti
- •The FOREIGNI KEY Constrainti
- •The FOREIGNI KEY Constrainti
- •FOREIGNI KEY Constrainti Keywords
- •The CHECK Constrainti
- •Addingi a Constrainti
- •Addingi a Constrainti
- •Droppingi a Constrainti
- •Disablingi li Constraintsi
- •Enablingli Constraintsi
- •Viewingi i Constraintsi
- •Viewingi i the Columnsl Associatedi withi Constraintsi
- •Summary
- •Practicei Overviewi

11
Including Constraints

Objectivesj i
After completing this lesson, you should be able to do the following:
•• Describeri constraintstr i ts
•• Creater te and maintaini t in constraintstr i ts
11-2

What Are Constraints?i
•• Constraintstr i ts enforcef r rulesr l att thet tablet le levell l..
•• Constraintstr i ts preventr t thet deletionl ti off a tablet le ifif theret re arere dependenciesi ..
•• The followingf ll i constrainttr i t typest arere validlid inin
Oracle:r l :
–NOT NULL
–UNIQUEI Key
–PRIMARYI KEY
–FOREIGNI KEY
–CHECK
11-3

Constrainti Guidelinesi li
•• Name a constrainttr i t orr thet Oracler le Serverr r willill generater te a name by usingi thet SYS_Cn formatf r t..
•• Creater te a constraint:tr i t:
–Att thet same timeti as thet tablet le isis createdr t
–Afterft r thet tablet le has been createdr t
•• Definefi a constrainttr i t att thet columnl orr tablet le levell l..
•• Viewi a constrainttr i t inin thet datata dictionaryi ti ry..
11-4

Definingi i Constraintsi
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

Definingi i Constraintsi
•• Columnl constrainttr i t levell l
column [CONSTRAINT constraint_name] constraint_type,
•• Tablele constrainttr i t levell l
column,...
[CONSTRAINT constraint_name] constraint_type
(column, ...),
11-6

The NOT NULL Constrainti
Ensures that null values are not permitted for the column
EMP
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
EMPNO |
ENAME |
|
JOB |
|
... |
COMM |
DEPTNO |
|
|
|||
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
7839 |
KING |
|
PRESIDENT |
|
|
10 |
|
|
||||
|
|
7698 |
BLAKE |
|
MANAGER |
|
|
30 |
|
|
||||
|
|
7782 |
CLARK |
|
MANAGER |
|
|
10 |
|
|
||||
|
|
7566 |
JONES |
|
MANAGER |
|
|
20 |
|
|
||||
|
|
... |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Absence of NOT NULL |
|
|
|
||||||||
|
|
|
|
|
|
|||||||||
NOT NULL constraint |
|
NOT NULL constraint |
||||||||||||
(no row may contain |
|
constraint |
|
|
|
|
|
|||||||
a null value for |
|
(any row can contain |
|
|
|
|
|
|||||||
this column) |
|
null for this column) |
|
|
|
|
|
11-7

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

The UNIQUEI Key Constrainti
UNIQUE key constraint
DEPT
|
|
|
|
|
|
|
DEPTNO |
DNAME |
LOC |
|
|
|
------ |
---------- |
-------- |
|
|
|
10 |
ACCOUNTING |
NEW YORK |
|
|
|
20 |
RESEARCH |
DALLAS |
|
|
|
30 |
SALES |
CHICAGO |
|
|
|
40 |
OPERATIONS |
BOSTON |
|
|
|
|
|
|
|
|
|
|
Insert into |
|||
|
|
|
|
|
|
|
50 |
SALES |
DETROIT |
|
|
|
|
|
|
|
|
|
60 |
|
BOSTON |
|
|
|
|
|
|
|
|
Not allowed (DNAME SALES already exists)
Allowed
11-9

The UNIQUEI Key Constrainti
Defined at either the table level or the column 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 PRIMARYI KEY Constrainti
PRIMARY KEY
DEPT
|
DEPTNO |
DNAME |
LOC |
||
|
------ |
---------- |
-------- |
|
|
|
10 |
ACCOUNTING |
NEW YORK |
|
|
|
20 |
RESEARCH |
DALLAS |
|
|
|
30 |
SALES |
CHICAGO |
|
|
|
40 |
OPERATIONS BOSTON |
|||
|
|
|
|
|
|
|
|
Insert into |
|||
|
|
|
|
|
|
|
20 |
MARKETING |
DALLAS |
|
|
|
|
|
|
|
|
|
|
FINANCE |
NEW YORK |
|
|
|
|
|
|
|
|
Not allowed (DEPTNO 20 already exists)
Not allowed (DEPTNO is null)
11-11