1.创建一个新的表
create table if not exists goods_cates(id int unsigned key auto_increment,name varchar(40) not null) ;
2.把一个表的数据插入另一个表
insert into good_cates (name) values select cate_name from goods group by cate_name ;
3 同步表数据
update goods as g inner join goods_cates as c on g.cate_name = c.name set g.cate_name = c.id;
4 更改表结构
alter table goods change cate_name cate_id int unsigned not null;
5 外键
alter table goods add foreign key (cate_id) references goods_cates(id);
网友评论