1. 程式人生 > >外鍵的創建、刪除、查看

外鍵的創建、刪除、查看

一個用戶 lena cda set sql value lte for www

一、創建表的時候創建外鍵

如果表A的主關鍵字是表B中的字段,則該字段稱為表B的外鍵,表A稱為主表,表B稱為從表。外鍵是用來實現參照完整性的,不同的外鍵約束方式將可以使兩張表緊密的結合起來,特別是修改或者刪除的級聯操作將使得日常的維護工作更加輕松。這裏以MySQL為例,總結一下3種外鍵約束方式的區別和聯系。

  這裏以用戶表和用戶組表為例,這是一個典型的多對一關系,多個用戶對應於一個用戶組。

  首先創建用戶組表:

  創建用戶組表

  create table t_group (

  id int not null,

  name varchar(30),

  primary key (id)

  );

  並插入兩條記錄:

  插入記錄

  insert into t_group values (1, Group1);

  insert into t_group values (2, Group2);

  下面創建用戶表,分別以不同的約束方式創建外鍵引用關系:

  1、級聯(cascade)方式

  級聯方式

  create table t_user (

  id int not null,

  name varchar(30),

  groupid int,

  primary key (id),

  foreign key (groupid) references
t_group(id) on delete cascade on update cascade   );

  參照完整性測試

  insert into t_user values (1, qianxin, 1); #可以插入

  insert into t_user values (2, yiyu, 2); #可以插入

  insert into t_user values (3, dai, 3); #錯誤,無法插入,用戶組3不存在,與參照完整性約束不符

  約束方式測試

  insert into t_user values (1, qianxin, 1
);   insert into t_user values (2, yiyu, 2);   insert into t_user values (3, dai, 2);   delete from t_group where id=2; #導致t_user中的2、3記錄級聯刪除   update t_group set id=2 where id=1; #導致t_user中的1記錄的groupid級聯修改為2

  2、置空(set null)方式

  置空方式

  create table t_user (

  id int not null,

  name varchar(30),

  groupid int,

  primary key (id),

  foreign key (groupid) references t_group(id) on delete set null on update set null

  );

  參照完整性測試

insert into t_user values (1, qianxin, 1); #可以插入

  insert into t_user values (2, yiyu, 2); #可以插入

  insert into t_user values (3, dai, 3); #錯誤,無法插入,用戶組3不存在,與參照完整性約束不符

  約束方式測試

  

insert into t_user values (1, qianxin, 1);

  insert into t_user values (2, yiyu, 2);

  insert into t_user values (3, dai, 2);

  delete from t_group where id=2; #導致t_user中的2、3記錄的groupid被設置為NULL

  update t_group set id=2 where id=1; #導致t_user中的1記錄的groupid被設置為NULL

  3、禁止(no action / restrict)方式

  禁止方式

  

create table t_user (

  id int not null,

  name varchar(30),

  groupid int,

  primary key (id),

  foreign key (groupid) references t_group(id) on delete no action on update no action

  );

  參照完整性測試

 

 insert into t_user values (1, qianxin, 1); #可以插入

  insert into t_user values (2, yiyu, 2); #可以插入

  insert into t_user values (3, dai, 3); #錯誤,無法插入,用戶組3不存在,與參照完整性約束不符

  約束方式測試

  

    insert into t_user values (1, qianxin, 1);

  insert into t_user values (2, yiyu, 2);

  insert into t_user values (3, dai, 2);

  delete from t_group where id=2; #錯誤,從表中有相關引用,因此主表中無法刪除

  update t_group set id=2 where id=1; #錯誤,從表中有相關引用,因此主表中無法修改

  註:在MySQL中,restrict方式與no action方式作用相同

二、在表外創建外鍵

  

#創建新聞表
create table news(
nId int(4) not null primary key AUTO_INCREMENT,
ntId INT(4) not null,
ntName varchar(4) not null,
nTitle varchar(4) not null,
nAuthor varchar(4) not null,
nCreatedate datetime not null,
nPicpath varchar(4),
nContent longtext not null,
nModifydate timestamp,
nSummary text not null,
FOREIGN KEY (ntId) REFERENCES Topic(tId)
);
desc news;
#創建評論表
create table `comment`(
cId int(4) not null primary key auto_increment,
cnId int(4) not null,
cContent varchar(50) not null,
cDate timestamp,
cIp varchar(2),
cAuthor varchar(2)
);
#添加外鍵方式1默認名
alter table `comment` add constraint foreign key(cnid) references news(nId);
#添加外鍵方式2指定名
alter table `comment` add constraint fk_news_comment foreign key(cnid) references news(nId);

三、刪除外鍵

#查看創建語言
show create table `comment`;
#查看所有的主鍵和外鍵
SELECT * FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE
#刪除外鍵
alter table tableName drop foreign key foreignKeyName;

外鍵的創建、刪除、查看