Mysql detailed study guide (super detailed)

Article directory

DatabaseMySQL

1. The concept of database

1.1 Current data storage method

Transient state: refers to data stored in memory

Persistent state: using file storage (IO stream, object serialization)

1.2 Disadvantages of existing methods

  • type of data
  • Data magnitude
  • Security Mechanism
  • Backup and recovery mechanism
  • Query search

1.3 New file types

Can I use a table like excel to store it?

Yes, but Excel tables can clearly describe the structure of the data, but it is difficult to perform complex queries, and it is extremely inconvenient to operate Excel with code.

1.4 Database

A database is a warehouse that is organized, stored, and managed according to a certain data structure.

1.4 Classification

grid:

Hierarchy:

Relational structure: Stored in Table, relationships are established between multiple tables, and access is achieved through operations such as classification, merging, connection, and selection. It is the current mainstream database structure.

  • Oracle: Oracle product, an enterprise-level large-scale database product with strong professionalism. Service charges apply.
  • DB2: IBM product, an enterprise-level large-scale database product with strong professionalism. Service charges apply.
  • MySQL: A free database product, it is still the first database for small and medium-sized enterprises and individuals, and it is also the first choice for personal learning.
  • SQL Server: A product of Microsoft. In the early days, it could only be applied to Windows systems.

Non-relational structure: The full name is Not only SQL, and the abbreviation is no SQL. It is only used when processing high concurrency, high performance, and massive data. It is generally stored in key-value mode. Generally used as a supplement to relational databases. For example: MongoDB, Redis, ES, etc.

1.5 Database management system

Software for operating and managing databases, used to create, manage and maintain databases.

Used for unified management and control of the database. Users access data in the database through the database management system.

2. MySQL

2.1 Introduction

It is a relational database.

RDBMS: relation database management system relational database management system

2.2 Table of Contents

bin: executable file

data: system database

include: header file

lib: supported libraries

2.3 Core files

my.ini: Settings for the core configuration of the database.

port=3306 # 端口号
default-character-set=utf8 # 默认字符集
default-storage-engine=INNODB # 默认存储引擎
max_connections=100 # 最大连接数

3. SQL language

SQL: structured query language, structured query language.

Used to operate the database.

Operations on data generally include additions, deletions, modifications, and searches. Abbreviated as CRUD (create, read, update, delete)

4. MySQL operation

4.1 Connect to database

If environment variables have been configured:

Enter mysql -u root -p and press Enter

Enter the root user password and press Enter to complete the login.

If the environment variable is not configured, in Windows systems, you can find the mysql client command line tool in the start menu. Open it and enter the password.

4.2 Basic commands

4.2.1 View all databases

show databases; View which databases are in MySQL

4.2.2 Create database

create database database name; Create a database with the default character set.

create database database name character set GBK; Create a database with the character set GBK.

create database if not exists database name; if a database does not exist, create one.

Note: Be sure to specify the character set when creating the database.

create database java2106 character set utf8;

4.2.3 View created database information

show create database database name;

4.2.4 Modify database

alter database database name character set character set; modify character set

alter database java2106 character set gbk;

4.2.5 View the currently used database

select database(); Note the brackets at the end, which means that a function is called here.

4.2.6 Switch to use database

use database name;

4.2.7 Delete database

drop database database name;

Notice:

To create, modify, delete, view, etc. the database structure, the commands used are: create, alter, drop, show

5. Data query

A database is made up of tables, which contain rows and columns. Data query is a query on the table. Query results are displayed in the form of rows and columns and can be viewed as a virtual table.

5.1 Data import

show tables; View all table information.

source sql file address; import a sql file.

create database companydb character set utf8; -- 创建数据库
use companydb; -- 切换使用数据库
source d:\companydb.sql; -- 导入一个sql文件内容到数据库。
show tables; -- 查询所有的表名

5.2 Basic query

select column name from table name; to query data in a table, only query the corresponding columns.

select * from t_employees; -- 查询所有的列,所有的行。
select first_name, email from t_employees; -- 查询first_name, email两列。

Perform operations on the columns in the query: you can use +, -, *, /, operations, but you cannot use %. % is not a remainder operation in SQL.

Note: +Cannot perform string splicing operations.

SELECT EMPLOYEE_ID, FIRST_NAME, SALARY * 12 FROM t_employees;

Alias ​​operation allows you to alias a column. The syntax structure is

select EMPLOYEE_ID AS 编号 from t_employees;

Note: The AS keyword can be omitted. There is no difference whether the alias is enclosed in single quotes or not.

SELECT EMPLOYEE_ID id, FIRST_NAME '名', SALARY * 12 as 年薪 FROM t_employees;

distinct removes duplicates from query results.

Note: If there are multiple columns, the contents of the multiple columns need to be the same to remove duplicates.

SELECT DISTINCT MANAGER_ID FROM t_employees;

5.3 Sorting query

Sort query results according to specified rules.

select column name from table name order by sorting column sorting rule;

There are two sorting rules:

  • ASC default value, ascending order (from small to large)
  • DESC descending order (from large to small)
-- 按照单列排序
SELECT EMPLOYEE_ID, FIRST_NAME, SALARY FROM t_employees ORDER BY SALARY DESC; 

Sort by multiple columns:

Sort according to the rules of the first sorting column first, and if there are the same columns, then sort according to the rules of the second sorting column, and so on.

SELECT EMPLOYEE_ID, FIRST_NAME, SALARY FROM t_employees ORDER BY SALARY DESC, EMPLOYEE_ID DESC; 

Notice:

The above case means that the employees are sorted according to their salary first. If the salaries are the same, the employees with higher salaries will be sorted first.

5.4 Conditional query

grammar:

