[Backend Essay] MySQL operation statement record

The sql statement is not case-sensitive
show bases;
--No. comment
/**/Multi-line comment
DDL definition
DML delete
DQL query
DCL permission control language
1. DDL operation database
(1) Query
SHOW DATABASES;
(2) Create
CREATE DATABASES;/ /Create database
CREATE DATABASES IF NOT EXISTS database name;
(3) Delete
DROP DATABASES database name; //Delete database
DROP DATABASE IF EXISTS database name //Delete database, delete if it exists
(4) Use database
SELEST DATABASE();/ /View the currently used database
USE database name; //Use database
2, DDL operation table
(1) to create Create create
table table name (
field name 1 data type 1,
field name 2 data type 2,
field name n data type n
) ;
(2) Query Retrieve
show tables; // Query all table names under the current database
desc table name; // Query table structure
(3) Modify updata
alter table table name rename to new table name; //Modify table name
alter table table name add column name data type; //Add a column
alter table table name modify column name new data type; modify data type
alter table table name change column Name new column name, new data type; modify column name and data type; alter
table table name drop column name; delete column
(4) Delete Delete
drop table table name; delete table
drop table if exists indicates; //Delete table judgment version
3. DML delete data
(1) Add data
insert into table name (column name 1, column name 2,...) values ​​(value 1, value 2...);//Add data to the specified column
insert into table name values ​​( value 1, value 2...);//Add data to all columns
insert into table name (column name 1, column name 2,....) values ​​(value 1,...) (value 1...) ); //Batch add
insert into table name values ​​(value 1, value 2), (value 1,...);
(2) Modify data
update table name set column name 1=value 1, column name 2=value 2 ,...[where condition];
4. DQL database query operation
select
field list
from
table name list
where
condition list
group by
group field
having
post-group conditions
order by
sort field
limit
paging limit
(1) basic query
select field list from table name;
select * from table name; --Query all data
select destinct field list from table name; //remove duplicates Record
AS: AS can also be omitted // Alias
​​(2) Conditional query
where field operator condition;
select field list from table name where condition list;
select * from students where name like '马%'; // Query people with the surname Ma Information
select * from students where name like '_花%'; //Query information where the second character is flower
select * from students where name like '%马%'; Query information about students with horse in their names
(3) Sorting Query
select field list from table name order by sorting field name 1 [sorting method 1], sorting field name 2 [sorting method 2]...; sorting method
asc
: ascending order (default value i)
desc: descending order
*** Aggregation function
count (column name) counts quantity
max (column name) maximum value
min (column name) minimum value
sum (column name) sum
avg (alias) average
aggregate function syntax
select aggregate function name (column name) from table;
(4) group query
select field list from Table name [where conditional qualification before grouping] group by grouping field name [having conditional filtering after grouping];
execution order: where>aggregation function>having
example: select avg(math) from stu group by sex;//from stu table The average score in the middle examination is classified by sex.
The result
is 91
for men and 72.6
for women. Query the average math score of men and women, as well as their respective numbers. Requirements: resist 70 points and do not participate in grouping. After grouping, the number of people is greater than 2.
Select sex,avg(math),count(* ) from stu where math > 70 group by sex having count(*) > 2;
where cannot use aggregate function having can
(5) paging query
select field list from table name limit starting index, query entry number
starting index = (current Page number - 1) * Number of items displayed on each page
select * from stu limit 0, 3; // Starting from 0, query three pieces of data
select * from stu limit 0, 3; // Display three pieces of data on each page, query page 1 data
select * from stu limit 3, 3; // Display three pieces of data per page, query the data on the second page
select * from stu limit 6, 3; // Display three pieces of data per page, query the data on the third page
limit is the dialect of mysql


operator 

Guess you like

Origin blog.csdn.net/qq_63499305/article/details/130650685