mysql 外键 级联

主表

-- 创建用户信息表
create table userinfo ( 
	userid int primary key not null auto_increment COMMENT '主键',
    username varchar (20) not null unique comment '用户名',
    userpwd varchar (20) not null comment '用户密码' ,
    sex boolean not null default true comment '性别' ,
    idiograph varchar (50) comment '个性签名' ,
    address varchar(100) comment '地址' ,
    job varchar(10) comment '职位' ,
    studenttime int not null default 0 comment '学习时长单位分' ,
	integral int not null default 0 comment '积分' ,
    solicitude int not null default 0 comment '关注量' ,
    follower int not null default 0 comment '粉丝量' ,
    headportrait varchar(100) comment '头像' ,
    email varchar(20) comment '邮箱'
);  

  外键连接主表

-- 粉丝表
create table follower(
	solicitudeid int not null comment '用户id',
    followerid int not null comment '关注的用户id',
    -- 设置外键foreign key(solicitudeid,主键references userinfo(userid),当触发某种事件on delete,执行的操作CASCADE,这里是当删除主表的时候就清除这里的相关数据
	foreign key(solicitudeid) references userinfo(userid) on delete CASCADE, 
    foreign key(followerid) references userinfo(userid) on delete CASCADE
);

  粉丝表有个外键去连接用户信息表

当用户信息表中的某个数据被删除时会触发粉丝表的on delete的cascade,会清除掉相关的数据

posted @ 2016-11-25 16:11  有女朋友的程序猿  阅读(214)  评论(0编辑  收藏  举报