Sie sind auf Seite 1von 7

Objectives

11
Including Constraints

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

Copyright Oracle Corporation, 1998. All rights reserved.

11-2

Copyright Oracle Corporation, 1998. All rights reserved.

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 a a table table if there are dependencies. if there are dependencies. The The following following constraint constraint types types are are valid valid in in Oracle: Oracle: NOT NOT NULL NULL UNIQUE UNIQUE Key Key PRIMARY PRIMARY KEY KEY FOREIGN FOREIGN KEY KEY CHECK CHECK
11-3 Copyright Oracle Corporation, 1998. All rights reserved.

Constraint Guidelines
Name Name a a constraint constraint or or the the Oracle Oracle Server Server will will generate generate a a name name by by using using the the SYS_C SYS_Cn n format. format. Create Create a a 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 a a constraint constraint at at the the column column or or table table level. level. View View a a constraint constraint in in the the data data dictionary. dictionary. (USER_CONSTRAINTS) (USER_CONSTRAINTS)
11-4 Copyright Oracle Corporation, 1998. All rights reserved.

<Course name> <Lesson number>-1

Defining Constraints
CREATE TABLE [schema.]table (column datatype [DEFAULT expr] [column_constraint], [table_constraint]);

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


CREATE TABLE emp( empno NUMBER(4), ename VARCHAR2(10), deptno NUMBER(7,2) NOT NULL, CONSTRAINT emp_empno_pk PRIMARY KEY (EMPNO)); column,... column,... [CONSTRAINT ] [CONSTRAINT constraint_name constraint_name ] constraint_type constraint_type ( column , ( column , ...), ...),

11-5

Copyright Oracle Corporation, 1998. All rights reserved.

11-6

Copyright Oracle Corporation, 1998. All rights reserved.

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 7839 7698 7782 7566 ... KING BLAKE CLARK JONES JOB PRESIDENT MANAGER MANAGER MANAGER ... COMM DEPTNO 10 30 10 20

The NOT NULL Constraint


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

NOT NULL constraint (no row may contain a null value for this column)
11-7

Absence of NOT NULL constraint (any row can contain null for this column)

NOT NULL constraint

Copyright Oracle Corporation, 1998. All rights reserved.

11-8

Copyright Oracle Corporation, 1998. All rights reserved.

<Course name> <Lesson number>-2

The UNIQUE Key Constraint


UNIQUE key constraint

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 2 deptno 3 dname 4 loc 5 CONSTRAINT dept( NUMBER(2), VARCHAR2(14), VARCHAR2(13), dept_dname_uk UNIQUE(dname));

DEPT
DEPTNO -----10 20 30 40 DNAME ---------ACCOUNTING RESEARCH SALES OPERATIONS LOC -------NEW YORK DALLAS CHICAGO BOSTON

Insert into 50 SALES 60 DETROIT BOSTON

Not allowed (DNAMESALES already exists) Allowed

11-9

Copyright Oracle Corporation, 1998. All rights reserved.

11-10

Copyright Oracle Corporation, 1998. All rights reserved.

The PRIMARY KEY Constraint


PRIMARY KEY

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 2 deptno 3 dname 4 loc 5 CONSTRAINT 6 CONSTRAINT dept( NUMBER(2), VARCHAR2(14), VARCHAR2(13), dept_dname_uk UNIQUE (dname), dept_deptno_pk PRIMARY KEY(deptno));

DEPT
DEPTNO -----10 20 30 40 DNAME ---------ACCOUNTING RESEARCH SALES OPERATIONS LOC -------NEW YORK DALLAS CHICAGO BOSTON

20 MARKETING FINANCE

Insert into DALLAS NEW YORK

Not allowed (DEPTNO20 already exists) Not allowed (DEPTNO is null)

11-11

Copyright Oracle Corporation, 1998. All rights reserved.

11-12

Copyright Oracle Corporation, 1998. All rights reserved.

<Course name> <Lesson number>-3

The FOREIGN KEY Constraint


DEPT
PRIMARY KEY DEPTNO -----10 20 ... DNAME ---------ACCOUNTING RESEARCH LOC -------NEW YORK DALLAS

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

