当前位置:网站首页>Mysql database crud operation
Mysql database crud operation
2022-06-13 05:15:00 【strive_ one】
Database CRUD operation
- create: Add data
- read: Reading data
- update: Modifying data
- delete: Delete data
Prepare the data
# Create database
create database select001 charset=utf8;
# Create table
create table goods(
id int unsigned primary key auto_increment not null,
name varchar(150) not null, -- Name of commodity
cate varchar(40) not null, -- Commodity form
brand_name varchar(40) not null, -- Brand of goods
price decimal(10,3) not null default 0, -- prices for goods
is_show bit not null default 1, -- Is it on the shelf
is_saleoff bit not null default 0 -- Is there a discount
);
# Insert data into table
insert into goods values(0,'r510vc 15.6 Inch notebook ',' The notebook ',' ASUS ','3399',default,default);
insert into goods values(0,'y400n 14.0 Inch Laptop ',' The notebook ',' lenovo ','4999',default,default);
insert into goods values(0,'g150th 15.6 Inch game book ',' Game book ',' The thor ','8499',default,default);
insert into goods values(0,'x550cc 15.6 Inch notebook ',' The notebook ',' ASUS ','2799',default,default);
insert into goods values(0,'x240 Ultra extreme copy ',' Ultrabook ',' lenovo ','4999',default,default);
insert into goods values(0,'u330p 13.3 Inch ultrabook ',' Ultrabook ',' lenovo ','4299',default,default);
insert into goods values(0,'svp13226scb Touch ultrabook ',' Ultrabook ',' SONY ','7999',default,default);
insert into goods values(0,'ipad mini 7.9 Inch tablet ',' The tablet ',' Apple ','1998',default,default);
insert into goods values(0,'ipad air 9.7 Inch tablet ',' The tablet ',' Apple ','3388',default,default);
insert into goods values(0,'ipad mini Equipped with retina display ',' The tablet ',' Apple ','2788',default,default);
insert into goods values(0,'ideacentre c340 20 Inch all in one computer ',' Desktop computer ',' lenovo ','3499',default,default);
insert into goods values(0,'vostro 3800-r1206 Desktop computer ',' Desktop computer ',' Dell ','2899',default,default);
insert into goods values(0,'imac me086ch/a 21.5 Inch all in one computer ',' Desktop computer ',' Apple ','9188',default,default);
insert into goods values(0,'at7-7414lp Desktop computer linux )',' Desktop computer ',' Acer ','3699',default,default);
insert into goods values(0,'z220sff f4f06pa The workstation ',' The server / The workstation ',' HP ','4288',default,default);
insert into goods values(0,'poweredge ii The server ',' The server / The workstation ',' Dell ','5388',default,default);
insert into goods values(0,'mac pro Professional desktop computers ',' The server / The workstation ',' Apple ','28888',default,default);
insert into goods values(0,'hmz-t3w Head mounted display device ',' Notebook accessories ',' SONY ','6999',default,default);
insert into goods values(0,' Business backpack ',' Notebook accessories ',' SONY ','99',default,default);
insert into goods values(0,'x3250 m4 Rack servers ',' The server / The workstation ','ibm','6888',default,default);
insert into goods values(0,'hmz-t3w Head mounted display device ',' Notebook accessories ',' SONY ','6999',default,default);
insert into goods values(0,' Business backpack ',' Notebook accessories ',' SONY ','99',default,default);
- Query all products with prices greater than the average price , And in descending order of price
select * from goods where price > (select avg(price) from goods) order by price desc;
- The query type is " Ultra extreme copy " The price of your goods
select price from goods where cate = " Ultra extreme copy " order by price desc;
Find insert data and connect update data
- Now split the above table into two tables :
create table if not exists goods_cates(cate_id int unsigned primary key auto_increment, cate_name varchar(40));
- Inquire about goods All the records of the table , And press “ Category ” grouping
select cate from goods group by cate;
- Insert the data just queried into goods_cates In this list
insert into goods_cates(cate_name) select cate from goods group by cate;
- hold cate_id Replace the numbers in this column with the corresponding goods Medium cate Column
- adopt goods_cates Data table to update goods surface
update goods as g inner join goods_cates as c on g.cate = c.cate_name set g.cate = c.cate_id;
- Next, replace the brand column with the same method
# Create tables and write data
create table goods_brands(id int unsigned primary key auto_increment not null, brand_name varchar(40)) select brand_name from goods group by brand_name;
# Add the new table's id The field value is written to the... In the corresponding product table brand_name Field
update goods as g inner join goods_brands as b on g.brand_name = b.brand_name set g.brand_name = g.id;
- Modify the structure of the product table
# View the subject structure
desc goods;
# Modify table structure
alter table goods change cate cate_id int unsigned,change brand_name brand_id int unsigned;
Multiple tables join queries
# Query the details of all products ( Inside , Left , The right connection )
# Connect multiple tables
select * from goods as g inner join goods_cates as c on g.cate_id = c.cate_id inner join goods_brands as b on g.brand_id = b.id;
- Left connection , The first table on the left is the main table .
- Right connection , The last table is the main table , The others are from the table .
Creation and deletion of foreign key constraints
- Add a foreign key constraint
# Add a foreign key constraint
alter table goods add foreign key (brand_id) references goods_brands(id);
alter table goods add foreign key (cate_id) references goods_brands(cate_id);
- If the addition fails , See if there is a problem with the data itself
- Foreign keys can detect the validity of data , Do not delete the wrong data , Do not modify the wrong data
- Be careful : The data type of the field to which the foreign key is added must be consistent with the data type of the field of the associated table .
- Delete foreign key constraint : The name of the foreign key that needs to be deleted :
# Delete foreign key constraint
alter table goods drop foreign key goods_ibfk_1;
- Now there are no foreign keys , But two more key, Let's take this key Also deleted
边栏推荐
- Clause 33: decltype is used for auto & type formal parameters, with std:: forward
- Clause 34: lambda is preferred over std:: bind
- Implementing the driver registration initcall mechanism on stm32
- MySQL log management and master-slave replication
- All blog navigation
- C language learning log 1.19
- Spice story
- C language learning log 10.19
- ZABBIX wechat alarm
- UNO
猜你喜欢
Pychart error resolution: process finished with exit code -1073741819 (0xc0000005)
Case - random numbers without repetition (HashSet and TreeSet)
Customer information management system - C language
Hidden implementation and decoupling, knowing Pimpl mode
Article 29: assuming that the mobile operation does not exist, is expensive, and is not used
System file interface open
Case - simulated landlords (upgraded version)
metaRTC4.0稳定版发布
Chapter 15 mechanism: Address Translation
Bomb disposal cat
随机推荐
Std:: Map empty example
行情绘图课程大纲1-基础知识
OpenCV中的saturate操作(饱和操作)究竟是怎么回事
通过命令行创建harbor镜像库
[multithreading] thread pool core class -threadpoolexecutor
Chapter 13 abstraction: address space
Std:: Map initialization
QT realizes message sending and file transmission between client and server
Case - count the number of occurrences of each string in the string
Case - recursive factorial (recursive)
External sort
Detailed explanation of R language sparse matrix
Dynamic programming - longest common substring
Search DFS and BFS
C language learning log 2.19
C language learning log 1.17
SQL table columns and statements of database
ZABBIX wechat alarm
First assessment
QT interface rendering style