select * from table name where condition;

5.4.1 Equivalence judgment

SELECT * FROM t_employees WHERE SALARY = 11000;
SELECT * FROM t_employees WHERE FIRST_NAME = 'Steven';

Note: If the value compared in the condition is a number, you can add or leave single quotes. However, if it is not a number, but a string or time, you must add single quotes.

Only an equal sign is needed for equality comparison.

5.4.2 Logical judgment

When there are multiple conditions, you can use AND and OR for logical processing, and NOT for negation.

-- 条件并列
SELECT * FROM t_employees WHERE SALARY = 11000 AND COMMISSION_PCT = 0.3;
-- 条件或者
SELECT * FROM t_employees WHERE FIRST_NAME = 'Steven' OR FIRST_NAME = 'Den';
-- 条件取反
SELECT * FROM t_employees WHERE NOT SALARY = 11000 ORDER BY SALARY DESC;

5.4.3 Unequal value judgment

You can use >, <, >=, <=, !=, <> to perform unequal value judgment.

Note: != has the same effect as <>.

Note: Strings can also be judged using unequal values. The first character is judged first, then the second. For example 'ac' is greater than 'ab'

Note: Dates can also be judged using unequal values. The later date is greater than the previous date. If it is represented by a string, it can also be judged according to the string. For example: date > '2021' means getting all data after 2021.

SELECT * FROM t_employees WHERE SALARY <> 11000 ORDER BY SALARY DESC;

5.4.4 Interval judgment

Use between...and to implement interval judgment.

Equivalent to column name >= smaller value AND column name <= larger value

SELECT * FROM t_employees WHERE SALARY BETWEEN 11000 AND 12000;
-- 相当于
SELECT * FROM t_employees WHERE SALARY >= 11000 AND SALARY <= 12000;

Note: The BETWEEN... AND interval must be written with a small value first and then a large value, otherwise the query will have no results.

5.4.5 Null value judgment

Column name is NULL

Column name is not NULL

SELECT * FROM t_employees WHERE COMMISSION_PCT IS NULL; -- 不能用 = NULL
SELECT * FROM t_employees WHERE COMMISSION_PCT IS NOT NULL;

5.4.6 Enumeration query

To list the values ​​to be matched, use the in keyword

Syntax: column name IN (value 1, value 2, value 3...)

SELECT * FROM t_employees WHERE SALARY IN (8000,10000,6000);
-- 等同于
SELECT * FROM t_employees WHERE SALARY = 8000 OR SALARY = 10000 OR SALARY = 6000;

5.4.7 Fuzzy query

Fuzzy queries can be used when the conditions are unclear.

Syntax: column name LIKE 'A%'

Use %and _as wildcard characters. %Represents 0 to multiple characters, _representing one character.

Note: Column name LIKE '%%' queries all.

-- 表示名以Al开头,后面有3个字符的
SELECT * FROM t_employees WHERE FIRST_NAME LIKE 'Al___' ORDER BY FIRST_NAME;
-- 表示名以Al开头
SELECT * FROM t_employees WHERE FIRST_NAME LIKE 'Al%' ORDER BY FIRST_NAME;
-- 表示名包含Al
SELECT * FROM t_employees WHERE FIRST_NAME LIKE '%Al%' ORDER BY FIRST_NAME;

Note: If there is no wildcard character in the condition after LIKE, it is equivalent to =

5.4.8 Branch query

Some sql meanings:

