指尖上的记忆指尖上的记忆
首页
  • 基础
  • Laravel框架
  • Symfony框架
  • 基础
  • Gin框架
  • 基础
  • Spring框架
  • 命令
  • Nginx
  • Ai
  • Deploy
  • Docker
  • K8s
  • Micro
  • RabbitMQ
  • Mysql
  • PostgreSsql
  • Redis
  • MongoDb
  • Html
  • Js
  • 前端
  • 后端
  • Git
  • 知识扫盲
  • Golang
🌟 gitHub
首页
  • 基础
  • Laravel框架
  • Symfony框架
  • 基础
  • Gin框架
  • 基础
  • Spring框架
  • 命令
  • Nginx
  • Ai
  • Deploy
  • Docker
  • K8s
  • Micro
  • RabbitMQ
  • Mysql
  • PostgreSsql
  • Redis
  • MongoDb
  • Html
  • Js
  • 前端
  • 后端
  • Git
  • 知识扫盲
  • Golang
🌟 gitHub

mysql之sql-mode设置:

mysql8的默认sql_mode:
select @@sql_mode;
ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION
  
这个 ONLY_FULL_GROUP_BY 会在sql的select查询的时候报错,一般需要去掉,原因如下:
Reject queries for which the select list, HAVING condition, or ORDER BY list refer to nonaggregated columns that are neither named in the GROUP BY clause nor are functionally dependent on (uniquely determined by) GROUP BY columns.
对于选择列表、HAVING条件或ORDER BY列表中提到的非分组列的查询都会被拒绝,这些列既没有在GROUP BY子句中命名,也没有在功能上依赖于(由GROUP BY列唯一决定)的查询。
  
在配置文件 /etc/mysql/my.cnf 中 [mysqld] 下面加上一行
设置: sql_mode ='STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION'
  
对于STRICT_TRANS_TABLES:严格模式 一般也是要禁用的(大部分时候都只是禁用了ONLY_FULL_GROUP_BY),原因如下:
Strict mode controls how MySQL handles invalid or missing values in data-change statements such as INSERT or UPDATE. A value can be invalid for several reasons. For example, it might have the wrong data type for the column, or it might be out of range. A value is missing when a new row to be inserted does not contain a value for a non-NULL column that has no explicit DEFAULT clause in its definition. (For a NULL column, NULL is inserted if the value is missing.) Strict mode also affects DDL statements such as CREATE TABLE.
严格模式控制MySQL如何处理数据变更语句(如INSERT或UPDATE)中的无效或缺失值。一个值可能因为几个原因而无效。例如,它可能有错误的列的数据类型,或者它可能超出了范围。当要插入的新行不包含一个非NULL列的值时,一个值就会丢失,该列在其定义中没有明确的DEFAULT子句。(对于一个NULL列,如果数值缺失,则插入NULL。) 严格模式也影响到DDL语句,如CREATE TABLE。
  
生产上可以直接 sql_mode = ''