Can foreign key references contain NULL values ​​in PostgreSQL?

As an example

create table indexing_table
(
  id SERIAL PRIMARY KEY,
  created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
);

Is there any difference between the following tables?

Table 1:

create table referencing_table
(
  indexing_table_id INTEGER references indexing_table
);

Table 2:

create table referencing_table
(
  indexing_table_id INTEGER references indexing_table NOT NULL
);

Alternatively, in the case of table 1, where there is no restriction NOT NULL, is it allowed to insert records containing values NULL?

+12
source share
2 answers

In 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 not be executed in table 2.

ERROR: null value in column "indexing_table_id" violates not-null constraint
DETAIL: Failing row contains (null).
+28

, , ( , , university_id ). , university_id.

, referencing_table, , , , .

0

All Articles