Database operation -- sql statement

database operation

Allow remote login

grant all privileges on . to root@‘%’ identified by ‘密码’ WITH GRANT OPTION

DDL – database operations

DDL-Query

query all databases

SHOW DATABASES;

Query the current database

SELECT DATABASE();

create database

CREATE DATABASE [IF NOT EXISTS] 数据库名 [DEFAULT CHARSET 字符集] [COLLATE 排序规则];

delete database

DROP DATABASE [IF EXISTS] 数据库名;

use database

USE 数据库名;

Query all tables in the current database

SHOW TABLES;

query table structure

DESC 表名;

Query the specified table creation statement

SHOW CREATE TABLE 表名
DDL-table operation-create

Create database tables

DDL-table operation-modify

add field

ALTER TABLE 表名 ADD 字段名 类型(长度) [COMMENT 注释] [约束];

modify data type

ALTER TABLE 表名 MODITY 字段名 新数据类型(长度);

Modify field name and field type

ALTER TABLE 表名 CHANGE 旧字段名 新字段名 类型(长度) [COMMENT '注释'] [约束];

Modify field position

Modify field 1 to follow field 2

ALTER TABLE 表名 CHANGE 字段名1 字段名1 VARCHAR(50) AFTER 字段名2

delete field

ALTER TABLE 表名 DROP 字段名;

modify table name

ALTER TABLE 表名 RENAME TO 新表名;

delete table

DROP TABLE [IF EXISTS] 表名;

DML - Data Manipulation Language

DML - add data

1. Add data to the specified field

INSERT INTO 表名 (字段名1,字段名2,...) VALUES(值1,值2,...);

2. Add data to all fields

INSERT INTO 表名  VALUES(值1,值2,...);

3. Add data in batches

INSERT INTO 表名 (字段名1,字段名2,...) VALUES(值1,值2,...),值1,值2,...),值1,值2,...);
INSERT INTO 表名 VALUES(值1,值2,...),值1,值2,...),值1,值2,...);
DML - modify data
UPDATE 表名 SET 字段名1=值1,字段名2=值2 [WHERE 条件];

If the where condition is not written, the entire table is modified

DML - delete data
DELETE FROM 表名 [WHERE 条件]

Notice:

  • The condition of the DELETE statement may or may not be present. If there is no condition, all data in the entire table will be deleted.
  • The DELETE statement cannot delete the value of a certain field (UPDATE can be used).

DQL - Data Query Language

The full English name of DQL is Data Query Language (Data Query Language), a data query language used to query records in tables in the database.

DQL-syntax

insert image description here

DQL - basic query

1. Query multiple fields

select 字段1,字段2,字段3... from 表名;
select * from 表名;

2. Set an alias

select 字段1 [as 别名1],字段2 [as 别名2]... from 表名;

3. Remove duplicate records

select distinct 字段列表 from 表名;
DQL-Conditional query

1. Grammar

select 字段列表 from 表名 where 条件列表;

2. Conditions

insert image description here

DQL - aggregate functions

1 Introduction

Take a column of data as a whole and perform longitudinal calculations

2. Common aggregation functions
insert image description here
3. Grammar

select 聚合函数(字段列表) from 表名;
DQL-group query

1. Grammar

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

The difference between where and having

  • The execution timing is different: where is to filter before grouping, if the where condition is not met, do not participate in grouping; and having is to filter the results after grouping.
  • The judgment conditions are different: where cannot judge the aggregation function, but having can.

Notice

Execution order: where > aggregate function > having.

After grouping, the fields to be queried are generally aggregation functions and grouping fields, and it is meaningless to query other fields.
insert image description here

DQL - sort query

1. Grammar

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

2. Sort by

  • ASC : ascending order (default)
  • DESC: descending order

Note: If it is multi-field sorting, when the first field has the same value, it will be sorted according to the second field.

DQL-Paging query

1. Grammar

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

Notice

  • The starting index starts from 0, starting index = (query page number - 1) * number of records displayed on each page.
  • Pagination query is a database dialect, and different databases have different implementations. In MySQL, it is LIMIT.
  • If the first page of data is being queried, the starting index can be omitted, which can be abbreviated as limit 10.
DQL - order of execution

insert image description here

DCL - Data Control Language

DCL-Introduction

The full English name of DCL is Data Control Language (Data Control Language), which is used to manage database users and control database access rights.

DCL-admin user

1. Query users

USE mysql;
SELECT * FROM user;

2. Create a user

ALTER USER '用户名'@'主机名' IDENTIFIED BY '密码';

3. Modify user password

ALTER USER '用户名'@'主机名' IDENTIFIED WITH mysql_native_password BY '新密码';

4. Delete user

DROP USER '用户名'@'主机名';

Notice:

  • Hostnames can use % wildcards.
  • This type of SQL developers operate less, mainly used by DBA (Database Administrator database administrator).
DCL-Access Control

1. Query permission

show grants for '用户名'@'主机名';

2. Grant permissions

gant 权限列表 on 数据库名.表名 to '用户名'@'主机名';

3. Revoke permissions

REVOKE 权限列表 ON 数据库名.表名 FROM '用户名'@'主机名';

Notice:

  • Between multiple permissions, use commas to separate
    P USER 'username'@'hostname';

注意:

- 主机名可以使用%通配。
- 这类SQL开发人员操作的比较少,主要是DBA ( Database Administrator数据库管理员)使用。

##### DCL-权限控制

1.查询权限

show grants for 'username'@'hostname';


2.授予权限

gant permission list on database name. table name to 'username'@'hostname';


3.撤销权限

REVOKE permission list ON database name. table name FROM 'username'@'hostname';


注意:

- 多个权限之间,使用逗号分隔
- 授权时,数据库名和表名可以使用*进行通配,代表所有

Guess you like

Origin blog.csdn.net/weixin_62107875/article/details/127472034