sql_mode is a variable that is easily overlooked. The default value in 5.5 is null. Under this setting, some illegal operations can be allowed, such as allowing the insertion of some illegal data.
This value setting has been strengthened in 5.6, and 5.7 has paid more attention to security specifications. This value defaults to strict mode
1. sql_mode is used to solve the following types of problems
By setting sql mode, data verification with different stringency levels can be completed to effectively ensure data readiness.
By setting the sql mode to relaxed mode, we ensure that most sql conforms to the standard sql syntax. In this way, when the application is migrated between different databases, there is no need to make major modifications to the business sql, and it can be easily Conveniently migrate to the target database.
2. Description of the default value of the sql_mode parameter in MySQL5.7 (the following is the MySQL 5.7.27 version)
ONLY_FULL_GROUP_BY
For SQL that uses GROUP BY for query, fields that do not appear in GROUP BY are not allowed to appear in the SELECT part. That is, the fields in the SELECT query must appear in GROUP BY or use aggregate functions or be Has unique properties.
create table test(name varchar(10),value int); insert into test values ('a',1),('a',20),('b',23),('c',15),('c',30); #默认情况是可能会写出无意义或错误的聚合语句: SET sql_mode=''; select * from test group by name; select value,sum(value) from test group by name; # 使用该模式后,写法必须标准 SET sql_mode='ONLY_FULL_GROUP_BY'; select name,sum(value) from test group by name; -- 错误写法则报错 select value,sum(value) from test group by name; # 报错终止 ERROR 1055 (42000): Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'test.test.value' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by
STRICT_TRANS_TABLES
This option only works for transactional storage engines and is invalid for non-transactional storage engines. , its function is to enable strict SQL mode. In strict sql mode, if a field value that does not meet the requirements is inserted or updated in an INSERT or UPDATE statement, an error will be reported directly and the operation will be interrupted
create table test(value int(1)); SET sql_mode=''; #默认只要第一个值 insert into test(value) values('a'),(1); #不报错 insert into test(value) values(2),('a'); #不报错 select * from test; +------------+ | value | +------------+ | 0 | | 1 | | 2 | | 0 | +------------+ #后面删除表不再说明! drop table test; create table test(value int(1)); SET sql_mode='STRICT_TRANS_TABLES'; #每个值都判断 insert into test(value) values('a'),(1); #报错,第一行'a'错误。 ERROR 1366 (HY000): Incorrect integer value: 'a' for column 'value' at row 1
NO_ZERO_IN_DATE
The time field value inserted in MySQL does not allow the date and month to be zero
create table test(value date); SET sql_mode=''; insert into test(value) values('2020-00-00'); #结果为 '2020-00-00' SET sql_mode='NO_ZERO_IN_DATE'; insert into test(value) values('2021-00-00'); #不符合,转为 '0000-00-00'
NO_ZERO_DATE
The time field value inserted in MySQL is not allowed to insert the ‘0000-00-00’ date
create table test(value date); SET sql_mode=''; insert into test(value) values('0000-00-00'); #无警告 warning SET sql_mode='STRICT_TRANS_TABLES'; insert into test(value) values('0000-00-00'); #无警告 warning SET sql_mode='NO_ZERO_DATE'; insert into test(value) values('0000-00-00'); #有警告 warning SET sql_mode='NO_ZERO_DATE,STRICT_TRANS_TABLES' insert into test(value) values('0000-00-00'); # 报错终止 ERROR 1292 (22007): Incorrect date value: '0000-00-00' for column 'value' at row 1
##ERROR_FOR_DIVISION_BY_ZERO
- When this option is turned off, the number is divided by 0, resulting in NULL and no warning is generated
- When this option is turned on and is in non-strict In sql mode, if the number is divided by 0, NULL will be obtained but a warning will be generated
- When this option is turned on and in strict sql mode, the number is divided by 0, an error will be generated and the operation will be interrupted
create table test(value int); SET sql_mode=''; select 10/0; #无警告 warning insert into test(value) values(10/0); #无警告 warning SET sql_mode='STRICT_TRANS_TABLES'; select 10/0; #无警告 warning insert into test(value) values(10/0); #无警告 warning SET sql_mode='ERROR_FOR_DIVISION_BY_ZERO'; select 10/0; #有警告 warning insert into test(value) values(10/0); #有警告 warning SET sql_mode='ERROR_FOR_DIVISION_BY_ZERO,STRICT_TRANS_TABLES'; select 10/0; #有警告 warning insert into test(value) values(10/0); #报错:ERROR 1365 (22012): Division by 0
- ##NO_AUTO_CREATE_USER
##Prohibit GRANT from creating users with empty passwords
SET sql_mode=''; grant all on test.* to test01@'localhost'; #不报错(无需要设置密码) SET sql_mode='NO_AUTO_CREATE_USER'; # 报错 ERROR 1133 (42000): Can't find any matching row in the user table #正确 写法,需要设置密码 grant all on test.* to test01@'localhost' identified by 'test01...';
- NO_ENGINE_SUBSTITUTION
-
#When using CREATE TABLE or ALTER TABLE syntax to execute the storage engine, if the set storage engine is disabled or not Compiling will produce an error.
# 查看当前支持的存储引擎 show engines; set sql_mode=''; create table test(id int) ENGINE="test"; Query OK, 0 rows affected, 2 warnings (0.03 sec) select table_name,engine from information_schema.tables where table_schema='test' and table_name='test'; # 转为默认存储引擎 +------------+--------+ | table_name | engine | +------------+--------+ | test | InnoDB | +------------+--------+ SET sql_mode='NO_ENGINE_SUBSTITUTION'; create table test(id int) ENGINE=test; # 报错 ERROR 1286 (42000): Unknown storage engine 'test'3. sql_mode setting and modificationMethod 1: This is a modifiable global variable
> show variables like '%sql_mode%';
> set @@sql_mode="NO_ENGINE_SUBSTITUTION"
> set session sql_mode='STRICT_TRANS_TABLES';
Method 2: By modifying the configuration file (requires restart to take effect)# vim /etc/my.cnf [mysqld] ...... sql_mode="NO_ENGINE_SUBSTITUTION" ......
The above is the detailed content of How to set SQL_MODE in MySQL 5.7. For more information, please follow other related articles on the PHP Chinese website!

InnoDB uses redologs and undologs to ensure data consistency and reliability. 1.redologs record data page modification to ensure crash recovery and transaction persistence. 2.undologs records the original data value and supports transaction rollback and MVCC.

Key metrics for EXPLAIN commands include type, key, rows, and Extra. 1) The type reflects the access type of the query. The higher the value, the higher the efficiency, such as const is better than ALL. 2) The key displays the index used, and NULL indicates no index. 3) rows estimates the number of scanned rows, affecting query performance. 4) Extra provides additional information, such as Usingfilesort prompts that it needs to be optimized.

Usingtemporary indicates that the need to create temporary tables in MySQL queries, which are commonly found in ORDERBY using DISTINCT, GROUPBY, or non-indexed columns. You can avoid the occurrence of indexes and rewrite queries and improve query performance. Specifically, when Usingtemporary appears in EXPLAIN output, it means that MySQL needs to create temporary tables to handle queries. This usually occurs when: 1) deduplication or grouping when using DISTINCT or GROUPBY; 2) sort when ORDERBY contains non-index columns; 3) use complex subquery or join operations. Optimization methods include: 1) ORDERBY and GROUPB

MySQL/InnoDB supports four transaction isolation levels: ReadUncommitted, ReadCommitted, RepeatableRead and Serializable. 1.ReadUncommitted allows reading of uncommitted data, which may cause dirty reading. 2. ReadCommitted avoids dirty reading, but non-repeatable reading may occur. 3.RepeatableRead is the default level, avoiding dirty reading and non-repeatable reading, but phantom reading may occur. 4. Serializable avoids all concurrency problems but reduces concurrency. Choosing the appropriate isolation level requires balancing data consistency and performance requirements.

MySQL is suitable for web applications and content management systems and is popular for its open source, high performance and ease of use. 1) Compared with PostgreSQL, MySQL performs better in simple queries and high concurrent read operations. 2) Compared with Oracle, MySQL is more popular among small and medium-sized enterprises because of its open source and low cost. 3) Compared with Microsoft SQL Server, MySQL is more suitable for cross-platform applications. 4) Unlike MongoDB, MySQL is more suitable for structured data and transaction processing.

MySQL index cardinality has a significant impact on query performance: 1. High cardinality index can more effectively narrow the data range and improve query efficiency; 2. Low cardinality index may lead to full table scanning and reduce query performance; 3. In joint index, high cardinality sequences should be placed in front to optimize query.

The MySQL learning path includes basic knowledge, core concepts, usage examples, and optimization techniques. 1) Understand basic concepts such as tables, rows, columns, and SQL queries. 2) Learn the definition, working principles and advantages of MySQL. 3) Master basic CRUD operations and advanced usage, such as indexes and stored procedures. 4) Familiar with common error debugging and performance optimization suggestions, such as rational use of indexes and optimization queries. Through these steps, you will have a full grasp of the use and optimization of MySQL.

MySQL's real-world applications include basic database design and complex query optimization. 1) Basic usage: used to store and manage user data, such as inserting, querying, updating and deleting user information. 2) Advanced usage: Handle complex business logic, such as order and inventory management of e-commerce platforms. 3) Performance optimization: Improve performance by rationally using indexes, partition tables and query caches.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

Zend Studio 13.0.1
Powerful PHP integrated development environment

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.