用的时候,却还要去找文档,麻烦。
语法:
alter table tablename add FOREIGN KEY [
示例:
语法:
alter table tablename add FOREIGN KEY [
id
] (
index_col_name
, ...) REFERENCES
tbl_name
(
index_col_name
, ...).
示例:
a.建立数据库:Create database msg;
b.更改数据库:Use msg;
c.建立表LeaveMSG:Create table LeaveMSG(id int AUTO_INCREMENT primary key not null,author varchar(50),ldate varchar(20),msg text);
d.建立表ReplyMSG:Create 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)