I want to build a composite primary key made up of foreign keys from two different tables. I'm still pretty green to this…
A great answer was given to building a composite key from multiple foreign keys on this question but I want to do this from multiple tables. I am using Oracle 11g.
foreign key (Name, BoughtFrom, TimeBought)
references the_other_table_name (Name, BoughtFrom, TimeBought)
Imagine that the "references" line actually includes multiple tables. I'm not even sure that the above syntax will work with Oracle.
I have one table "Student" and one table "Parent". It is a many-to-many relationship. My association table is named "Relation" and I would like to make a composite primary key from "PAR_ID" and "STU_ID". Both are foreign keys.
Best Answer
Short version:
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.