Basic MySQL table operations/*********************************/create table USER www.2cto.com (ID int unsigned, USERNAME varchar (10) not null, PASSWORD varchar (32) not null, GENDER tinyint, BIRTHDAY date, www.2cto.com IMAGEURL varchar (30), CREATED_DATE timestamp, STATUS tinyint default 1) engine = innodb, default charset = utf8; alter table USER add constraint PK_USER primary key (ID); alter table USER modify column ID int unsigned not null auto_increment; create unique index IX_USER_USERNAME on USER (USERNAME); create index IX_USER_PASSWORD on USER (PASSWORD); create index IX_USER_CREATED_DATE on USER (CREATED_DATE ); /******** MESSAGE **************/create table MESSAGE (ID int unsigned, USER_ID int unsigned not null, CONTENT varchar (400) not null, CREATED_DATE timestamp, STATUS tinyint default 1) engine = innod B, default charset = utf8; www.2cto.com alter table MESSAGE add constraint PK_MESSAGE primary key (ID); alter table MESSAGE modify column ID int unsigned not null auto_increment; alter table MESSAGE add constraint FK_MESSAGE_USER foreign key (USER_ID) references USER (ID); create index IX_MESSAGE_USER_ID on MESSAGE (USER_ID); create index IX_MESSAGE_CREATED_DATE on MESSAGE (CREATED_DATE ); // note that the engine is set to innodb And the USER_ID type must be the same as the USER (ID) type. Otherwise, the foreign key cannot be added successfully. Author: wangyang2698341