MySQL-study notes-basics

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:
    data model

SQL language

SQL general syntax, SQL classification, DDL, DML, DQL, DCL

SQL general syntax

  1. SQL statements can be written in one or more lines and end with a semicolon.
  2. SQL statements can use spaces/indentation to enhance the readability of the statement.
  3. The SQL statement of the MySQL database is not case-sensitive, and it is recommended to use capital letters for keywords
  4. 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:

  1. Inquire
    • query all databases
      SHOW DATABASES;
      
    • query all databases
      SELECT DATABASE();
      
  2. create
      CREATE DATABASES [IF NOT EXISTS] 数据库名 [DEFAULT CHARSET 字符集] [COLLATE 排序规则];
    
  3. delete
      DROP DATABASE [IF EXISTS]数据库名;
    
  4. 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.


value type
Numerical type:
string 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
datetime type
DDL-Table Operation-Modify

  1. add field
ALTER TABLE 表名 ADD 字符串 类型(长度)[COMMENT 注释] [约束];
  1. modify data type
ALTER TABLE 表名 MODIFY 字段名 新数据类型(长度);
  1. Modify field name and field type
ALTER TABLE 表名 CHANGE 旧字段名 新字段名 类型(长度)[COMMENT 注释] [约束];
  1. delete field
ALTER TABLE 表名 DROP 字段名;
  1. modify table name
ALTER TABLE 表名 RENAME TO 新表名;

DDL-table operation-delete

  1. delete table
DROP TABLE [IF EXISTS]表名;
  1. 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

  1. Add data to the specified field
INSERT INTO 表名(字段名1,字段名2,...) VALUES(1,2,...);
  1. Add data to all fields
INSERT INTO 表名 VALUES(1,2,...);
  1. 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

  1. Query multiple fields
SELECT 字段1,字段2,字段3,... FROM 表名;
SELECT * FROM 表名;   尽量不要写 * 最好全部列出
  1. set alias
SELECT 字段1[AS 字段1],字段2[AS 字段2],... FROM 表名;
  1. Remove duplicate records
SELECT DISTINCT 字段列表 FROM 表名;

DQL-Conditional Query
4. Syntax

SELECT 字段列表 FROM 表名 WHERE 条件列表;
  1. Condition
    Commonly used comparison operators are as follows:
    comparison operator
    Commonly used logical operators are as follows:

Logical Operators
DQL - aggregate functions

  1. Concept
    Take a column of data as a whole and perform longitudinal calculations
  2. Common aggregate functions
    Common aggregate functions
  3. grammar
SELECT 聚合函数(字段列表) FROM 表名;

Note: null values ​​do not participate in all aggregate function operations

DQL-group query

  1. grammar
SELECT 字段列表 FROM 表名 WHERE 条件列表 GROUP BY 分组字段名 [HAVING 分组后过滤条件];
  1. 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

  1. grammar
SELECT 字段列表 FROM 表名 ORDER BY 字段1 排序方式1,字段2 排序方式2;
  1. 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

  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 query is the first page of data, the starting index can be omitted, directly abbreviated as

DQL - order of execution
Execution order vs. write order

DCL

Used to manage database users and control database access rights.

DCL - Manage Users

  1. Query users
USE mysql;
SELECT * FROM user;
  1. create user
CREAT USER '用户名'@'主机名' IDENTIFIED BY '密码';
  1. Modify user password
ALTER USER '用户名'@'主机名' IDENTIFIED mysql_native_password WITH '新密码';
  1. 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:
insert image description here

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 .

  1. Query permission
SHOW GRANTS FOR '用户名'@'主机名';
  1. Granted permission
GRANTS 权限列表 ON 数据库名.表名 TO '用户名'@'主机名';
  1. revoke permission
REVOKE 权限列表 ON 数据库名.表名 FROM '用户名'@'主机名';

function

constraint

multi-table query

thing

Guess you like

Origin blog.csdn.net/m0_56116736/article/details/127467422