Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Semestr2 / 1 - Oracle / 11-DDL-constraints.ppt
Скачиваний:
18
Добавлен:
12.05.2015
Размер:
155 Кб
Скачать
☆

The PRIMARYI 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),

5CONSTRAINT dept_dname_uk UNIQUE (dname),

6CONSTRAINT dept_deptno_pk PRIMARY KEY(deptno));

11-12

The FOREIGNI KEY Constrainti

DEPT

PRIMARY

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

DEPTNO

DNAME

 

 

LOC

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

KEY

 

 

 

 

 

 

 

------

----------

--------

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

10

ACCOUNTING

NEW YORK

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

20

RESEARCH

 

DALLAS

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

...

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

EMP

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

FOREIGN

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

EMPNO

 

ENAME

JOB

 

...

COMM

DEPTNO

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

KEY

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

7839

 

KING

PRESIDENT

 

 

 

 

10

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

7698

 

BLAKE

MANAGER

 

 

 

 

 

30

 

 

 

 

 

 

 

 

 

 

...

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Not allowed

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Insert into

 

 

 

 

 

 

 

 

 

 

(DEPTNO 9

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

does not exist

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

in the DEPT

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

7571

 

FORD

MANAGER

 

...

200

 

9

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

table

 

 

7571

 

FORD

MANAGER

 

...

200

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Allowed

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

11-13

The FOREIGNI KEY Constrainti

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

6hiredate 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

FOREIGNI KEY Constrainti Keywords

•• FOREIGNI KEY

Definesfi thet columnl inin thet childild tablet le att thet tablet le constrainttr i t levell l

•• REFERENCES

IdentifiesI tifi thet tablet le and columnl inin thet parentr t tablet le

•• ON DELETE CASCADE

Allowsll deletionl ti inin thet parentr t tablet le and deletionl ti off thet dependentt rowsr inin thet

11-15childild tablet le

The CHECK Constrainti

•• Definesfi a conditioniti thatt t each rowr mustt satisfyti fy

•• Expressionsr i thatt t arere nott allowed:ll :

–Referencesf r toto pseudocolumnsl CURRVAL,, NEXTVAL,, LEVEL,, and ROWNUM

–Callslls toto SYSDATE,, UID,I , USER,, and USERENV functionsf ti

–Queriesri thatt t referr f r toto othert r valuesl inin othert r rowsr

..., deptno NUMBER(2),

CONSTRAINT emp_deptno_ck

CHECK (DEPTNO BETWEEN 10 AND 99),...

11-16

Addingi a Constrainti

ALTER TABLE table

ADD [CONSTRAINT constraint] type (column);

•• Add orr drop,r , butt nott modify,if , a constrainttr i t

•• Enablele orr disablei le constraintstr i ts

•• Add a NOT NULL constrainttr i t by usingi thet

MODIFYI clausel

11-17

Addingi a Constrainti

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

2ADD CONSTRAINT emp_mgr_fk

3 FOREIGN KEY(mgr) REFERENCES emp(empno);

Table altered.

11-18

Droppingi a Constrainti

•• Remove thet managerr constrainttr i t fromfr thet EMP tablet le..

 

 

 

 

 

 

SQL>

ALTER TABLE

emp

 

 

2

DROP CONSTRAINT

emp_mgr_fk;

 

 

Table altered.

 

 

 

 

 

 

 

•• Remove thet PRIMARYI KEY constrainttr i t on thet DEPT tablet le and dropr thet associatedi t FOREIGNI KEY constrainttr i t on thet EMP..DEPTNO columnl ..

 

 

 

 

 

 

SQL>

ALTER TABLE

dept

 

 

2

DROP PRIMARY KEY CASCADE;

 

 

Table altered.

 

 

 

 

 

 

 

11-19

Disablingi li Constraintsi

•• Executete thet DISABLEI clausel off thet

ALTER TABLE statementt t t toto deactivateti te an integrityi t rity constrainttr i t..

•• Applyly thet CASCADE optionti toto disablei le dependentt integrityi t rity constraintstr i ts..

 

 

 

 

 

 

SQL>

ALTER TABLE

emp

 

 

2

DISABLE CONSTRAINT

emp_empno_pk CASCADE;

 

 

Table altered.

 

 

 

 

 

 

 

 

 

 

 

 

11-20

Enablingli Constraintsi

•• Activateti te an integrityi t rity constrainttr i t currentlyrr tly disabledi l inin thet tablet le definitionfi iti by usingi thet ENABLE clausel ..

 

 

 

 

 

 

SQL>

ALTER TABLE

emp

 

 

2

ENABLE CONSTRAINT

emp_empno_pk;

 

 

Table altered.

 

 

 

 

 

 

 

•• A UNIQUEI orr PRIMARYI KEY indexi isis automaticallyt ti lly createdr t ifif you enablele a UNIQUEI key orr PRIMARYI KEY constrainttr i t..

11-21

Соседние файлы в папке 1 - Oracle