EMP
EMPNO ENAME 7839 KING 7698 BLAKE ... JOB PRESIDENT MANAGER ... COMM DEPTNO 10 30 Not allowed (DEPTNO9 does not exist in the DEPT table Allowed FOREIGN KEY

Insert into 7571 FORD 7571 FORD


11-13

MANAGER MANAGER

... ...

200 200

Copyright Oracle Corporation, 1998. All rights reserved.

11-14

Copyright Oracle Corporation, 1998. All rights reserved.

FOREIGN KEY Constraint Keywords


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

The CHECK Constraint


Defines Defines a a condition condition that that each each row row must must satisfy satisfy Expressions Expressions that that are are not not allowed: allowed: References References to to pseudocolumns pseudocolumns CURRVAL, CURRVAL, NEXTVAL, NEXTVAL, LEVEL, LEVEL, and and ROWNUM ROWNUM 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 Copyright Oracle Corporation, 1998. All rights reserved.

<Course name> <Lesson number>-4

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

Adding a Constraint
Add Add a a FOREIGN FOREIGN KEY KEY constraint constraint to to the the EMP EMP table table indicating indicating that that a a manager manager must must already already exist exist as as a a 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.

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

11-17

Copyright Oracle Corporation, 1998. All rights reserved.

11-18

Copyright Oracle Corporation, 1998. All rights reserved.

Dropping a Constraint
Remove Remove the the manager manager constraint constraint from from the the EMP EMP table. table.
SQL> emp SQL> ALTER ALTER TABLE TABLE emp 2 2 DROP DROP CONSTRAINT CONSTRAINT emp_mgr_fk; emp_mgr_fk; Table altered. Table altered.

ALTER ALTER TABLE TABLE table table DROP DROP PRIMARY PRIMARY KEY KEY | | UNIQUE UNIQUE ( (column column) ) | | CONSTRAINT CONSTRAINT constraint constraint [CASCADE]; [CASCADE];

Remove Remove the the PRIMARY PRIMARY KEY KEY constraint constraint on on the the DEPT DEPT table table and and drop drop the the associated associated FOREIGN FOREIGN KEY KEY constraint constraint on on the the EMP.DEPTNO EMP.DEPTNO column. column.
SQL> dept SQL> ALTER ALTER TABLE TABLE dept 2 2 DROP DROP PRIMARY PRIMARY KEY KEY CASCADE; CASCADE; Table Table altered. altered.
11-19 Copyright Oracle Corporation, 1998. All rights reserved. 11-20 Copyright Oracle Corporation, 1998. All rights reserved.

<Course name> <Lesson number>-5

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

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 2 2 ENABLE ENABLE CONSTRAINT CONSTRAINT Table altered. Table altered. emp emp emp_empno_pk; emp_empno_pk;

ALTER table ALTER TABLE TABLE table DISABLE DISABLE CONSTRAINT CONSTRAINT constraint constraint [CASCADE]; [CASCADE];
11-21 Copyright Oracle Corporation, 1998. All rights reserved.

A A UNIQUE UNIQUE or or PRIMARY PRIMARY KEY KEY index index is is automatically automatically created created if if you you enable enable a a UNIQUE UNIQUE key key or or PRIMARY PRIMARY KEY KEY constraint. constraint.
11-22 Copyright Oracle Corporation, 1998. All rights reserved.

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> 2 3 4 SELECT constraint_name, constraint_type, search_condition FROM user_constraints WHERE table_name = 'EMP'; C C SEARCH_CONDITION SEARCH_CONDITION - ------------------------------------------------C C EMPNO EMPNO IS IS NOT NOT NULL NULL C C DEPTNO DEPTNO IS IS NOT NOT NULL NULL P P

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 2 FROM 3 WHERE constraint_name, column_name user_cons_columns table_name = 'EMP'; COLUMN_NAME COLUMN_NAME ------------------------------------------DEPTNO DEPTNO EMPNO EMPNO MGR MGR EMPNO EMPNO DEPTNO DEPTNO

CONSTRAINT_NAME CONSTRAINT_NAME ----------------------------------------------SYS_C00674 SYS_C00674 SYS_C00675 SYS_C00675 EMP_EMPNO_PK EMP_EMPNO_PK ... ...

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-24

11-23

Copyright Oracle Corporation, 1998. All rights reserved.

Copyright Oracle Corporation, 1998. All rights reserved.

<Course name> <Lesson number>-6

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

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

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-25 Copyright Oracle Corporation, 1998. All rights reserved. 11-26 Copyright Oracle Corporation, 1998. All rights reserved.

11-27

Copyright Oracle Corporation, 1998. All rights reserved.

<Course name> <Lesson number>-7

Das könnte Ihnen auch gefallen