0% found this document useful (0 votes)
14 views

Data Base

This document discusses how to create and manage constraints in Oracle databases. It describes the different types of constraints like NOT NULL, UNIQUE, PRIMARY KEY, and FOREIGN KEY. It also explains how to define, add, drop, disable, enable and view constraints using SQL commands.

Uploaded by

douminou12
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

Data Base

This document discusses how to create and manage constraints in Oracle databases. It describes the different types of constraints like NOT NULL, UNIQUE, PRIMARY KEY, and FOREIGN KEY. It also explains how to define, add, drop, disable, enable and view constraints using SQL commands.

Uploaded by

douminou12
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 22

CREATING

CONSTRAINTS
Fall Semester, 2014
Objectives
2

After completing this lesson, you should be able to


do the following:
 Describe constraints

 Create and maintain constraints


What Are Constraints?
3

 Constraints enforce rules at the table level.


 Constraints prevent the deletion of a table if there are
dependencies.
 The following constraint types are valid:
 NOT NULL
 UNIQUE
 PRIMARY KEY
 FOREIGN KEY
Constraint Guidelines
4

 Create a constraint either:


 At the same time as the table is created, or
 After the table has been created
 Define a constraint at the column or table level.
 View a constraint in the data dictionary.
Defining Constraints
5

CREATE TABLE [schema.]table


(column datatype [DEFAULT expr]
[column_constraint],
...
[table_constraint][,...]);

CREATE TABLE employees(


employee_id NUMBER(6),
first_name VARCHAR2(20),
...
job_id VARCHAR2(10) NOT NULL,
CONSTRAINT emp_emp_id_pk
PRIMARY KEY (EMPLOYEE_ID));
The NOT NULL Constraint
7

Ensures that null values are not permitted for the


column:

NOT NULL constraint NOT NULL Absence of NOT NULL


(No row can contain constraint constraint
a null value for (Any row can contain
this column.) null for this column.)
The NOT NULL Constraint
8

Is defined at the column level:


CREATE TABLE employees(
employee_id NUMBER(6),
last_name VARCHAR2(25) NOT NULL, System
named
salary NUMBER(8,2),
commission_pct NUMBER(2,2),
hire_date DATE
CONSTRAINT emp_hire_date_nn User
named
NOT NULL,
...
The UNIQUE Constraint
9
UNIQUE constraint
EMPLOYEES


INSERT INTO

Allowed
Not allowed:
already exists
The UNIQUE Constraint
10

Defined at either the table level or the column level:


CREATE TABLE employees(
employee_id NUMBER(6),
last_name VARCHAR2(25) NOT NULL,
email VARCHAR2(25),
salary NUMBER(8,2),
commission_pct NUMBER(2,2),
hire_date DATE NOT NULL,
...
CONSTRAINT emp_email_uk UNIQUE(email));
The PRIMARY KEY Constraint
11

DEPARTMENTS
PRIMARY KEY

Not allowed INSERT INTO


(Null value)

Not allowed
(50 already exists)
The PRIMARY KEY Constraint
12

Defined at either the table level or the column level:

CREATE TABLE departments(


department_id NUMBER(4),
department_name VARCHAR2(30)
CONSTRAINT dept_name_nn NOT NULL,
manager_id NUMBER(6),
location_id NUMBER(4),
CONSTRAINT dept_id_pk PRIMARY KEY(department_id));
The FOREIGN KEY Constraint
13

DEPARTMENTS

PRIMARY
KEY

EMPLOYEES
FOREIGN
KEY


Not allowed
INSERT INTO (9 does not
exist)
Allowed
The FOREIGN KEY Constraint
14

Defined at either the table level or the column level:

CREATE TABLE employees(


employee_id NUMBER(6),
last_name VARCHAR2(25) NOT NULL,
email VARCHAR2(25),
salary NUMBER(8,2),
commission_pct NUMBER(2,2),
hire_date DATE NOT NULL,
...
department_id NUMBER(4),
CONSTRAINT emp_dept_fk FOREIGN KEY (department_id)
REFERENCES departments(department_id),
CONSTRAINT emp_email_uk UNIQUE(email));
FOREIGN KEY Constraint
15
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: Deletes the dependent rows in the
child table when a row in the parent table is deleted.
• ON DELETE SET NULL: Converts dependent foreign key
values to null
Adding a Constraint Syntax
16

Use the ALTER TABLE statement to:


 Add or drop a constraint, but not modify its
structure
 Enable or disable constraints

 Add a NOT NULL constraint by using the


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

Add a FOREIGN KEY constraint to the EMPLOYEES


table indicating that a manager must already exist as a
valid employee in the EMPLOYEES table.

ALTER TABLE employees


ADD CONSTRAINT emp_manager_fk
FOREIGN KEY(manager_id)
REFERENCES employees(employee_id);
Table altered.
Dropping a Constraint
18

 Remove the manager constraint from the


EMPLOYEES table.
ALTER
ALTER TABLE
TABLE employees
employees
DROP
DROP CONSTRAINT
CONSTRAINT emp_manager_fk;
emp_manager_fk;
Table
Table altered.
altered.
 Remove the PRIMARY KEY constraint on the
DEPARTMENTS table and drop the associated
FOREIGN KEY constraint on the
EMPLOYEES.DEPARTMENT_ID column.

ALTER
ALTER TABLE
TABLE departments
departments
DROP
DROP PRIMARY
PRIMARY KEY
KEY CASCADE;
CASCADE;
Table
Table altered.
altered.
Disabling Constraints
19

 Execute the DISABLE clause of the ALTER TABLE


statement to deactivate an integrity constraint.
 Apply the CASCADE option to disable dependent
integrity constraints.

ALTER
ALTER TABLE
TABLE employees
employees
DISABLE
DISABLE CONSTRAINT
CONSTRAINT emp_emp_id_pk
emp_emp_id_pk CASCADE;
CASCADE;
Table
Table altered.
altered.
Enabling Constraints
20

 Activate an integrity constraint currently disabled


in the table definition by using the ENABLE
clause.
ALTER
ALTER TABLE
TABLE employees
employees
ENABLE
ENABLE CONSTRAINT
CONSTRAINT emp_emp_id_pk;
emp_emp_id_pk;
Table
Table altered.
altered.

 A UNIQUE or PRIMARY KEY index is


automatically created if you enable a UNIQUE
key or PRIMARY KEY constraint.
Viewing Constraints
22

Query the USER_CONSTRAINTS table to view all


constraint definitions and names.
SELECT constraint_name, constraint_type,
search_condition
FROM user_constraints
WHERE table_name = 'EMPLOYEES';


Summary
23

In this lesson, you should have learned how to create


constraints.
 Types of constraints:
 NOT NULL
 UNIQUE
 PRIMARY KEY
 FOREIGN KEY
 You can query the USER_CONSTRAINTS table to
view all constraint definitions and names.
24 Questions?

You might also like