Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does a Foreign Key referencing PK need the NOT NULL constraint?

Does a Foreign Key referencing a Primary Key need the NOT NULL constraint in a PostgreSQL database?
The database is highly normalized and will be very large. I do not wish to add extra constraints that will slow down the queries even more if said queries are unneeded.

like image 435
WolfmanDragon Avatar asked Sep 01 '09 18:09

WolfmanDragon


1 Answers

If you want to be able to represent unknown in the FK column of that table, then make it nullable, if it has to have a value, make it Not Null.

You can have as many records as you want in the referencing table with null FK values. The unique constraint is on the rows in the referenced table (Where the PK is) not on the rows in the referencing table (where the FK is).

like image 175
Charles Bretana Avatar answered Sep 20 '22 05:09

Charles Bretana