Can foreign key references contain NULL values in PostgreSQL?
For table 1, this INSERT statement will succeed. If you run it 100 times, it will succeed 100 times.
insert into referencing_table values (null);
The same INSERT statement will fail on table 2.
ERROR: null value in column "indexing_table_id" violates not-null constraint DETAIL: Failing row contains (null).
Sometimes you want a foreign keyed column to be nullable because it is not required (just as not every citizen in a citizens table went to a university, so a university_id
column can be null). In other cases, the column should not be null, just as every student lshould be associated with a university_id
.
Therefore, the two referencing_table
s you describe are actually very different, if you consider what you are trying to achieve.