建立外鍵關系:先建主表再見從表;
主表:
create table zhu
(
code int parimary key,
name varchar(20)
) ;
從表:
create table cong
(
code int primary key,
name varchar(20),
zhu int,
foreign key【 代表外鍵】 (zhu) references【引用】 zhu ( code)[ 建立外鍵關系 ]
)
樣式:foreign key(列名) references 主表名(列名) 外鍵
zhu表中的code與cong表中的zhu建立了主外鍵關系
----------------------------------------------------------------------------------------------------------
4.增加外鍵約束
alter table sc
add constraint fk_student(外鍵約束的名稱)
foreign key(sno)//將sc表中的sno設為外鍵
references student(sno);關聯student表的sno主鍵