为什么 MySQL 将 null 更改为零? [英] Why is MySQL changing null to zero?
问题描述
我对数据库和 SQL 完全陌生.
我有一列标记为 NOT NULL
.
当我在执行 INSERT 时忘记给该列赋值时,它会在该列中插入一个 0 值.
有什么我可以做的让它返回错误而不是将 NULL
更改为 0?
I'm completely new to databases and SQL.
I have a column marked NOT NULL
.
When I forget to give a value to that column when doing an INSERT, it inserts a 0 value in that column.
Is there anything I can do to make it return an error instead of changing the NULL
to 0?
推荐答案
您想看看 SQL_MODE
配置.它允许您定义 MySQL 处理此类事情的严格程度.默认情况下它非常宽松,这不是我通常想要的.它还取决于数据类型,尤其是日期,默认情况下它不是最佳的.
You want to have a look at the SQL_MODE
configuration. It allows you to define how strict MySQL handles such things. By default it is pretty lenient which is not what I usually want. It also depends on the data type, especially with Dates it is less than optimal by default.
我个人通常选择STRICT_ALL_TABLES
.
在此处查看 MySQL 手册 (5.0):
See the MySQL manual (5.0) here:
http://dev.mysql.com/doc/refman/5.0/en/server-sql-mode.html
这篇关于为什么 MySQL 将 null 更改为零?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!