Skip to content
Advertisement

Creating a table with 2 primary keys and two foreign keys referring to 2 different tables with same constraint name

For this particular schema:

This is the schema

I have created the department student and subject tables now the problem right now arises in the creation of the mark table.

It is specified that there are 2 primary keys and 2 foreign keys , but as the title suggests creating 2 foreign keys referring to 2 different tables having the same constraint name seems impossible as per my understanding by surfing on the internet and reading on few threads here.

Is there any way to do this ? I want to have both the foreign key’s constraint name to be “fk”.

This code pops with a identifier error:

But even if i create 2 constraints with different name test cases fail. Is there a solution?

This is department table

This is student table

This is staff table

Advertisement

Answer

You have a composite primary key – on more than one column – which is fine; but you can’t have a composite foreign key as the student table doesn’t have a subject_id column – hence it giving you an invalid-identifier error.

You need two foreign keys, something like:

The constraint names have to be unique, both within and across tables in the same schema, and something more descriptive than ‘pk’ or ‘fk’ would be sensible anyway.

db<>fiddle

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement