当前位置:网站首页>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;

 Insert picture description here

  • hold cate_id Replace the numbers in this column with the corresponding goods Medium cate Column
     Insert picture description here
  • 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;

 Insert picture description here

  • 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;

 Insert picture description here

  • Modify the structure of the product table
#  View the subject structure 
desc goods;

 Insert picture description here

#  Modify table structure 
alter table goods change cate cate_id int unsigned,change brand_name brand_id int unsigned;

 Insert picture description here

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;

 Insert picture description here

  • 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;

 Insert picture description here
 Insert picture description here

  • Now there are no foreign keys , But two more key, Let's take this key Also deleted
     Insert picture description here
     Insert picture description here
原网站

版权声明
本文为[strive_ one]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202280514548655.html