是否有可能在PostgreSQL中创建一个可以为空的外键约束?

时间:2018-05-31 07:19:03

标签: sql postgresql

我有一个可以为空的int8列' z_id'在桌子上' XY'。 ' z_id'是Z' Z'的主要关键。表。我希望有一个约束来映射' Z'实体到' XY'表格如果' z_id'在持久期间提供。

1 个答案:

答案 0 :(得分:2)

如果我理解正确,你会尝试:

t=# create table z(i int primary key);
CREATE TABLE
t=# insert into z values (1);
INSERT 0 1
t=# create table xy(i int);
CREATE TABLE
t=# insert into xy values(1),(null);
INSERT 0 2
t=# alter table xy add constraint fk foreign key (i) references z(i);
ALTER TABLE
t=# select * from xy;
 i
---
 1

(2 rows)

你可能有same doubdts

相关问题