SELECT 1 FROM t_employees; -- 表里有多少条记录,就会显示多少行数字1
SELECT `EMPLOYEE_ID` FROM t_employees; -- 表示查询EMPLOYEE_ID列的数据,`号一般用来区分关键字
SELECT name, `DESC` from users; -- 区分关键字
SELECT 'EMPLOYEE_ID' FROM t_employees; -- 表里有多少条记录,就会显示多少行字符串EMPLOYEE_ID

Branch structure query:

grammar:

CASE

WHEN condition 1 THEN result 1

WHEN condition 2 THEN result 2

WHEN condition 2 THEN result 2

ELSE result 4

END

-- 根据薪资查询级别
SELECT EMPLOYEE_ID, FIRST_NAME, SALARY, 
CASE
   WHEN SALARY >= 20000 THEN '高级'
   WHEN SALARY >= 10000 THEN '中级'
   WHEN SALARY >= 5000 THEN '初级'
   ELSE '见习'
END 级别
 FROM t_employees ORDER BY SALARY DESC;

5.4.9 Time function

The time function in MySQL can be used alone or in other SQL statements.

SELECT SYSDATE(); -- 得到当前的日期时间
SELECT CURDATE(); -- 得到当前的日期
SELECT CURTIME(); -- 得到当前的时间
SELECT NOW(); -- 得到当前的日期时间
SELECT WEEK('2021-08-02'); -- 得到指定日期是当年的第几周
SELECT WEEK(CURDATE()); -- 得到当前日期是当年的第几周(函数可以嵌套)
-- 得到日期时间中的年月日时分秒
SELECT YEAR(CURDATE());
SELECT MONTH(CURDATE());
SELECT DAY(CURDATE());
SELECT HOUR(NOW());
SELECT DATEDIFF('2021-06-03','2021-08-02'); -- 得到两个日期中间间隔的天数,结果是-60
SELECT ADDDATE(CURDATE(),80); -- 得到今天的80天后是哪一天
SELECT * FROM t_employees WHERE HIRE_DATE = ADDDATE(CURDATE(),-8200); -- 判断HIRE_DATE日期为今天的8200天前

5.4.10 String functions

String functions in MySQL can be used alone or in other SQL statements.

SELECT CONCAT(FIRST_NAME,',',LAST_NAME) 全名 FROM t_employees; -- 拼接字符串
-- 第一个参数是要改变的字符串,第二个参数是从哪个位置开始(下标从1开始),第三个参数是长度,最后一个参数是替换的字符串
SELECT INSERT(FIRST_NAME,3,3,'Hello')  FROM t_employees WHERE EMPLOYEE_ID = 100; -- 将字符串Steven变成StHellon
SELECT LOWER(FIRST_NAME) FROM t_employees WHERE EMPLOYEE_ID = 100; -- 转小写
SELECT UPPER(FIRST_NAME) FROM t_employees WHERE EMPLOYEE_ID = 100; -- 转大写
SELECT SUBSTRING(FIRST_NAME,2) FROM t_employees WHERE EMPLOYEE_ID = 100; -- 截取字符串

5.5 Aggregation functions

Similar to formulas in Excel.

sum(): sum

avg(): Find the average

max(): Find the maximum value

min(): Find the minimum value

count(): Count the number of rows

SELECT SUM(SALARY) FROM t_employees; -- 统计当月员工总工资和
SELECT avg(SALARY) FROM t_employees; -- 计算平均薪资
SELECT max(SALARY) FROM t_employees; -- 最高工资
SELECT min(SALARY) FROM t_employees; -- 最低工资
SELECT count(1) FROM t_employees; -- 查询行数,可以使用*或列名,使用数字1性能最好
SELECT count(1) FROM t_employees WHERE SALARY > 10000; -- 查询薪资大于10000的人数

5.6 Group query

5.6.1 Group basic query

Grouping query is to divide the data into multiple groups according to the specified columns, and then perform data statistics by group. So generally, grouping means that statistics need to be aggregated.

grammar:

Group by group column

Note: If there is grouping in the statement, the column queried previously must be an aggregate function or a column that has a one-to-one correspondence with the grouping column. Otherwise, only the first row of records will be displayed, which is a meaningless record.

SELECT DEPARTMENT_ID,COUNT(1),AVG(SALARY),MAX(SALARY), MIN(SALARY), SUM(SALARY) FROM t_employees GROUP BY DEPARTMENT_ID;

5.6.2 Group filter query

After grouping, if you need to add filter conditions, use the having keyword.

-- 先分组,再写条件使用having关键字
SELECT DEPARTMENT_ID, COUNT(1), MAX(SALARY) FROM t_employees GROUP BY DEPARTMENT_ID HAVING DEPARTMENT_ID IN (30, 50, 80);

However, the above statement can obviously write the conditions first and then group them.

-- 先写条件,再分组
SELECT DEPARTMENT_ID, COUNT(1), MAX(SALARY) FROM t_employees WHERE DEPARTMENT_ID IN (30, 50, 80) GROUP BY DEPARTMENT_ID ;

In the project, the scenario where the having keyword must be used should be to use aggregate functions as conditions.

Check the departments whose average salary is higher than 8,000.

-- 此时,将条件放到group by的前面,并使用where是不行的
SELECT DEPARTMENT_ID, AVG(SALARY) FROM t_employees GROUP BY DEPARTMENT_ID HAVING AVG(SALARY) > 8000;

Note: The aggregate function after having can only be compared with constants and cannot write column names. For example, the following code is wrong:

-- 查询部门大于平均薪资的人数,不能这么写
SELECT DEPARTMENT_ID, COUNT(1) FROM t_employees GROUP BY DEPARTMENT_ID HAVING SALARY > AVG(SALARY);

5.7 Restricted query

Display a subset of results that meet the conditions. It is usually used for paging display in projects.

Syntax: limit skip number of items, display number of items

-- 跳过5条,显示5条,即显示第6-10条
SELECT * FROM t_employees LIMIT 5, 5;
-- 如果是跳过0条,显示5条
SELECT * FROM t_employees LIMIT 0, 5;
-- 可以简写为:
SELECT * FROM t_employees LIMIT 5;

In the project, if there is paging business, the calculation rules are as follows:

// page是页数
// size是每页显示的条数
public void findAll(int page, int size){
    
    
    int skip = (page - 1) * size;
    String sql = "select * from t1 limit " + skip + ", " + size;
}

5.8 Summary of basic query sequence

SELECT column name

FROM table name

WHERE condition

GROUP BY grouping column

HAVING post-grouping condition

ORDER BY sort column sorting rules

LIMIT skip the number of items, display the number of items

5.9 Subquery

Contain query statements within query statements.

5.9.1 Use query results as conditions

For example: Query employee information whose salary is greater than Bruce.

-- 查询Bruce的工资信息,得到单行单列的数据(6000)
SELECT SALARY FROM t_employees WHERE FIRST_NAME = 'Bruce';
-- 查询工资大于6000的员工信息
SELECT * FROM t_employees WHERE SALARY > 6000;
-- 将两条语句组合,需要先查询的部分应该加上括号
SELECT * FROM t_employees WHERE SALARY > (SELECT SALARY FROM t_employees WHERE FIRST_NAME = 'Bruce');

Note: The results need to be used as query conditions. The result here must be a single row and single column.

5.9.2 Query results are multiple rows and single column (enumeration)

Query the information of employees in the same department as 'King'.

-- 查询King对应的部门
SELECT DEPARTMENT_ID FROM t_employees WHERE LAST_NAME = 'King';
-- 结果有两个,不能直接用等号,需要使用in
SELECT * FROM t_employees WHERE DEPARTMENT_ID in (80, 90) ;
-- 将两条语句组合,需要先查询的部分应该加上括号
SELECT * FROM t_employees WHERE DEPARTMENT_ID in (SELECT DEPARTMENT_ID FROM t_employees WHERE LAST_NAME = 'King') ;

5.9.3 ALL and ANY usage

Query employee information about the salary of all employees in more than 60 departments.

-- 查询60部门的员工工资,结果是多行单列
SELECT SALARY FROM t_employees WHERE DEPARTMENT_ID = 60;
-- 如果要大于60部门所有员工工资,其实只需要大于最高工资即可
SELECT * FROM t_employees WHERE SALARY >  (SELECT max(SALARY) FROM t_employees WHERE DEPARTMENT_ID = 60);
-- 上面的写法可以实现结果,但是是人为分析的(算法),按照原始要求应该写为
SELECT * FROM t_employees WHERE SALARY > ALL (SELECT SALARY FROM t_employees WHERE DEPARTMENT_ID = 60);

Query employee information about the salary of any employee in more than 60 departments.

-- 查询60部门的员工工资,结果是多行单列
SELECT SALARY FROM t_employees WHERE DEPARTMENT_ID = 60;
-- 如果要大于60部门任意员工工资,其实只需要大于最低工资即可
SELECT * FROM t_employees WHERE SALARY >  (SELECT min(SALARY) FROM t_employees WHERE DEPARTMENT_ID = 60);
-- 上面的写法可以实现结果,但是是人为分析的(算法),按照原始要求应该写为
SELECT * FROM t_employees WHERE SALARY > ANY (SELECT SALARY FROM t_employees WHERE DEPARTMENT_ID = 60);

Notice:

The difference between ALL and ANY:

ALL needs to determine whether all the multiple rows in the result meet the conditions, and ANY only needs to determine whether one of the multiple rows in the result meets the condition.

5.9.4 Query again using subquery as condition

Query the information of all senior level employees. (Refer to 5.4.8)

First, display the employee level using branch query:

SELECT EMPLOYEE_ID, FIRST_NAME, SALARY, 
CASE
   WHEN SALARY >= 20000 THEN '高级'
   WHEN SALARY >= 10000 THEN '中级'
   WHEN SALARY >= 5000 THEN '初级'
   ELSE '见习'
END lv
 FROM t_employees

Try adding the condition WHERE lv = 'Advanced' directly at the end, and the result is an error. The reason is that the alias is added after the query result is obtained, and the condition is used during the query process, but the result has not yet come out at this time, and there is no alias at all. .

Therefore, the query results need to be queried again in a manner similar to a virtual table.

SELECT * FROM
(SELECT EMPLOYEE_ID, FIRST_NAME, SALARY, 
CASE
   WHEN SALARY >= 20000 THEN '高级'
   WHEN SALARY >= 10000 THEN '中级'
   WHEN SALARY >= 5000 THEN '初级'
   ELSE '见习'
END lv
 FROM t_employees) as t1 WHERE lv = '中级';

Note: When querying the query results as a virtual table, you must give the query results an alias, otherwise an error will be reported.

5.10 Merge query

Merge the two query results into one result.

As mentioned earlier, in is not efficient and other methods should be used. For example:

To query department numbers 90 and 60, if you use in, as follows:

-- 使用in
SELECT * FROM t_employees WHERE DEPARTMENT_ID IN (90, 60);

You can split the query on 90 and 60 and merge the results:

SELECT * FROM t_employees WHERE DEPARTMENT_ID = 90
UNION ALL
SELECT * FROM t_employees WHERE DEPARTMENT_ID = 60;
-- 也可以使用
SELECT * FROM t_employees WHERE DEPARTMENT_ID = 90
UNION
SELECT * FROM t_employees WHERE DEPARTMENT_ID = 60;

Classic interview questions:

The difference between UNION and UNION ALL:

UNION will remove duplicate rows. UNION ALL will retain all rows.

Note: The number of queried columns must be consistent before merging, and all column contents in one row must be exactly the same as another row to be considered duplicate.

5.11 Connection query

5.11.1 Cross-connection (understanding)

The two tables completely intersect to form a Cartesian set, that is, if there are 3 pieces of data in table A and 5 pieces of data in table B, the result will be 15 records.

SELECT * FROM t_employees CROSS JOIN t_departments
-- 也可以写作
SELECT * FROM t_employees, t_departments

5.11.2 Inner joins

Two tables are joined based on a certain association condition. There will only be results if there is a correlation.

-- 查询所有的员工信息,表内有107条记录,但是有一个员工没有部门id
SELECT * FROM t_employees; 
-- 查询所有的部门信息,表内有32条记录
SELECT * FROM t_departments;
-- 关联查询部门和员工,共有106条记录,因为没有部门id的员工无法显示(没有关联)
SELECT * FROM t_employees e, t_departments d WHERE e.DEPARTMENT_ID = d.DEPARTMENT_ID;
-- 另一种使用内连接的写法
SELECT * FROM t_employees e INNER JOIN t_departments d ON e.DEPARTMENT_ID = d.DEPARTMENT_ID;

Note: INNER JOIN is a writing method that was added in later versions of MySQL, so before it was written using where and conditions.

5.11.3 Outer joins

Two tables are joined based on a certain association condition. Take the table corresponding to the connection direction as the basis (all records of the table will be displayed).

Left outer join: based on the table on the left.

Right outer join: based on the table on the right.

-- left outer join 可以简写为 left join,以左边的表为准,会先显示内连接的所有106条记录,然后显示左边表中没有关联的数据1条,一共107条记录,没有关联的部门信息显示为null
SELECT * FROM t_employees e left OUTER JOIN t_departments d ON e.DEPARTMENT_ID = d.DEPARTMENT_ID ORDER BY e.DEPARTMENT_ID;
-- 右外连接,以右边的表为基准,先显示内连接所有的数据106条,然后显示没有与员工关联的部门信息,员工部分显示为null
SELECT * FROM t_employees e right JOIN t_departments d ON e.DEPARTMENT_ID = d.DEPARTMENT_ID ORDER BY e.DEPARTMENT_ID;

5.12 Complex queries

– Query the number of people with greater than average salary in each department

1. Check the average salary of each department

SELECT DEPARTMENT_ID, AVG(SALARY) FROM t_employees GROUP BY DEPARTMENT_ID;

2. Query information about departments with greater than average salary

SELECT * FROM t_employees e INNER JOIN 
(SELECT DEPARTMENT_ID, AVG(SALARY) v FROM t_employees GROUP BY DEPARTMENT_ID) t1
ON e.DEPARTMENT_ID = t1.DEPARTMENT_ID WHERE SALARY > v;

3. Group the above results and find the quantity.

SELECT e.DEPARTMENT_ID, COUNT(1) FROM t_employees e INNER JOIN 
(SELECT DEPARTMENT_ID, AVG(SALARY) v FROM t_employees GROUP BY DEPARTMENT_ID) t1
ON e.DEPARTMENT_ID = t1.DEPARTMENT_ID WHERE SALARY > v
GROUP BY e.DEPARTMENT_ID;

5.13 Row to Column [Classic Interview Question]

The default table data display method is as follows: table name: score1

Namestu_name Course co_name Score
Zhang San Java 90
Zhang San SQL 95
Zhang San HTML 88

It needs to be displayed in the form of columns after querying:

Name Java SQL HTML
Zhang San 90 95 88

1. First use single table operation to create a single table, such as the first table style above.

Query all data

SELECT * FROM score1;

2. Since multiple new columns (Java, HTML, SQL) need to be used to display data, the case when method is used to conditionally display.

SELECT *,
CASE WHEN co_name = 'Java' THEN scores ELSE 0 END AS 'Java',
CASE WHEN co_name = 'HTML' THEN scores ELSE 0 END AS 'HTML',
CASE WHEN co_name = 'SQL' THEN scores ELSE 0 END AS 'SQL'
 FROM score1;

3. Remove other redundant columns

SELECT stu_name,
CASE WHEN co_name = 'Java' THEN scores ELSE 0 END AS 'Java',
CASE WHEN co_name = 'HTML' THEN scores ELSE 0 END AS 'HTML',
CASE WHEN co_name = 'SQL' THEN scores ELSE 0 END AS 'SQL'
 FROM score1;

4. Display in groups according to students’ names

SELECT stu_name,
CASE WHEN co_name = 'Java' THEN scores ELSE 0 END AS 'Java',
CASE WHEN co_name = 'HTML' THEN scores ELSE 0 END AS 'HTML',
CASE WHEN co_name = 'SQL' THEN scores ELSE 0 END AS 'SQL'
 FROM score1 group by stu_name;

5. After grouping, columns that are not related to the grouped column will only display the first row of data. In order to display the correct data, you can use the max function to find the maximum value of the column.

SELECT stu_name,
max(CASE WHEN co_name = 'Java' THEN scores ELSE 0 END) AS 'Java',
max(CASE WHEN co_name = 'HTML' THEN scores ELSE 0 END) AS 'HTML',
max(CASE WHEN co_name = 'SQL' THEN scores ELSE 0 END) AS 'SQL'
 FROM score1 GROUP BY stu_name;

Convert rows to columns of data in the exercises:

1. First display the data in the form of the previous table 1 (score1).

(SELECT
s.STU_NAME,
sc.DEGREE,
c.COU_NAME
FROM
student AS s
INNER JOIN score AS sc ON s.STU_ID = sc.STU_ID
INNER JOIN course AS c ON sc.COU_ID = c.COU_ID);

2. Treat the above display results as virtual table t1, and query and display rows and columns based on it.

SELECT STU_NAME,
MAX(CASE WHEN COU_NAME = '计算机导论' THEN DEGREE ELSE 0 END) AS '计算机导论',
MAX(CASE WHEN COU_NAME = '操作系统' THEN DEGREE ELSE 0 END) AS '操作系统',
MAX(CASE WHEN COU_NAME = '数字电路' THEN DEGREE ELSE 0 END) AS '数字电路'
 FROM
(SELECT
s.STU_NAME,
sc.DEGREE,
c.COU_NAME
FROM
student AS s
INNER JOIN score AS sc ON s.STU_ID = sc.STU_ID
INNER JOIN course AS c ON sc.COU_ID = c.COU_ID) t1 GROUP BY STU_NAME;

5.14 Query rows with duplicates in a certain column [Classic interview question]

The way to remove duplicates is relatively simple, just use the distinct keyword.

If you want to query rows with duplicate rows in a certain column, first group them according to the column and find the number of each group. If the number of the group is greater than 1, it means that the column is duplicated.

SELECT
 sc.COU_ID
FROM
score AS sc
GROUP BY COU_ID
HAVING COUNT(1) > 1;

6. DML operation

Data manipulation language, used to add, delete, and modify data in the database.

6.1 New

adding data.

grammar:

Add a row of records:

INSERT INTO table name (column name 1, column name 2, column name 3) VALUES (value 1, value 2, value 3)

When adding values ​​corresponding to all columns, you can also do the following:

INSERT INTO table name VALUES (value 1, value 2, value 3)

Add multiple rows of records at once:

INSERT INTO table name (column name 1, column name 2, column name 3) VALUES (value 1, value 2, value 3), (value 1, value 2, value 3), (value 1, value 2, value 3) , (value1, value2, value3);

-- 添加一行,指定部分列
INSERT INTO t_jobs(JOB_ID, MIN_SALARY, MAX_SALARY) VALUES('AAA', 50000, 100000);
-- 添加一行,不指定任何列,全列添加
INSERT INTO t_jobs VALUES('BBB', 'BBBBBBB', 50000, 100000);
-- 添加多行
INSERT INTO t_jobs(JOB_ID, MIN_SALARY, MAX_SALARY) VALUES('AAAA', 50000, 100000), ('AAAAA', 50000, 100000), ('AAAAAA', 50000, 100000);

Note: Values ​​and column names must correspond.

6.2 Modification

change the data:

grammar:

UPDATE table name SET column name 1 = value 1, column name 2 = value 2, column name 3 = value 3 WHERE condition;

Note: If no conditions are added when modifying, all rows will be modified.

UPDATE t_jobs SET JOB_TITLE = 'AAAAAAAAAA', MIN_SALARY = 2000 WHERE JOB_ID = 'AAA';

6.3 Delete and clear tables

delete data:

grammar:

DELETE FROM table name WHERE condition;

Note: If no conditions are added, all contents in the table will be deleted.

DELETE FROM t_jobs WHERE Job_id = 'AAA';

Clear table: clear the entire table data.

TRUNCATE TABLE table name;

TRUNCATE TABLE t_locations;

Classic interview questions:

Difference between TRUNCATE and DELETE when deleting all data?

  • DELETE is to delete items one by one. TRUNCATE directly deletes the table itself and then re-creates it.
  • DELETE will retain the auto-incremented number when deleting the table, while TRUNCATE will auto-increment and restart the calculation.

7. MySQL field data types

7.1 Overview of data types

Field data types are broadly divided into two categories:

  • Without single quotes: numbers
  • With single quotes:
    • Date and time, with single quotes, but the format must be as required
    • String, with single quotes, no format required

7.2 Numeric types

int: integer, also similar to Java, here called tinyint, smallint, int, bigint

double: decimal

decimal: decimal

Note: double and decimal can also limit the length and decimal places. For example, double(5,2) means a length of 5 digits, a decimal place of 2 digits, and a range of -999.99~999.99.

7.3 Date type

date: date, for example: 2021-10-10

time: time, for example: 13:14:15

datetime: date time, for example: 2021-10-10 13:14:15

timestamp: timestamp

7.4 String type

char: fixed-length string, for example: char(4)

varchar: variable length string, for example: varchar(4)

blob: binary

text: long text

Note: In the database, the length must be specified when using char and varchar. The length cannot be exceeded when adding data, and it will be truncated if exceeded.

The difference between char and varchar is that if you use char but the added content does not reach the length, it will be automatically filled with spaces later, but varchar will not. For example, the length above is 4. If you add content tom, if the char type is used, tom spaces will be displayed. If the varchar type is used, tom will be displayed.

Therefore, in the project, unless you know that the field must have a fixed length, you will use char (gender column). Most of them use varchar.

8. Table structure operations

8.1 Table creation

Several elements for table creation:

1. Table name

2. The name and type of each column

3. Rules (restraints)

Basic syntax:

CREATE TABLE table name (

​ Column name type constraints,

​ Column name type constraints,

Column name type constraints

) character set;

CREATE TABLE product(
	id INT NOT NULL,
  pro_name VARCHAR(20) NULL,
  create_time datetime
) charset=utf8;

8.2 Table modification

grammar:

ALTER TABLE table name operation;

8.2.1 Adding columns

ALTER TABLE product ADD price INT NOT NULL;

8.2.2 Modify columns

ALTER TABLE product MODIFY price DOUBLE;

8.2.3 Modify column name

ALTER TABLE product CHANGE price pro_price DOUBLE NOT NULL;

8.2.4 Delete columns

ALTER TABLE product DROP pro_price;

Note: Modification operations on the table will be affected by the data, and the modification may not be successful.

For example: the column type in a table is varchar(20), and there is a record 'abc' in the table. If you modify the table and change the column type to int, an error will be reported.

Suggestion: Export the table data first, then clear the table, then modify the table, and finally import the data.

8.2.5 Modify table name

ALTER TABLE product RENAME pro;

8.3 Delete table

Delete the table structure.

Syntax: DROP TABLE table name;

DROP TABLE pro;

9. Three paradigms in the table design process

Normal form: refers to the rules for table design.

In theory, table design can have five normal forms, but in practice it cannot be achieved, so generally designed tables only need to meet three normal forms.

Three paradigms:

First normal form: domain (field) integrity. Columns should be non-divisible.

Second normal form: entity integrity, primary key constraints. All columns in each row should be completely dependent on the primary key.

Third normal form: referential integrity, (foreign key constraints). You can only reference the primary key of another table as a foreign key.

Note: All subsequent paradigms are based on the previous paradigm, that is, the second paradigm must first satisfy the first paradigm.

10. Constraints

10.1 Entity integrity constraints

10.1.1 Primary key

Use primary key to identify the column as the primary key, which cannot be repeated or empty.

CREATE TABLE product(
  id INT PRIMARY KEY,
  pro_name VARCHAR(20),
  create_time datetime
);
CREATE TABLE product1(
  id INT ,
  pro_name VARCHAR(20),
  create_time datetime,
  PRIMARY KEY (id)
);

When multiple columns are used as the primary key at the same time, it is still one primary key, but multiple columns must be used together to determine whether they are duplicated.

-- 指定多列同时做主键
CREATE TABLE score1(
  stu_id INT,
  cou_id INT,
  socre INT,
  PRIMARY KEY(stu_id, cou_id)
);

10.1.2 Unique keys

When a table is defined, in addition to the primary key, there are other columns that need to be unique. You can use the keyword UNIQUE.

Note: The primary key not only needs to be unique, but also cannot be empty. However, UNIQUE only needs to be unique and can be empty, and can have multiple rows. In this case, it needs to be set to NOT NULL.

CREATE TABLE product(
  id INT PRIMARY KEY,
  pro_name VARCHAR(20) UNIQUE NOT NULL,
  create_time datetime
);

10.1.3 Automatic growth

In mysql, the int type can be set to auto-increment. And it needs to be used together with the primary key and cannot be used alone. Other databases are used differently from mysql.

CREATE TABLE product(
  id INT PRIMARY KEY auto_increment,
  pro_name VARCHAR(20) UNIQUE NOT NULL,
  create_time datetime
);

10.2 Domain Integrity

Set the correctness of the cell.

  • NOT NULL
  • Default value, DEFAULT
CREATE TABLE product(
  id INT PRIMARY KEY auto_increment,
  pro_name VARCHAR(20) UNIQUE NOT NULL,
  create_time datetime,
	price int DEFAULT 1
);

Note: Even if the int type is allowed to be empty, if no value is set, it will default to null. When the default value is set, the default value will be used.

10.3 Referential integrity

Foreign key columns in one table must reference primary key columns in another table.

grammar:

CONSTRAINT fk_t FOREIGN KEY (type_id) REFERENCES pro_type(id)

type_id is a foreign key column in the current table

pro_type(id) is the name of the referenced table and the corresponding primary key column

fk_t is the name of the foreign key in the current table (arbitrarily chosen, but the name cannot be repeated in different tables)

CREATE TABLE product(
 id INT PRIMARY KEY auto_increment,
 pro_name VARCHAR(20) NOT NULL,
 type_id INT NOT NULL, 
 CONSTRAINT fk_t FOREIGN KEY (type_id) REFERENCES pro_type(id)
);

Note: If there is already data in the table and you want to establish a reference foreign key relationship by modifying the table, an error may be reported. It is recommended to delete the data before establishing it.

11. Permission management

11.1 Create user

grammar:

CREATE USER username IDENTIFIED by password;

CREATE USER fenghua IDENTIFIED by '123456';

11.2 Authorization

Syntax: GRANT permission ON database name. table name TO user name

SELECT\DELETE\UPDATE\INSERT

-- 给与fenghua用户对于companydb数据库所有的表查询权限
GRANT SELECT ON companydb.* TO fenghua;
-- 给与fenghua用户对于所有库所有的表所有权限
GRANT ALL ON *.* TO fenghua;

11.3 Revoking permissions

REVOKE permission ON database name. table name FROM user name;

-- 撤销给与的fenghua用户对于companydb数据库所有的表查询权限
REVOKE SELECT ON companydb.* FROM fenghua;

11.4 Delete users

Syntax: DROP USER username;

DROP USER fenghua;

12. View

A view can be understood as a virtual table, which actually uses a complex query statement as a variable.

Grammatical structures:

Create a view:

Method 1: CREATE VIEW view name AS query statement

Method 2: CREATE OR REPLACE VIEW view name AS query statement. The characteristic of this method is that if the view already exists, it will be replaced. If it does not exist, it will be created.

Modify the view:

ALTER VIEW view name AS query statement

Delete a view:

DROP VIEW view name

Note: The view is just a virtual variable (table), and deleting (DROP) and modifying (ALTER) the view does not affect the table corresponding to the view.

Use views to complete complex queries in 5.12:

-- 查询每个部门大于平均薪资的人数,原来的写法如下
SELECT e.DEPARTMENT_ID, COUNT(1) FROM t_employees e INNER JOIN 
(SELECT DEPARTMENT_ID, AVG(SALARY) v FROM t_employees GROUP BY DEPARTMENT_ID) t1
ON e.DEPARTMENT_ID = t1.DEPARTMENT_ID WHERE SALARY > v
GROUP BY e.DEPARTMENT_ID;
-- 将代码中的子查询使用视图
CREATE VIEW v1
AS
SELECT DEPARTMENT_ID, AVG(SALARY) v FROM t_employees GROUP BY DEPARTMENT_ID;
-- 修改原来的代码,使用视图替换
SELECT e.DEPARTMENT_ID, COUNT(1) FROM t_employees e INNER JOIN 
v1
ON e.DEPARTMENT_ID = v1.DEPARTMENT_ID WHERE SALARY > v
GROUP BY e.DEPARTMENT_ID;

Convert the original rows to columns using views:

-- 原来的sql如下,参考5.13
SELECT STU_NAME,
MAX(CASE WHEN COU_NAME = '计算机导论' THEN DEGREE ELSE 0 END) AS '计算机导论',
MAX(CASE WHEN COU_NAME = '操作系统' THEN DEGREE ELSE 0 END) AS '操作系统',
MAX(CASE WHEN COU_NAME = '数字电路' THEN DEGREE ELSE 0 END) AS '数字电路'
 FROM
(SELECT
s.STU_NAME,
sc.DEGREE,
c.COU_NAME
FROM
student AS s
INNER JOIN score AS sc ON s.STU_ID = sc.STU_ID
INNER JOIN course AS c ON sc.COU_ID = c.COU_ID) t1 GROUP BY STU_NAME;
-- 将上面的3表关联查询创建为一个视图
CREATE VIEW v2 AS
SELECT
s.STU_NAME,
sc.DEGREE,
c.COU_NAME
FROM
student AS s
INNER JOIN score AS sc ON s.STU_ID = sc.STU_ID
INNER JOIN course AS c ON sc.COU_ID = c.COU_ID;
-- 将上面的语句使用视图来完成
SELECT STU_NAME,
MAX(CASE WHEN COU_NAME = '计算机导论' THEN DEGREE ELSE 0 END) AS '计算机导论',
MAX(CASE WHEN COU_NAME = '操作系统' THEN DEGREE ELSE 0 END) AS '操作系统',
MAX(CASE WHEN COU_NAME = '数字电路' THEN DEGREE ELSE 0 END) AS '数字电路'
 FROM
v2 GROUP BY STU_NAME;

Notice:

  • A view is equivalent to a temporary table (query result), but when used, the original query statement is still substituted for execution, so the view does not store data. If the data in the original table changes, the results of the view will also change.
  • There is no optimization for query performance, it just simplifies the query statement.
  • Views can update and modify data (modifying view data is equivalent to modifying original table data). However, if the creation statement of the view contains any of the following, the data cannot be updated:
    • There are aggregate functions
    • used distinct
    • Used group by
    • used having
    • Using union or union all

13. Classification of SQL

DDL: data definition language, create tables, databases, etc. CREATE\ALTER\DROP

DCL: Data Control Language, Permissions, GRANT\REVOKE

DML: data manipulation language, addition, deletion and modification, INSERT\UPDATE\DELETE

DQL: Data query language, query data, SELECT\GROUP BY\ORDER BY\HAVING

TPL (TCL): transaction processing language, transaction processing, COMMIT\ROLLBACK

14. Affairs

14.1 Transaction scenarios

transfer. This should be a business operation (method) in the project.

In fact, this business operation will form two modification statements in the database, namely modifying account A to reduce money and modifying account B to add money.

If after modifying account A, an exception occurs when modifying account B and the modification fails, then there will be a situation where the money is reduced in account A, but the money is not added to account B, which is unreasonable.

At this time, transactions need to be used.

14.2 The concept of transactions

A transaction is an atomic operation consisting of one or more SQL statements.

In a transaction, it will succeed only if all SQLs are executed successfully. If one SQL fails to execute, all will fail.

14.3 Simple principles

The above case is executed in the database. If transactions are used (that is, no automatic submission), all modification operations are executed in the cache. If all executions are successful, the results will be written to the database (synchronized data) ( Commit the transaction), if any of them cannot be executed successfully, it will not be written to the database and the operation will be revoked (review the transaction).

14.4 Specific operations

Note: In the database, transactions are automatically submitted by default, that is, each SQL statement is an independent transaction by default, so after the SQL statement is executed successfully, it will be automatically submitted to the database.

Therefore, if you need to use transactions, you should first turn off the automatic commit of the transaction. Note: After the transaction is executed, you need to restore the automatic commit settings.

Transaction submission:

COMMIT, commits the transaction and synchronizes (writes) the cached data to the database.

Transaction Review: (Cancelled)

ROLLBACK, rolls back the transaction and undoes previous database operations.

Set auto-submit parameters:

SET AUTOCOMMIT = 0; Turn off automatic submission

SET AUTOCOMMIT = 1; Turn on automatic submission

SELECT @@autocommit; -- 查看系统是否自动提交 1:自动提交 0:不自动提交
set autocommit=0; -- 修改为不自动提交
-- 减钱
UPDATE account SET money = money - 500 WHERE account = '1001';
-- 加钱
UPDATE account SET money = money + 500 WHERE account = '1002';
COMMIT; -- 提交事务
set autocommit=1; -- 还原设置,修改为自动提交
set autocommit=0; -- 修改为不自动提交
-- 减钱
UPDATE account SET money = money - 500 WHERE account = '1001';
-- 加钱
UPDATE account SET money = money + 500 WHERE account = '1002';
ROLLBACK; -- 回滚事务
set autocommit=1; -- 还原设置,修改为自动提交

14.5 Four characteristics of transactions

A (atomicity): The transaction is a whole, either all succeed or all fail.

C (Consistency): Indicates that the state of the data must be consistent during the transaction operation. One statement fails and all others are rolled back.

I (Isolation): Transaction operations are isolated from each other. You can only see the state before or after the transaction operation, but not the state during the transaction operation.

D (persistence): After the transaction execution is completed, the results are permanently written to the database.

Memory Tips: One Yuan Nine. (Ichihara long time apart)

14.6 Understanding transaction isolation

Transactions should be isolated from each other.

Security and performance: If the same data is involved, how to choose between security and performance between transactions.

14.7 Transaction isolation levels

  • Read uncommitted, one transaction can read the uncommitted data of another transaction. Will cause dirty reads, virtual reads (non-repeatable reads), and phantom reads.
  • Read committed, one transaction can read committed data in another transaction. Will cause wasted time and phantom reading problems
  • Repeatable reading, only phantom reading problems will occur
  • Serializable, transactions are executed one by one, performance will be reduced to extremely low, and security will be increased to extremely high

Note: From top to bottom, the performance is getting worse and the security is getting higher and higher. In the project, a trade-off needs to be made between performance and security. So it is basically impossible to set the level to read uncommitted or serializable.

On the setting of the isolation level of transactions in the database:

Mainstream databases are set to read-committed by default. But only MySQL's default settings are repeatable reads.

14.8 Dirty read, non-repeatable read, phantom read

Dirty read: One transaction can read uncommitted data from another transaction. Dirty reading will not be allowed by any data provider, so the occurrence of dirty reading is generally considered to be a system BUG and needs to be modified.

Non-repeatable reading is also called virtual reading. In a transaction, the data read twice is inconsistent. At this time, there may be some impact for users, but for data providers, there is no impact on security. Therefore, due to security and performance considerations, most mainstream databases choose this isolation level.

Phantom reading. In a transaction, the number of data read twice is inconsistent (increasing or decreasing). At this time, there may be some impact for users, but for data providers, there is no impact on security. This isolation level can also be selected, and it is not much different from the previous isolation level in terms of performance and security.

Guess you like

Origin blog.csdn.net/asdf12388999/article/details/127230488