外键引用在PostgreSQL中可以包含NULL值吗? [英] Can foreign key references contain NULL values in PostgreSQL?

查看:160
本文介绍了外键引用在PostgreSQL中可以包含NULL值吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如

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

下表之间有区别吗?

表1:

create table referencing_table
(
  indexing_table_id INTEGER references indexing_table
);

表2:

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

或者,在表1的情况下,没有 NOT NULL 约束,是否允许我们插入包含 NULL 值的记录?

Alternatively, in the case of Table 1, where there is no NOT NULL constraint, are we allowed to insert records containing NULL values?

推荐答案

对于表1,此INSERT语句将成功。如果将其运行100次,它将成功100次。

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);

同一INSERT语句将在表2上失败。

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).

这篇关于外键引用在PostgreSQL中可以包含NULL值吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