首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >Oracle SQL约束在其他表中相同的值

Oracle SQL约束在其他表中相同的值
EN

Stack Overflow用户
提问于 2018-06-02 02:51:30
回答 2查看 88关注 0票数 2

我需要一个简短的答案,当我有表A,B,C,E时如何进行约束

  • 表E有E_ID (E id是主键)
  • 表B有B_ID,E_ID (E id是外键)
  • 表C有C_ID,E_ID (E id是外键)
  • 表A有A_ID,B_ID,C_ID (B和C id是外键)

<代码>F29

其中id是主键。

我希望constrain确保表A中有C_ID和B_ID具有相同E_ID的记录,并且它应该仍然具有第三范式。

EN

回答 2

Stack Overflow用户

发布于 2018-06-02 02:55:24

您需要以下四个约束:

代码语言:javascript
复制
alter table a add constraint fk1 foreign key (b_id) references b (b_id);
alter table a add constraint fk2 foreign key (c_id) references c (c_id);
alter table c add constraint fk3 foreign key (e_id) references e (e_id);
alter table b add constraint fk4 foreign key (e_id) references e (e_id);
票数 0
EN

Stack Overflow用户

发布于 2018-06-02 02:58:13

一种方法是在表a中重复e_id,然后将其用于外键约束。

请注意此数据模型中唯一键的外键关系:

代码语言:javascript
复制
create table e (eid int primary key);

create table b (bid int primary key, 
                eid int references e(eid),
                unique (bid, eid)
               );

create table c (cid int primary key, 
                eid int references e(eid),
                unique (cid, eid)
               );

create table a (aid int primary key, 
                bid int references b(bid),
                cid int references c(cid),
                eid int references e(eid),
                foreign key (bid, eid) references b(bid, eid),
                foreign key (cid, eid) references c(cid, eid)
               );

Here是一个SQL。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50649489

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档