Composite primary key from multiple tables / multiple foreign keys
Short version:
CREATE TABLE Relation
( stu_id INT NOT NULL REFERENCES Student,
par_id INT NOT NULL REFERENCES Parent,
PRIMARY KEY (stu_id, par_id)
) ;
Long version:
Why use short forms for names, like
stu_id
andpar_id
? Why notstudent_id
? Saving typing 3-4 characters? How will you differentiate between parent_id and parameter_id? Or school_id and schoolmaster_id?The name
"Relation"
is not very descriptive for a relationship. (Note also that in relational model terminology, "relation" has a meaning very close to "table".) I couldn't come with a good name though, so we could use"Guardian"
or"Student_Parent"
(this combination is often used in intersection tables)The short version above is just an example. While it is working, it uses a lot of shortcuts, like the inline references. It's far better in my opinion, to name all constraints and declare all (primary, unique, foreign key and check) constraints after the column declarations, like in the long version below.
It's also good to choose some naming conventions anduse them consistently in all the tables, e.g.
Tablename_PK
for the primary keys,ReferencedTable_referencingTable_FK
for the foreign keys,Something_UQ
for the unique constraints, etc.
CREATE TABLE Guardian
( -- columns
student_id INT NOT NULL,
parent_id INT NOT NULL,
-- constraints
CONSTRAINT Guardian_PK -- the name of the PK constraint
PRIMARY KEY (student_id, parent_id),
CONSTRAINT Student_Guardian_FK -- the name of the FK constraint
FOREIGN KEY (student_id)
REFERENCES Student (student_id)
ON UPDATE CASCADE -- the actions of the FK
ON DELETE RESTRICT,
CONSTRAINT Parent_Guardian_FK -- second FK
FOREIGN KEY (parent_id)
REFERENCES Parent (parent_id)
ON UPDATE CASCADE
ON DELETE RESTRICT
) ;