根据其他表添加约束到表Postgresql

发布于 2025-02-01 02:16:22 字数 788 浏览 3 评论 0原文

我在Postgres数据库中有这三个表:

create table staff ( 
e_id int primary key,
first_name text,
last_name text,
job text,
branch_name text);

create table temp_staff ( 
t_id int primary key,
t_first_name text,
t_last_name text,
t_job text );

create table replacements( 
e_id int ,
t_id int,
from_date date,
until_date date,
primary key(e_id, t_id, from_date) );

现在,我想在替换表中添加约束,因此只有在Staff Tabt中存在E_ID和T_ID中存在E_ID和T_ID的替换,并且T_ID存在于temp_staff表。 我尝试了这个问题:

alter table replacements
add constraint check_existence
check(exists (select 1 from staff where e_id = e_id) and exists (select 1 from temp_staff where t_id = t_id));

并遇到了错误:

ERROR:  cannot use subquery in check constraint

请提前提供任何帮助。

I have these three tables in postgres database:

create table staff ( 
e_id int primary key,
first_name text,
last_name text,
job text,
branch_name text);

create table temp_staff ( 
t_id int primary key,
t_first_name text,
t_last_name text,
t_job text );

create table replacements( 
e_id int ,
t_id int,
from_date date,
until_date date,
primary key(e_id, t_id, from_date) );

Now I want to add a constraint to the replacements table so you can insert a replacement with e_id and t_id only if e_id exists in the staff table and t_id exists in the temp_staff table.
I tried this:

alter table replacements
add constraint check_existence
check(exists (select 1 from staff where e_id = e_id) and exists (select 1 from temp_staff where t_id = t_id));

And got the error:

ERROR:  cannot use subquery in check constraint

Thanks in advance for any help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文