Adding a FOREIGN KEY Constraint
The next statement adds a FOREIGN KEY
constraint that references the employees.employee_id
column:
SQL> CREATE TABLE departments
2 (department_id number(10) primary key,
3 department_name varchar2(50) not null
4 );
Table created.
SQL>
SQL> CREATE TABLE employees
2 ( employee_id number(10) not null,
3 last_name varchar2(50) not null,
4 job_id varchar2(30),
5 salary number(6),
6 manager_id number(6)
7 );
Table created.
SQL>
SQL>
SQL> ALTER TABLE employees
2 ADD CONSTRAINT by_fk
3 department_id REFERENCES departments(department_id);
Table altered.
SQL>
Home »
Oracle »
Table »
Oracle »
Table »
Constraints:
- Adding a Constraint with CHECK
- Adding a NOT NULL Constraint
- Adding a FOREIGN KEY Constraint
- ON DELETE CASCADE
- ON DELETE SET NULL
- Adding a UNIQUE Constraint
- CHECK constraint
- Multiple Constraints
- Dropping a Constraint
- Disabling a Constraint
- Enabling a Constraint
- Deferred Constraints
- Getting Information on Constraints:user_constraints and all_constraints
Related: