外键必须要建在主表的主键之上

来源:互联网 发布:在线建立数据库 编辑:程序博客网 时间:2024/05/22 13:24

SQL>
SQL>
SQL>
SQL>
SQL> create table a(id number,name varchar2(32));

Table created.

SQL> create table b(pid number,name varchar2(32));

Table created.

SQL> alter table b add constraint FK_B foreign key (pid) references a (ID) ;
alter table b add constraint FK_B foreign key (pid) references a (ID)
                                                                  *
ERROR at line 1:
ORA-02270: 此列列表的唯一关键字或主键不匹配


SQL> alter table a add constraints pk_a primary key(id);

Table altered.

SQL> alter table b add constraint FK_B foreign key (pid) references a (ID) ;

Table altered.

SQL>

 

 

1 外键是针对从表的

2 主表一定要有主键,否则在从表中建不了外键

 

0 0
原创粉丝点击