Mysql:将 NOT NULL 列更新为 null 时未收到错误

标签 mysql sql database laravel exception

为什么mysql在更新非空列时接受空数据,然后将数据转换为0。

我期待一个错误,但它没有显示出来。如果有人尝试将非空列更新为空,我如何得到错误?我需要它,以便在出现错误时可以回滚事务。数据库中是否需要任何配置来执行此操作?谢谢

enter image description here

最佳答案

您还没有指定您正在使用哪个版本的 Mysql,以及在哪种模式下。我会假设您正在运行 Mysql 5.7,没有严格模式来回答这个问题。

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.

If strict mode is not in effect, MySQL inserts adjusted values for invalid or missing values and produces warnings (see Section 13.7.5.40, “SHOW WARNINGS Syntax”). In strict mode, you can produce this behavior by using INSERT IGNORE or UPDATE IGNORE.

来源:https://dev.mysql.com/doc/refman/5.7/en/sql-mode.html#sql-mode-full

我建议您启用严格模式( STRICT_ALL_TABLES )并修复您的应用程序以支持它;这还将强制执行其他查询限制,人们最常受到 ONLY_FULL_GROUP_BY 的影响。 .

To set the SQL mode at server startup, use the --sql-mode="modes" option on the command line, or sql-mode="modes" in an option file such as my.cnf (Unix operating systems) or my.ini (Windows). modes is a list of different modes separated by commas. To clear the SQL mode explicitly, set it to an empty string using --sql-mode="" on the command line, or sql-mode="" in an option file.

To change the SQL mode at runtime, set the global or session sql_mode system variable using a SET statement:

SET GLOBAL sql_mode = 'modes';
SET SESSION sql_mode = 'modes';

来源:https://dev.mysql.com/doc/refman/5.7/en/sql-mode.html#sql-mode-setting

关于Mysql:将 NOT NULL 列更新为 null 时未收到错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46600333/

相关文章:

mysql - 需要从 MySQL 中获取行

sql - 冒号 (':' ) 在 SQL 语法中是什么意思?

mysql - 更新存储过程中的不同列

java - Spring-Roo 删除 Mysql,尽管 hibernate.hbm2ddl.auto

MYSQL - 选择两列中的不同值

mysql - Django 无法连接到 MySQL

sql - 使用 "to_tsquery"和 "simple"时,tsvector 上的 "english"会产生不同的结果?

MYSQL 提取值

mysql - MAMP phpmyadmin 数据库文件不在预期位置

mysql - 如何使用 Derby 数据库?