(If applicable)
Other Text:
(Examples or comments displayed on slide, if any).
(If applicable)
Other Text:
(Examples or comments displayed on slide, if any).
Can also enforce constraints with triggers
REF Constraint not covered in this course
(If applicable)
Other Text:
(Examples or comments displayed on slide, if any).
(If applicable)
Other Text:
(Examples or comments displayed on slide, if any).
Inline constraint
SQL> create table t (c1 number primary key);
Table created.
Inline constraint SYNTAX
[CONSTRAINT constraint-name] constraint-type [constraint-state]
“constraint-types”
[NOT] NULL
UNIQUE
PRIMARY KEY
CHECK ( condition )
references-clause
“references-clause”
REFERENCES [schema.]table [column] [ON DELETE CASCADE | SET NULL]
(If applicable)
Other Text:
(Examples or comments displayed on slide, if any).
(If applicable)
Other Text:
(Examples or comments displayed on slide, if any).
SQL> create table t2
2 (c1 number,
3 constraint c1_fk foreign key(c1) references t,
4 constraint c1_value check (c1 between 0 and 10),
5 c2 number,
6 constraint t2_pk primary key (c1, c2) )
SQL> /
Table created.
3 out-of-line constraints defined
Index created to support constraint will have same name - “T2_PK”
(If applicable)
Other Text:
(Examples or comments displayed on slide, if any).
SQL> select table_name, constraint_name
2 from user_constraints
3 where table_name in ('T', 'T2');
TABLE_NAME CONSTRAINT_NAME
------------ ---------------------------
T SYS_C002815
T2 C1_VALUE
T2 T2_PK
T2 C1_FK
Constraints created in this lesson
(If applicable)
Other Text:
(Examples or comments displayed on slide, if any).
SQL> insert into t2
2 (c1, c2) values(11,1);
insert into t2
*
ERROR at line 1:
ORA-02290: check constraint (DAVE.C1_VALUE) violated
SQL select constraint_name, search_condition
2 from user_constraints where constraint_name = 'C1_VALUE';
CONSTRAINT_NAME SEARCH_CONDITION
------------------------------ --------------------
C1_VALUE c1 between 0 and 10
Constraints “execute” when DML executed
(If applicable)

Other Text:
(Examples or comments displayed on slide, if any).
(If applicable)
Other Text:
(Examples or comments displayed on slide, if any).
Customers
1 Dave
2 Gary
3 Pat
Orders
1 1 Effective Oracle by Design
2 1 expert one-on-one Oracle
3 3 Linux Problem Solver
SQL> create table customers
2 (custno number primary key,
3 name varchar2(50) not null);
Table created.
SQL> create table orders
2 (orderno number primary key,
3 custno number references customers
4 on delete cascade,
5 book_title varchar2(50) not null);
Table created.
RI constraint prevents anomalies
SQL> create index orders_idx
2 on orders(custno);
Index created.
Important to create index on FK
(If applicable)
Other Text:
(Examples or comments displayed on slide, if any).
(If applicable)
Other Text:
(Examples or comments displayed on slide, if any).
SQL> create table t3 (c1 number);
Table created.
SQL> alter table t3 add constraint t3_c1_pk
2 primary key(c1);
Table altered.
Constraint added with ALTER TABLE
Use out-of-line syntax
(If applicable)
Other Text:
(Examples or comments displayed on slide, if any).
SQL> alter table t3 add constraint t3_c1_pk
2 primary key(c1) exceptions into EXCEPTIONS;
alter table t3 add constraint t3_c1_pk
*
ERROR at line 1:
ORA-02437: cannot validate (DAVE.T3_C1_PK) - primary key viola
SQL> select * from exceptions;
ROW_ID OWNER
------------------ -------
AAAHgrAAHAAAAj3AAA DAVE
AAAHgrAAHAAAAj3AAB DAVE
SQL> select * from t3 where rowid = 'AAAHgrAAHAAAAj3AAA';
C1
----------
1
(If applicable)
Other Text:
(Examples or comments displayed on slide, if any).
(If applicable)
Other Text:
(Examples or comments displayed on slide, if any).
SQL> create table t5
2 (c1 number);
Table created.
SQL> insert into t5 values(10);
1 row created.
SQL> alter table t5 add constraint
2 t5_check check(c1 between 1 and 5) novalidate;
Table altered.
(If applicable)
Other Text:
(Examples or comments displayed on slide, if any).
(If applicable)