To add constraint foreign key on a table of Oracle database you must use the alter table command and add constraint keyword.
Syntax
ALTER TABLE table_name1
ADD CONSTRAINT constraint_name
FOREIGN KEY (coll_name)
REFERENCES table_name2(coll_name);
Add constraint foreign key example
ALTER TABLE orders ADD CONSTRAINT fk_course FOREIGN KEY (course_id) REFERENCES course(course_id);
Output:
table ORDERS altered.
Check constraints
SELECT OWNER, CONSTRAINT_NAME, STATUS FROM user_constraints WHERE table_name = 'ORDERS';
Output:
OWNER | CONSTRAINT_NAME | STATUS |
---|---|---|
SYSTEM | FK_COURSE | ENABLED |