Constraints

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 25
At a glance
Powered by AI
The key takeaways are that constraints are used to enforce rules at the table level and prevent invalid data, and the main types of constraints discussed are NOT NULL, UNIQUE, PRIMARY KEY, FOREIGN KEY, and CHECK.

The different types of constraints discussed are NOT NULL, UNIQUE, PRIMARY KEY, FOREIGN KEY, and CHECK.

Constraints can be defined at the column or table level, and at the time a table is created or after. They can be named or Oracle will generate a name.

11

Including Constraints

Copyright

Oracle Corporation, 1998. All rights reserved.

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

11-2

Copyright

Oracle Corporation, 1998. All rights reserved.

What Are Constraints?


Constraints enforce rules at the table level.
Constraints prevent the deletion of a table if
there are dependencies.
The following constraint types are valid in
Oracle:
NOT NULL
UNIQUE
PRIMARY KEY
FOREIGN KEY
CHECK
11-3

Copyright

Oracle Corporation, 1998. All rights reserved.

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

Define a constraint at the column or table


level.
View a constraint in the data dictionary.
11-4

Copyright

Oracle Corporation, 1998. All rights reserved.

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

Copyright

Oracle Corporation, 1998. All rights reserved.

Defining Constraints
Column constraint level
column
column [CONSTRAINT
[CONSTRAINT constraint_name]
constraint_name] constraint_type,
constraint_type,

Table constraint level


column,...
column,...
[CONSTRAINT
[CONSTRAINT constraint_name]
constraint_name] constraint_type
constraint_type
(column,
(column, ...),
...),

11-6

Copyright

Oracle Corporation, 1998. All rights reserved.

The NOT NULL Constraint


Ensures that null values are not permitted
for the column
EMP
EMPNO ENAME
7839
7698
7782
7566
...

KING
BLAKE
CLARK
JONES

NOT NULL constraint


(no row can contain
a null value for
this column)
11-7

Copyright

JOB

...

COMM

PRESIDENT
MANAGER
MANAGER
MANAGER

Absence of NOT NULL


constraint
(any row can contain
null for this column)

Oracle Corporation, 1998. All rights reserved.

DEPTNO
10
30
10
20

NOT NULL constraint

The NOT NULL Constraint


Defined at the column level
SQL> CREATE TABLE
2
empno
3
ename
4
job
5
mgr
6
hiredate
7
sal
8
comm
9
deptno

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);

Oracle Corporation, 1998. All rights reserved.

The UNIQUE Key Constraint


DEPT
DEPTNO
-----10
20
30
40

UNIQUE key constraint


DNAME
---------ACCOUNTING
RESEARCH
SALES
OPERATIONS

LOC
-------NEW YORK
DALLAS
CHICAGO
BOSTON

Insert into

11-9

50 SALES

DETROIT

Not allowed
(DNAMESALES
(DNAME
already exists)

60

BOSTON

Allowed

Copyright

Oracle Corporation, 1998. All rights reserved.

The UNIQUE Key Constraint


Defined at either the table level or the column
level
SQL> CREATE TABLE
2
deptno
3
dname
4
loc
5
CONSTRAINT

11-10

Copyright

dept(
NUMBER(2),
VARCHAR2(14),
VARCHAR2(13),
dept_dname_uk UNIQUE(dname));

Oracle Corporation, 1998. All rights reserved.

The PRIMARY KEY Constraint


DEPT
DEPTNO
-----10
20
30
40

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)

Oracle Corporation, 1998. All rights reserved.

The PRIMARY KEY Constraint


Defined at either the table level or the column
level
SQL> CREATE TABLE
2
deptno
3
dname
4
loc
5
CONSTRAINT
6
CONSTRAINT

11-12

Copyright

dept(
NUMBER(2),
VARCHAR2(14),
VARCHAR2(13),
dept_dname_uk UNIQUE (dname),
dept_deptno_pk PRIMARY KEY(deptno));

Oracle Corporation, 1998. All rights reserved.

The FOREIGN KEY Constraint


DEPT
PRIMARY
KEY

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

Oracle Corporation, 1998. All rights reserved.

Not allowed
(DEPTNO
(DEPTNO 9
does not exist
in the DEPT
table)
Allowed

The FOREIGN KEY Constraint


Defined at either the table level or 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),
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

Copyright

Oracle Corporation, 1998. All rights reserved.

FOREIGN KEY Constraint


Keywords
FOREIGN KEY

Defines the column in the child table at


the table constraint level
REFERENCES
Identifies the table and column in the
parent table
ON DELETE CASCADE
Allows deletion in the parent table and
deletion of the dependent rows in the
11-15child table
Copyright Oracle Corporation, 1998. All rights reserved.

The CHECK Constraint


Defines a condition that each row must
satisfy
Expressions that are not allowed:
References to CURRVAL, NEXTVAL,
LEVEL, and ROWNUM pseudocolumns
Calls to SYSDATE, UID, USER, and
USERENV functions
Queries that refer to other values in other
rows
..., deptno NUMBER(2),
CONSTRAINT emp_deptno_ck
CHECK (DEPTNO BETWEEN 10 AND 99),...
11-16

Copyright

Oracle Corporation, 1998. All rights reserved.

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

Add or drop, but not modify, a constraint


Enable or disable constraints
Add a NOT NULL constraint by using the
MODIFY clause

11-17

Copyright

Oracle Corporation, 1998. All rights reserved.

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

Oracle Corporation, 1998. All rights reserved.

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.

Remove the PRIMARY KEY constraint


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

Copyright

Oracle Corporation, 1998. All rights reserved.

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;

Oracle Corporation, 1998. All rights reserved.

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;

A UNIQUE or PRIMARY KEY index is


automatically created if you enable a
UNIQUE key or PRIMARY KEY
constraint.
11-21

Copyright

Oracle Corporation, 1998. All rights reserved.

Viewing Constraints
Query the USER_CONSTRAINTS table to
view all constraint definitions and names.
SQL>
2
3
4

SELECT constraint_name, constraint_type,


search_condition
FROM
user_constraints
WHERE table_name = 'EMP';

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

Oracle Corporation, 1998. All rights reserved.

Viewing the Columns


Associated with Constraints
View the columns associated with the
constraint names in the
USER_CONS_COLUMNS view.
SQL> SELECT
2 FROM
3 WHERE

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

Oracle Corporation, 1998. All rights reserved.

Summary
Create the following types of constraints:
NOT NULL
UNIQUE
PRIMARY KEY
FOREIGN KEY
CHECK

Query the USER_CONSTRAINTS table to


view all constraint definitions and names.
11-24

Copyright

Oracle Corporation, 1998. All rights reserved.

Practice Overview
Adding constraints to existing tables
Adding more columns to a table
Displaying information in data dictionary views

11-25

Copyright

Oracle Corporation, 1998. All rights reserved.

You might also like