准备数据
创建数据表
—— 创建‘京东’数据库
create database jing_dong charset=utf8;
—— 使用数据库
use jing_dong;
—— 创建一个商品数据表
create table goods(
id int unsigned primary key auto_increment not null,
name varchar(150) not null,
cate_name varchar(40) not null,
brand_name varchar(40) not null,
price decimal(10,3) not null,
is_show bit not null default 1,
is_saleoff bit not null default 0
);
create table goods(id int unsigned primary key auto_increment not null,name varchar(150) not null,cate_name varchar(40) not null,brand_name varchar(40) not null,price decmial(10,3) not null,is_show bit not null default 1,is_saleoff bit not null default 0);
插入数据
—— 向goods表中插入数据
insert into goods values(0,’r510vc 15.6英寸笔记本’, ‘笔记本’,’华硕’,’3399’,default, default);
insert into goods values(0,’y440n 114.0英寸笔记本’, ‘笔记本’,’联想’,’4999’,default, default);
insert into goods values(0,’g150th 15.6英寸笔记本’, ‘游戏本’,’雷神’,’8499’,default, default);
insert into goods values(0,’x550cc 15.5英寸笔记本’, ‘笔记本’,’华硕’,’3399’,default, default);
insert into goods values(0,’x240 超极本’, ‘超极本’,’联想’,’4880’,default, default);
insert into goods values(0,’u330p 13.3英寸超级本’, ‘超级本’,’联想’,’4299’,default, default);
insert into goods values(0,’svp13226scb 触控超级本’, ‘超级本’,’索尼’,’7999’,default, default);
insert into goods values(0,’iPad mini 7.9英寸平板电脑’, ‘平板电脑,’苹果’,’1998’,default, default);
insert into goods values(0,’iPad air 9.7英寸平板电脑’, ‘平板电脑,’苹果’,’3388’,default, default);
insert into goods values(0,’iPad mini 配备 retina 显示屏’, ‘平板电脑,’苹果’,’2788’,default, default);
insert into goods values(0,’ideacentre c340 20英寸 一体电脑’, ‘台式机’,’联想’,’3499’,default, default);
insert into goods values(0,’vostro 3800-r1206 台式电脑’, ‘台式机’,’戴尔’,’2899’,default, default);
insert into goods values(0,’iMac me086ch/a 21.5英寸一体电脑’, ‘台式机’,’苹果’,’9188’,default, default);
insert into goods values(0,’at7-7414lp 台式电脑 linux’, ‘台式机’,’弘基’,’3699’,default, default);
insert into goods values(0,’z220sff f4f06pa工作站’, ‘服务器/工作站’,’惠普’,’4288’,default, default);
insert into goods values(0,’poweredge ii服务器’, ‘服务器/工作站’,’戴尔’,’5388’,default, default);
insert into goods values(0,’mac pro专业级台式电脑’, ‘服务器/工作站’,’苹果’,’28888’,default, default);
insert into goods values(0,’hmz-t3w 头戴显示设备’, ‘笔记本配件’,’索尼’,’6999’,default, default);
insert into goods values(0,’商务双肩背包’, ‘笔记本配件本’,’索尼’,’99’,default, default);
insert into goods values(0,’x3250 m4机架式服务器’, ‘服务器/工作站’,’ibm’,’6888’,default,default);
insert into goods values(0,’商务双肩背包’, ‘笔记本配件本’,’索尼’,’99’,default, default);
—— 创建商品分类表
create table goods_cates(id int unsigned primary key auto_increment not null, name varchar(40) not null);
— 把goods表中的cate_name的分组结果 插入到 goods_cates中
insert into goods_cates (name) select cate_name from goods group by cate_name;
—- 同步表数据
update goods as g inner join goods_cates as c on g.cate_name=c.name set g.cate_name=c.id;
—- goods表中的cate_name 改为 cate_id 类型为 int unsigned 约束 not null
alter table goods change cate_name cate_id int unsigned not null;
—- goods表中cate_id设置外键
alter table goods add foreign key(cate_id) references goods_cates(id);
——— 创建商品品牌表/把goods表中的brand_name的分组结果 插入到 goods_brands中
create table goods_brands(id int unsigned primary key auto_increment not null, name varchar(40) not null) select brand_name as name from goods group by brand_name;
— 把goods表中的brand_name的分组结果 插入到 goods_brands中
—- insert into goods_brands(name) select brand_name from goods group by brand_name;
—- 同步表数据
update goods as g inner join goods_brands as b on g.brand_name=b.name set g.brand_name= b.id;
—- goods表中的brand_name 改为 brand_id 类型为 int unsigned 约束 not null
alter table goods change brand_name brand_id int unsigned not null;
—- goods表中brand_id设置外键
alter table goods add foreign key(brand_id) references goods_brands(id);
—- 删除goods表中的外键
alter table goods drop foreign key goods_ibfk_1;
—- 删除goods表中的外键
alter table goods drop foreign key goods_ibfk_2;
来源:https://www.cnblogs.com/peijz/p/12363560.html