拆表
先创建表格
create table if not exists goods_cates(
id int unsigned primary key auto_increment,
name varchar(40) not null,
);
查询goods表中商品的种类
select cate_name from goods group by cate_name;
将分组结果写入到goods_cates数据表
insert into goods_cates (name) select cate_name from goods group by cate_name;
将第一张表和竖着的第二张的关联
通过alter table语句修改表结构
alter table goods
change cate_name cate_id int unsigned not null;
(先将数据类型改成一致的)
然后再通过外键将他们关联起来
alter table goods add foreign key (cate_id) references goods_cates(id);
作业:第一张表的与第三张表的拆分及关联
拆分
先创建表格
create table if not exists goods_brand(
id int unsigned primary key auto_increment,
name varchar(40) not null
);
查询goods表中商品品牌
select brand_name from goods group by brand_name;
将分组结果写入到goods_brand数据表
insert into goods_brand (name) select brand_name from goods group by brand_name;
关联
alter table goods
change brand_name brand_id int unsigned not null;
alter table goods add foreign key (brand_id) references goods_bramd(id);