sql语句格式:
1. 添加外键约束:alter table 从表 add constraint 外键(形如:FK_从表_主表) foreign key (从表外键字段) references 主表(主键字段);
如:alter table t_book add constraint `fk` foreign key (`bookTypeId`) references t_booktype(`id`);
或者在创表时直接加上
CREATE TABLE t_book(
id int primary key auto_increment,
bookName varchar(20),
author varchar(10),
price decimal(6,2),
bookTypeId int,
constraint `fk` foreign key (`bookTypeId`) references `t_bookType`(`id`)
);
2、新增列
ALTER TABLE `trn_comment_msg`
ADD COLUMN `content` VARCHAR(1000) NOT NULL COMMENT '消息内容' AFTER `createUserId`,
ADD COLUMN `courseName` VARCHAR(20) NOT NULL COMMENT '课程名称' AFTER `content`;
3、历史数据迁移
-- trn_course_comment历史数据迁移 update trn_comment_msg tcm inner join trn_course_comment tcc on tcm.comId=tcc.comId inner join trn_course tc on tc.courseId=tcc.courseId set tcm.content=tcc.content,tcm.courseName=tc.courseName