Many to many using a primary-key and foreign-key relationship : Add Foreign Key « Constraints « Oracle PL / SQL






Many to many using a primary-key and foreign-key relationship

    
SQL> CREATE TABLE Course
  2     (course_id        VARCHAR2(10) NOT NULL,
  3      course_name      VARCHAR2(20),
  4      PRIMARY KEY (course_id));

Table created.

SQL>
SQL> CREATE TABLE emp
  2     (stud_id    VARCHAR2(10) NOT NULL,
  3      stud_name  VARCHAR2(20),
  4      PRIMARY KEY (stud_id));

Table created.

SQL>
SQL> CREATE TABLE Enrolls_in
  2     (course_id  VARCHAR2(10) NOT NULL,
  3      stud_id    VARCHAR2(10) NOT NULL,
  4      PRIMARY KEY (course_id, stud_id),
  5      FOREIGN KEY (course_id) REFERENCES Course (course_id)
  6      ON DELETE CASCADE,
  7      FOREIGN KEY (stud_id) REFERENCES emp (stud_id)
  8      ON DELETE CASCADE);

Table created.

SQL>
SQL> drop table Course cascade constraints;

Table dropped.

SQL> drop table emp cascade constraints;

Table dropped.

SQL> drop table Enrolls_in;

Table dropped.

SQL>

   
    
    
    
  








Related examples in the same category

1.Add Foreign Primary Key
2.Alter table to add primary key and alter another table to add foreign key
3.syntax to reference foreign key
4.Three foreign keys in a table
5.One to many using a primary-key and foreign-key relationship