This article is the author's study notes for learning dark horse programmer MySQL database entry to proficiency
Course Content and Database Related Concepts
Concrete functions,
constraints
, multi-table queries,
things in SQL language
MySQL overview
Database (DataBase DB): A warehouse that stores data, and the data is stored in an organized manner.
Data Base Management System DBMS: a large-scale software for manipulating and managing databases
SQL (Structured Query Language): a programming language for operating relational databases, defining a set of unified standards for operating relational databases
Mainstream relational database management system: operate with SQL
data model
Relational database (RDBMS):
- Concept: based on the relational model, a database composed of multiple interconnected two-dimensional tables
- Features: use table to store data, uniform format, easy maintenance; use SQL language operation, unified standard, easy to use
Data model:
SQL language
SQL general syntax, SQL classification, DDL, DML, DQL, DCL
SQL general syntax
- SQL statements can be written in one or more lines and end with a semicolon.
- SQL statements can use spaces/indentation to enhance the readability of the statement.
- The SQL statement of the MySQL database is not case-sensitive, and it is recommended to use capital letters for keywords
- Notes:
- Single-line comment: --comment content or **#** comment content (MySQL specific)
- Multi-line comment: /* comment content */
SQL classification
- DDL: Data Definition Language, database definition language, used to define database objects (databases, tables, fields).
- DML: Data Manipulation Language, database operation language, used to add, delete, and modify data in database tables.
- DQL: Data Query Language, database query language, used to query the records in the table in the database.
- DCL: Data Control Language, database control language, used to create database users and control database access rights
DDL
Operations on the database: query, create, delete, use
Operations on the data table: create, query
Database operations:
- Inquire
- query all databases
SHOW DATABASES;
- query all databases
SELECT DATABASE();
- query all databases
- create
CREATE DATABASES [IF NOT EXISTS] 数据库名 [DEFAULT CHARSET 字符集] [COLLATE 排序规则];
- delete
DROP DATABASE [IF EXISTS]数据库名;
- use
USE 数据库名;
DDL-table operation-query
- Query all tables in the current database
SHOW TABLES;
- query table structure
DESC 表名;
- Query the table creation statement of the specified table
SHOW CREATE TABLE 表名;
DDL-table operation-create
CREATE TABLE 表名(
字段1 字段类型[COMMENT 字段1注释],
字段2 字段类型[COMMENT 字段2注释],
字段3 字段类型[COMMENT 字段3注释],
...
字段n 字段类型[COMMENT 字段n注释]
)[COMMENT 表注释];
eg:
create table tb_user(
id int comment '编号',
name varchar(50) comment '姓名',
gender varchar(1) comment '性别'
)comment '用户表';
Note: […] is an optional parameter, there is no comma after the last field <\font>
DDL-table operation-data type
There are many data types in MySQL, which are mainly divided into three categories: numeric type, string type, and date time type.
Numerical type:
Both char and varchar can describe strings. Char is a fixed-length string. It takes up as many characters as the specified length, regardless of the length of the field value. And varchar is a variable-length string, and the specified length is the maximum occupied length. Relatively speaking, the performance of char will be higher.
Date Time Type
DDL-Table Operation-Modify
- add field
ALTER TABLE 表名 ADD 字符串 类型(长度)[COMMENT 注释] [约束];
- modify data type
ALTER TABLE 表名 MODIFY 字段名 新数据类型(长度);
- Modify field name and field type
ALTER TABLE 表名 CHANGE 旧字段名 新字段名 类型(长度)[COMMENT 注释] [约束];
- delete field
ALTER TABLE 表名 DROP 字段名;
- modify table name
ALTER TABLE 表名 RENAME TO 新表名;
DDL-table operation-delete
- delete table
DROP TABLE [IF EXISTS]表名;
- Drop the specified table and recreate it
TRUNCATE TABLE 表名;
Note: When deleting a table, all data in the table will also be deleted.
DML
Add, delete, and modify data in database tables.
DML - add data
- Add data to the specified field
INSERT INTO 表名(字段名1,字段名2,...) VALUES(值1,值2,...);
- Add data to all fields
INSERT INTO 表名 VALUES(值1,值2,...);
- Add data in batches
INSERT INTO 表名(字段名1,字段名2,...) VALUES(值1,值2,...),(值1,值2,...),(值1,值2,...);
INSERT INTO VALUES(值1,值2,...),(值1,值2,...),(值1,值2,...);
Notice:
- When inserting data, the order of the specified fields needs to correspond one-to-one with the order of the values.
- String and date data should be enclosed in quotes.
- The size of the inserted data should be within the specified range of the field.
DML - modify data
UPDATE 表名 SET 字段名1 = 值1,字段名2 = 值1,...[WHERE 条件];
Note: The condition of the modification statement is optional. If there is no condition, all data in the entire table will be modified.
DML - delete data
DELETE FROM 表名 [WHERE 条件];
Notice:
- The condition of the DELETE statement is optional. If there is no condition, all data in the entire table will be deleted
- The DELETE statement cannot delete the value of a field (UPDATE can be used).
DQL
Used to query the records in the table in the database.
DQL-syntax
SELECT
字段列表
FROM
表名列表
WHERE
条件列表
GROUP BY
分组字段列表
HAVING
分组后条件列表
LIMIT
分页参数
DQL - basic query
- Query multiple fields
SELECT 字段1,字段2,字段3,... FROM 表名;
SELECT * FROM 表名; 尽量不要写 * 最好全部列出
- set alias
SELECT 字段1[AS 字段1],字段2[AS 字段2],... FROM 表名;
- Remove duplicate records
SELECT DISTINCT 字段列表 FROM 表名;
DQL-Conditional Query
4. Syntax
SELECT 字段列表 FROM 表名 WHERE 条件列表;
- Condition
Commonly used comparison operators are as follows:
Commonly used logical operators are as follows:
DQL - aggregate functions
- Concept
Take a column of data as a whole and perform longitudinal calculations - Common aggregate functions
- grammar
SELECT 聚合函数(字段列表) FROM 表名;
Note: null values do not participate in all aggregate function operations
DQL-group query
- 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.
- The judgment conditions are different: where cannot judge the aggregation function, but having can.
DQL - sort query
- grammar
SELECT 字段列表 FROM 表名 ORDER BY 字段1 排序方式1,字段2 排序方式2;
- sort by
- ASC: Ascending order (default).
- DESC: descending order.
Note: If it is multi-field sorting, only when the first field has the same value will the second field be sorted.
DQL-Paging query
- 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 query is the first page of data, the starting index can be omitted, directly abbreviated as
DQL - order of execution
DCL
Used to manage database users and control database access rights.
DCL - Manage Users
- Query users
USE mysql;
SELECT * FROM user;
- create user
CREAT USER '用户名'@'主机名' IDENTIFIED BY '密码';
- Modify user password
ALTER USER '用户名'@'主机名' IDENTIFIED mysql_native_password WITH '新密码';
- delete users
DROP USER '用户名'@'主机名';
Notice:
- Hostnames can use % wildcards.
- This type of SQL developers operate less, mainly used by DBA (Database Administrator database administrator).
DCL-privilege control
There are many kinds of permissions defined in MySQL, but the commonly used ones are as follows:
The above is just a brief list of several common permission descriptions. For other permission descriptions and their meanings, you can directly refer to the official documents .
- Query permission
SHOW GRANTS FOR '用户名'@'主机名';
- Granted permission
GRANTS 权限列表 ON 数据库名.表名 TO '用户名'@'主机名';
- revoke permission
REVOKE 权限列表 ON 数据库名.表名 FROM '用户名'@'主机名';