mysql中设置外键的方式
☸☸ MySQL中设置外键的两种方式
-
前置条件(创建主表 t_comment)
create table t_comment( id int primary key auto_increment , title varchar(10) not null, contents varchar(100) not null );
-
使用外键约束(创建副标 t_reply)
1️⃣ create table 时添加外键约束💕
create table t_reply( id int primary key auto_increment, contents varchar(100) not null, c_id int, foreign key(c_id) references t_comment(id) );
注:指定外键名称(fk_jcjl)可以使用如下代码
constraint fk_jcjl foreign key(c_id) references t_comment(id)
2️⃣ alter table 时添加外键约束的💕
create table t_reply( id int primary key auto_increment, contents varchar(100) not null, c_id int ); alter table t_reply add foreign key(c_id) references t_comment(id);
上一篇:
Mongo时间戳转日期以及日期分组
下一篇:
MAC安装Mysql超详细完整教程