Escolar Documentos
Profissional Documentos
Cultura Documentos
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
11-2
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.
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 ,
11-5
11-6
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)
11-8
DEPT
DEPTNO -----10 20 30 40 DNAME ---------ACCOUNTING RESEARCH SALES OPERATIONS LOC -------NEW YORK DALLAS CHICAGO BOSTON
11-9
11-10
DEPT
DEPTNO -----10 20 30 40 DNAME ---------ACCOUNTING RESEARCH SALES OPERATIONS LOC -------NEW YORK DALLAS CHICAGO BOSTON
20 MARKETING FINANCE
11-11
11-12
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
MANAGER MANAGER
... ...
200 200
11-14
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
11-18
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.
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
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
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