`
JAVA天地
  • 浏览: 658098 次
  • 性别: Icon_minigender_1
  • 来自: 太原
文章分类
社区版块
存档分类
最新评论

MySQL中设置外键

阅读更多
用的时候,却还要去找文档,麻烦。
语法:
alter table tablename add FOREIGN KEY [id] (index_col_name, ...) REFERENCES tbl_name (index_col_name, ...).
示例:

a.建立数据库:Create database msg;

b.更改数据库:Use msg;

c.建立表LeaveMSGCreate table LeaveMSG(id int AUTO_INCREMENT primary key not null,author varchar(50),ldate varchar(20),msg text);

d.建立表ReplyMSGCreate table ReplyMSG(id int AUTO_INCREMENT primary key not null,lid int not null,rauthor varchar(50),rdate varchar(20),rmsg text);

e.建立外键关系:alter table replymsg add foreign key (lid) references leavemsg(id)

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics