有没有可能在Postgressql中创建一个可空的外键约束?(Is it possible to create a nullable foreign key constraint in Postgre

编程入门 行业动态 更新时间:2024-10-22 08:29:24
有没有可能在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?..

更多推荐

本文发布于:2023-07-05 07:00:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1034426.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:能在   创建一个   create   nullable   Postgressql

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!