有没有可能在Postgressql中创建一个可空的外键约束?(Is it possible to create a nullable foreign key constraint in PostgreSQL?)
我在表'XY'上有一个可空的int8列'z_id'。 'z_id'是'Z'表的主键。 如果在persistense期间提供'z_id',我想要一个约束将'Z'实体映射到'XY'表。
I have a nullable int8 column 'z_id' on a table 'XY'. The 'z_id' is the primary key of the 'Z' table. I'd like to have a constraint that maps the 'Z' entity to the 'XY' table if the 'z_id' is provided during persistense.
最满意答案
如果我正确理解你,你会尝试:
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)你可能有同样的怀疑 ?
If I understand you correctly, you try to:
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)And you probably had same doubdts?..
更多推荐
发布评论