Summary of MySQL study notes (xiaobai must see + basics)

DDL: Database Schema Definition Language DDL (Data Definition Language) is a language used to describe the real-world entities to be stored in the database.
DML: Data Manipulation Language (DML), through which users can implement basic operations on the database.
DCL: Data Control Language (Data Control Language) In SQL language, it is an instruction that can control data access rights. It can control databases such as data tables, view tables, stored procedures, user-defined functions, etc., by a specific user account Control of the object. It consists of two instructions, GRANT and REVOKE.
DQL: Data QueryLanguage data query language.

DQL: Database query language. Keywords: SELECT… FROM… WHERE.
DDL: Database schema definition language (database, data table). Keywords: CREATE, DROP, ALTER.
DML: Data Manipulation Language (data). Keywords: INSERT, UPDATE, DELETE.
DCL: Data Control Language (authority). Keywords: GRANT, REVOKE.
TCL: Transaction Control Language. Keywords: COMMIT, ROLLBACK, SAVEPOINT.
DDL, DML, DCL, DQL, TCL together constitute the complete language of the database.


Log in to the database server:
mysql -u account -p password // There is no space between -u account, the same password

Database operation:

Create a database:
create database database name;

When creating a database, specify the character set:
create database database name character set character set;
Example: create database database name character set utf8;

create database database name character set character set collate collation rules;
Example: create database database name character set character set collate utf8_bin;

View the database:
show databases;
(information_schem, performance_schema, mysql three libraries do not move!)

View the definition of the database:
show create database library name;

Modify the database:
alter database database name character set character set;

Delete database:
drop database database name;

Switch database:
use database name;

View the database currently in use:
select database();


Table operation:

Column type: int, char, varchar, float, double, boolean, date …
Column constraints:
primary key constraint: primary key
unique constraint: unique
non-null constraint: not null

Create table:
create table table name (column name 1 column type constraint, column name 2 column type constraint);
Example:
creat table table1(
sid int primary key,
sname varchar(16),
sex int,
age int,
class int ,
score float
); //Pay attention to punctuation

View table:
show tables;

View the definition of the
table : show create table table name;

View table structure:
desc table name;
example: desc student;

Modify the table:
add columns (add):
alter table table name add column name column type column constraints;
example: alter table student add score int not null;

Modify column (modify):
alter table table name modify column name column type;
example: alter table student modify sex varchar(2);

Modify column name (change):
alter table table name change old column name new column name column constraints;
example: alter table student sex gender varcher(3);

Delete column (drop):
alter table table name drop score;

Modify table name (rename):
rename tabel old table name to new table name;
Example: rename table student to stu;

Modify the table character set:
alter table table name character set character set;
alter table stu character set gbk;

Delete table:
drop table table name;
example: drop table stu;


Operation of data in the table:

Insert data
into the table : insert into table name (column name 1, column name 2, column name 3) values ​​(value 1, value 2, value 3);
Example: insert into student(sid,name,sex,age) values( 1,'james', 1,23);
simple writing:
insert into student values(2,'lucy',2 ,25)
select insert:
insert into student(sid,sname) values(3,'Lebron',1, 25);
Batch insert:
insert into student values(4,'zhangsan',1,25),(5,'lisi',2,24),(6,'wangwu',2,30);

Delete data:
delete from table name [where condition]
Example: delete from student where sid=1;
delete from student; //delete all

Modify (update) table data:
update table name set column name = column value, column name 2 = column value 2 [where condition]
Example: update student set sname='Zhang San' where sid=2;
update student set sname ='Li Si', sex=3; //all changes


Query the data in the table:
select [distinct] [*] [column name 1, column name 2] from table name [where condition] //distinct: remove duplicate data
select * from table name;
example: select * from student; / /View all contents in the table
select sname, sex from student; //Select view

Alias ​​query, as keyword, as keyword can be omitted
Table alias: select s.sname, s.sex from student as s; //Mainly used in multi-table query
column alias: select original column name 1 as new alias, original column Name 2 as the new alias from product;
Example: select sname as name, sex as gender from student; //Just use "name" and "sex" and replace sname and sex in the table. (as can be omitted)

Remove duplicate values ​​query:
select age from student; //Query all ages of students
select distinct age from student; // Remove duplicates

Operation query:
select , age+1 as age plus one from student; //do ± / operation in the query result and display it as a new column

select * from student where age>18; //Query people who are older than 18 years
select * from student where age <> 22; //Query people who are not equal to 22 (!= also possible)
select * from student where age> 18 and age <25; //Query people with age between 18-25
select * from student where age between 18 and 25; //Same as above
select * from student where age <18 or age >25; //Query age less than 18 or greater 25 people

like: Fuzzy query
_: represents one character
%: represents multiple characters
select * from student where sname like'%张%'; //Query all people whose names have "Zhang"
select * from product where sname like'_兆%'; //Query all the people whose name is "mega" in the second word

in: Query in a range
select * from student where class in (1,2,4); //Query students in classes 1, 2, 4

Sort query: (order by keyword)
asc: ascend ascending order (default)
desc: descend descending order
Example: select * from student order by sid desc;

Aggregate function: (the aggregate function cannot be connected after the where condition)
sum(): sum
avg(): average
count(): statistical number
max(): maximum
min(): minimum
select sum(score) from student ; //Get the total
score ofstudentsselect avg(score) from student; //Get the average score of students
select count(*) from student; //Get the number of all students

select * from student where score> (select avg(score) from student); //Query people whose scores are greater than the average score

Grouping: group by
having keyword: can be connected to the aggregate function, appear after the group
where keyword: cannot be connected to the aggregate function, appear before the grouping
According to the class, the number of students after grouping
select class, count(*) from student group by class;

According to the class grouping, statistics of the average score of each group of students, and the average score>60
select class,avg(score) from student group by class having avg(score)>60;

编写顺序:
select…from…where…group by…having…order by

执行顺序:
from…where…group by…having…select…order by

Guess you like

Origin blog.csdn.net/weixin_44264223/article/details/109732449