首页 > 其他 > 详细

外键的创建、删除、查看

时间:2017-11-19 12:14:26      阅读:240      评论:0      收藏:0      [点我收藏+]

一、创建表的时候创建外键

如果表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;

 

外键的创建、删除、查看

原文:http://www.cnblogs.com/binglong180/p/7859187.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!