Cascade constraints : Constraint « Table « Oracle PL/SQL Tutorial






SQL>
SQL> create table employee (
  2    gender_id  char(1),
  3    gender_nm  varchar2(6),
  4    constraint gender_pk primary key ( gender_id ),
  5    constraint gender_id_ck check ( gender_id in ( 'M', 'F' ) )
  6  );

Table created.

SQL>
SQL> insert into employee
  2  values ( 'F', 'Female' );

1 row created.

SQL>
SQL> insert into employee
  2  values ( 'M', 'Male' );

1 row created.

SQL>
SQL> create table people (
  2    first_name        varchar2(20),
  3    last_name         varchar2(25),
  4    gender         char(1)
  5  )
  6  /
create table people (
             *
ERROR at line 1:
ORA-00955: name is already used by an existing object


SQL>
SQL> alter table people
  2  add constraint people_gender_fk
  3  foreign key ( gender )
  4  references employee
  5  /

Table altered.

SQL>
SQL> insert into people
  2  values ( 'S', 'D', 'M' );

1 row created.

SQL>
SQL> insert into people
  2  values ( 'C', 'Beck', 'M' );

1 row created.

SQL>
SQL> insert into people
  2  values ( 'N', 'E', 'F' );

1 row created.

SQL>
SQL> drop table employee;
drop table employee
           *
ERROR at line 1:
ORA-02449: unique/primary keys in table referenced by foreign keys


SQL>
SQL> drop table employee cascade constraints;

Table dropped.

SQL>








6.10.Constraint
6.10.1.Adding a Constraint
6.10.2.Add constaint for a date type column: larger than a certain date
6.10.3.Adding a 'NOT NULL' Constraint
6.10.4.The database automatically assigns a name to the constraint
6.10.5.Dropping a Constraint
6.10.6.Disabling a Constraint
6.10.7.Add CASCADE to the end of a DISABLE CONSTRAINT clause
6.10.8.Enabling a Constraint
6.10.9.You can also choose to apply a constraint to new data only by specifying ENABLE NOVALIDATE.
6.10.10.Deferred Constraints
6.10.11.How to drop a constaint
6.10.12.Violate a constraint
6.10.13.Cascade constraints