Parsing: How to Create a foreign key in MySQL
Example:
1. master table
Drop table if exists 'biao ';
Create table 'biao '(
Id'int (11) not null auto_increment,
Title 'varchar (11) default NULL,
Content 'varchar (11) default NULL,
Primary key ('id ')
) ENGINE = InnoDB default charset = gb2312;
2. Table cotton
The link is 1: N.
Drop table cotton;
Create table cotton (
Id int primary key,
User varchar (11 ),
Email varchar (11 ),
Url varchar (11 ),
Content varchar (11 ),
AddTime date,
Biao_id int,
Constraint FK_biao_id foreign key (blog_id) references biao (id)
) ENGINE = InnoDB default charset = gb2312;
(Note: Be sure to remember varchar (11). Otherwise, an error may occur and the SQL statement can be exported from an existing table ).
The result of this SQL statement exported using SQL-front is:
Drop table if exists 'comment ';
Create table 'comment '(
'Id' int (11) not null,
'User' varchar (11) default NULL,
'Email 'varchar (11) default NULL,
'Url' varchar (11) default NULL,
'Content' varchar (11) default NULL,
'Addtime' date default NULL,
'Blog _ id' int (11) default NULL,
Primary key ('id '),
KEY 'fk _ blog_id '('blog _ id ')
) ENGINE = InnoDB default charset = gb2312;
Alter table 'comment'
Add foreign key ('blog _ id') REFERENCES 'blog '('id ');