1

I've got a database table for users that has a setup similar to this:

+---------------------+--------------+------+-----+------------+----------------+
| Field               | Type         | Null | Key | Default    | Extra          |
+---------------------+--------------+------+-----+------------+----------------+
| id_user             | int(5)       | NO   | PRI | NULL       | auto_increment |
| login               | varchar(50)  | NO   |     |            |                |
| password            | varchar(50)  | NO   |     |            |                |
| address             | varchar(255) | NO   |     | NULL       |                |
+---------------------+--------------+------+-----+------------+----------------+

Since the address field is set to not null and the default is null, this should effectively make the address field required, right?

But when I perform a query such as the following:

INSERT INTO mydatabase.users
SET
    mydatabase.users.login='test_username',
    mydatabase.users.password='test_password'

It successfully saves the user without complaining about the address field, which is set to an empty space.

What could be causing this? I expect an error in this type of situation. I've tried it with MySQL 5.5 and 5.6.

4

1 回答 1

3

配置文件中没有启用严格的 SQL 模式,因此 mysql 放松了对数据插入的某些控制:

严格模式控制 MySQL 如何处理数据更改语句(如 INSERT 或 UPDATE)中的无效或缺失值。一个值可能由于多种原因而无效。例如,它可能具有错误的列数据类型,或者可能超出范围。当要插入的新行不包含在其定义中没有显式 DEFAULT 子句的非 NULL 列的值时,缺少值。(对于 NULL 列,如果缺少值,则插入 NULL。)严格模式也会影响 DDL 语句,例如 CREATE TABLE。

于 2016-01-13T12:31:16.363 回答