当前位置: 代码迷 >> Oracle管理 >> oracle数据库创设表主外键语法
  详细解决方案

oracle数据库创设表主外键语法

热度:89   发布时间:2016-04-24 05:15:43.0
oracle数据库创建表主外键语法
比如我有一张A表
a_id是主键

然后用B表的a_id跟A表的a_id创建主外键关系

谢谢~··

------解决方案--------------------
alter table b add
CONSTRAINT fk_a_id
FOREIGN KEY (a_id)
REFERENCES a(a_id)
------解决方案--------------------
alter table a add constraint pk_a primary key(a_id)
------解决方案--------------------
外键reference的字段,一定是unique key的约束的,所以你的a_id需要是主键,或者是unique key

加外键的话
可以直接alter已经建好的表

alter table b add constraint FK_B_AID foreign key(a_id) references a(a_id);

也可以在create里自己指定

create table b(....., constraint FK_B_AID foreign key(a_id) references a(a_id));
------解决方案--------------------
create table a(
a_id int primary key
 );

create table b(
b_id int primary key,
a_id int references a(a_id)
 );



这 怎么搞的这么丑啊
------解决方案--------------------
探讨
比如我有一张A表
a_id是主键

然后用B表的a_id跟A表的a_id创建主外键关系

谢谢~··
  相关解决方案