Insert, update, create, modify table data

1. Create a product classification table

Thinking

Currently there is only one goods table, we want to add a product classification information, such as: mobile device classification information, only through the goods table can not complete the addition of product classification, so how to add product classification information?

answer:

  1. Create a commodity classification table, and add the commodity classification information in the goods table to the table.
  2. Change the category name in the goods table to the corresponding category id in the product category table

 

1. Create a product classification table

-- 创建商品分类表
create table good_cates(
    id int not null primary key auto_increment, 
    name varchar(50) not null
);

 

2. Add the product category in the goods table to the product category table

-- 查询goods表中商品的分类信息
select cate_name from goods group by cate_name;

-- 将查询结果插入到good_cates表中
insert into good_cates(name) select cate_name from goods group by cate_name;

-- 添加移动设备分类信息
insert into good_cates(name) values('移动设备');

Description:

  • insert into .. select .. means: insert the query result into the specified table, that is, table copy.

 

Second, update the product classification information in the goods table

Above we have created a product classification table (good_cates), and completed the insertion of product classification information. Now we need to update the product classification information in the goods table and change the product classification name to the negotiation category id.

Next we implement the second step:

  • Change the category name in the goods table to the corresponding category id in the product category table
-- 查看goods表中的商品分类名称对应的商品分类id
select * from goods inner join good_cates on goods.cate_name = good_cates.name;

-- 把该语句中from 后的语句理解为一张虚表  
update goods g inner join good_cates gc on g.cate_name=gc.name set g.cate_name=gc.id;

 

Three, create a table and add data to a field

1. Thinking

In the previous lesson, we completed the creation of the product classification table (good_cates) and the addition of product classification information, and changed the product classification name in the product table (goods) to the corresponding product classification id. If we want to add a brand, For example, the brand information of Shuang Feiyan cannot be added through the goods table. How to add brand information?

answer:

  1. Create a brand table and add the brand information in the goods table to the table.
  2. Change the brand name in the goods table to the corresponding brand id in the brand table

 

1. Create a brand table

-- 查询品牌信息 
select brand_name from goods group by brand_name;

-- 通过create table ...select来创建数据表并且同时插入数据
-- 创建商品分类表,注意: 需要对brand_name 用as起别名,否则name字段就没有值
create table good_brands (     
id int unsigned primary key auto_increment,     
name varchar(40) not null) select brand_name as name from goods group by brand_name;

Description:

  • create table .. select column name: means to create a table and insert data

 

2. Update the brand information in the goods table

-- 将goods表中的品牌名称更改成品牌表中对应的品牌id
update goods as g inner join good_brands gb on g.brand_name = gb.name set g.brand_name = gb.id;

 

Fourth, modify the goods table structure

At present, we have changed the product classification and brand information in the good table to product classification id and brand id. Next, we need to change the cate_name and brand_name fields to cate_id and brand_id fields, and the types to int types.

-- 查看表结构
desc goods;
-- 通过alter table语句修改表结构
alter table goods change cate_name cate_id int not null, change brand_name brand_id int not null;

Description:

  • Alert table can modify multiple field information at the same time

 

V. Summary

  • To complete the table copy, you can use: insert into .. select .. SQL statement
  • To connect and update the data in the table, use: update .. join .. statement
  • To create a table and insert data into the field use: create table .. select statement
  • To modify the table structure, you can use: alter table statement, multiple modified fields are separated by commas

Guess you like

Origin blog.csdn.net/weixin_48135624/article/details/115240581