MySQL basics-basic sql statements

Table of contents

1.SQL classification

2.SQL-DDL

2.1 Database operations

Inquire

create

delete

use a database

2.2 Data table operations

Create table

lookup table

Modify table

3.SQL-DML (addition, deletion and modification)

3.1 Insert

3.2 Modification

3.3 Delete

4.SQL-DQL (check)

4.1 Basic query

4.2 Conditional query

4.3 Aggregation function query

4.4 Group query

4.5 Sorting query

4.6 Paging query (MySql)

4.7.DQL execution sequence

5.SQL-DCL

5.1 User management

Create user

change Password

delete users

5.2 User rights

Query permissions

Granted permission

revoke permission


Record study notes on SQL database

1.SQL classification

The basic categories are as follows:

Classification full name illustrate
DDL Data Definition Language Data definition language, used to define database objects (database, tables, fields)
DML Data Manipulation Language Data manipulation language, used to add, delete, and modify data in the database
DQL Data Query Language Data query language, used to query records in tables in databases
DCL Data Control Language Data control language, used to create database users and control database access permissions

2.SQL-DDL

2.1 Database operations

Inquire

Query all databases

show databases;

create

Create database

create database cat;

This means the creation is successful

drop database if exists cat;

You can also create a database like this

Detect whether the database exists, create it if it exists, otherwise do not create it, and no error will be reported

create database if not exists cat;

delete

Delete database

drop database cat;

In order not to report an error, you can do this when deleting a database that does not exist.

drop database if exists cat;

use a database

use cat;

Query the database in use

select database();

2.2 Data table operations

Create table

create table tb_user(
    id int comment '标号',
    name varchar(50),
    age int,
    gender varchar(1)
) comment '用户表';

comment comment is optional or not

lookup table

Query all tables in the database

show tables;

Query specific table structure

desc tb_user;

Query the specific table creation statement in order to see the comments clearly

show create table tb_user;

Modify table

 Add field

alter table tb_user add password varchar(50) comment '密码';

Modify field names and field types

alter table tb_user 旧字段名 新字段名 varchar(255);

Delete field

alter table tb_user drop username;

Modify table name

 alter table 旧表名 rename to 新表名;

Delete table

drop table if exists 表名;

Delete the table and recreate the table (all fields are deleted and need to be recreated)

truncate table 表名;

3.SQL-DML (addition, deletion and modification)

3.1 Insert

Insert data (insert based on fields)

insert into tb_user (id, name, age, gender) values (1,'张三',10,'男');

To insert all, all fields must be included and in order

insert into tb_user values (2,'李四',10,'男');

3.2 Modification

The word "where" represents the condition (if there is no condition, it will affect all data)

update tb_user set name = '王五' where id = 1;

3.3 Delete

delete from tb_user where id = 1;

4.SQL-DQL (check)

4.1 Basic query

Query all fields

select * from 表名;

Query specified fields

select name,id,gender from 表名;

Query the specified field and create an alias

select workaddress as '工作地址' from 表名;

Query the specified field and perform deduplication processing

select distinct workaddress as '工作地址' from 表名;

4.2 Conditional query

Conditions for carrying

comparison operator symbol Function
>         more than the
>= greater or equal to
< less than
<= less than or equal to
= equal
<> or != not equal to
BETWEEN ... AND ... Within a certain range (including minimum and maximum values)
IN(...) The value in the list after in, select one from more than one
LINK placeholder Fuzzy matching ('_' matches a single character, '%' matches any number of characters)
IS NULL A field that is NULL
Logical Operators Function
AND 或 && And (multiple conditions are true at the same time)
OR 或 || Or (one of multiple arbitrary conditions is true)
NOT or! No

All data with age less than 20

select * from emp where age < 20;

Query data that does not carry a certain field

select * from emp where idcard is null;

Use with not

select * from emp where idcard is not null;

Use of between and (query data between age 15--30)

select * from emp where age between 15 and 30;

The use of in (the following two commands have the same effect)

select * from emp where age=18 or age=20 or age=90;

select * from emp where age in(18,20,90);

Fuzzy matching (data whose name is queried is 3 characters)

select * from emp where name like '___';

4.3 Aggregation function query

Common aggregate functions

Function name Function
count total number
max maximum value   
min minimum value
avg average value
sum Sum

grammar:

select 聚合函数(字段列表) from 表名;

Query the sum of all data with age<30 

select count(*) from emp where age < 30;

Query average age

select avg(age) from emp;

4.4 Group query

grammar

select 字段列表 from 表名 where 条件 group by 分组字段名 having 分组后过滤条件;

The difference between where and having:

The execution timing is different : where is filtering before grouping, if the where condition is not met, the grouping will not be participated; while having is filtering the results after grouping.

The judgment conditions are different : where cannot judge the aggregate function, but having can.

According to gender grouping, count the number of male employees and female employees 

select gender,count(*) from emp group by  gender ;

 

According to gender grouping, calculate the average age of male employees and female employees 

select gender, avg(age) from emp group by gender;

Query employees whose age is less than 45, group them according to their work addresses, and obtain the work addresses where the number of employees is greater than or equal to 3

address_count is the alias obtained by counting the number

select workaddress,count(*) address_count from emp where age < 45 group by workaddress having address_count >= 3;

4.5 Sorting query

select 字段列表 from 表名 order by 字段1 排序方式1, 字段2 排序方式2;

Ascending sort (asc) default

 Sort by age in ascending order

select * from emp order by age;

Sort descending

select * from emp order by age desc;

4.6 Paging query (MySql)

Different databases may have different fields

select 字段列表 from 表名 limit 起始索引,查询记录数;

Query the first ten data

select * from emp limit 0,10;

4.7.DQL execution sequence

①:from

②:where

③:group by,having

④:select

⑤:order by

⑥:limit

5.SQL-DCL

The user table of mysql database is in the user table under mysql database.

5.1 User management

Create user

The newly created user does not have any permissions

Can only be accessed on localhost

create user 'itcast'@'localhost' identified by '123456';

Set up any host access

create user 'itcast'@'%' identified by '123456';

change Password

alter user 'itcast'@'localhost' identified with mysql_native_password by '123';

delete users

drop user 'itcast'@'localhost';

5.2 User rights

Query permissions

show grants for 'root'@'localhost';

Granted permission

Grant a user all permissions on a specific database:

This will grant the user all permissions in the specified database, including SELECT, INSERT, UPDATE, DELETE, etc.

GRANT ALL PRIVILEGES ON database_name.* TO 'username'@'localhost';

Grant a user specific permissions on a specific table:

This will only grant the user SELECT and INSERT permissions in the specified table.

GRANT SELECT, INSERT ON database_name.table_name TO 'username'@'localhost';

Grant the user certain permissions on all databases:

This will grant the user permission to perform operations such as CREATE, DROP, and ALTER on all databases.

GRANT CREATE, DROP, ALTER ON *.* TO 'username'@'localhost';

Grant the user all permissions on all databases:

GRANT ALL PRIVILEGES ON *.* TO 'username'@'localhost';

revoke permission

Revoke all permissions from a user on a specific database:

This will revoke all permissions from the user on the specified database.

REVOKE ALL PRIVILEGES ON database_name.* FROM 'username'@'localhost';

Revoke a user's specific permissions on a specific table:

This will revoke the user's SELECT and INSERT permissions on the specified table.

REVOKE SELECT, INSERT ON database_name.table_name FROM 'username'@'localhost';

Revoke all user permissions on all databases:

REVOKE ALL PRIVILEGES ON *.* FROM 'username'@'localhost';

Guess you like

Origin blog.csdn.net/m0_64642443/article/details/133238433