当前位置:网站首页>[MySQL] basic database operation

[MySQL] basic database operation

2022-06-12 14:16:00 Speech unrecognized

1 database Additions and deletions

/*******************************************************/


#  increase :  Create database 
create database if not exists sun character set utf8;

#  Delete database 
drop database sun_db;

#  Change :  Using a database 
use sun;

#  check :  Query the database in use   Query all databases 
show databases ;
select database();

/*******************************************************/

2 Database table Additions and deletions

/*******************************************************/
#  increase :  Add table  create table  Table name  ( Field   data type   constraint )
create table cs(
    uid int primary key,
    name varchar(100) not null ,
    age int
);

#  Delete :  Delete database tables 
drop table cs;

#  check :  Inquire about   Database table 
show tables ;
desc cs1;

#  Change :  Modify database table name 
rename table cs to cs1;

/*******************************************************/



3 Data table fields : Additions and deletions

/*******************************************************/
#  increase  :  Add fields 
alter table cs add `desc` varchar(20);

#  Delete  :  Delete field 
alter table cs drop age;

#  Change :  Modify field name 
alter table cs change age ages int;
alter table cs change ages age int not null unique ;
alter table cs change uid uid int auto_increment;
#  check :  Fields in the query table 
desc cs;
/*******************************************************/

4 Data sheet data : Additions and deletions

/*******************************************************/
#  increase :  Add data 
insert into cs (uid, name, `desc`) values (null,' The kid ',' handsome ');
insert into cs values (null,' The kid 1',' frigging awesome ');
insert into cs values (null,' The kid 2','8888'),(null,' The kid 3','8888'),(null,' The kid 4','8888');
insert into cs values (null,' The kid 5','8888'),(null,' The kid 6','8888'),(null,' The kid 7','8888');

#  Delete :  Delete data 
delete from cs where uid=3;
delete from cs;  #  Be careful :  Delete all   Do not reset the primary key auto increment 
truncate cs; #  Be careful :  Delete all   Reset primary key auto increment 

#  Change :  Modifying data 
update cs set `desc`='9999' where uid = 5;
update cs set `desc`='9999'; #  Be careful :  Without conditions   All modified 

#  Inquire about 
select * from product;
select p.pname as " goods ",p.price as " Price " from product as p ;
select pname as " goods ",price as " Price " from product;
#  Conditions of the query  where
select * from product where price > 300;
select * from product where price >= 300;
select * from product where price < 300;
select * from product where price <= 300;
select * from product where price != 300;
select * from product where price <> 300;

#  Logical query  and or not
select * from product where price >200 and price<3000;
select * from product where price <200 or price>3000;
select * from product where not (price>200 and price<3000);

#  Empty judgment 
select * from product where category_id is null;
select * from product where category_id is not null;

#  Sort query 
select * from product order by price;
select * from product order by price,category_id;
select * from product order by price desc ,category_id desc ;
select * from product order by price asc,category_id asc;

#  Fuzzy query  between and ,like % _ ,in
select * from product where price between 200 and 3000;
select * from product where price in (200,600);
select * from product where pname like "__ Si ";
select * from product where pname like "% Si %";
select * from product where pname like "% Hegemony %";
select * from product where pname like "___";

#  Aggregate query  count max min sum avg
select count(*) from product;
select count(*) from product where price>300;
select max(price) from product;
select min(price) from product;
select sum(price) from product;
select avg(price) from product;
select round(avg(price),5) from product;
#  Group query  group by having  Conditions 
select category_id from product group by category_id;
select category_id,sum(price) from product group by category_id;
select category_id,sum(price) from product group by category_id having sum(price)>300;

#  Paging query  limit m,n
select price,pname from product where price>300 order by price desc ;
select price,pname from product where price>300 order by price desc limit 0,3;

/*******************************************************/

原网站

版权声明
本文为[Speech unrecognized]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/163/202206121407442697.html