-1

I set default value in a field NOT NULL. The sql is as follows.

insert into table_name () values ()

I expected this sql should be rejected by MySQL as a field restricts NULL. But, the field had a value ""(null character string).

Then, I tried another sql.

insert into table_name (name) values (NULL)

As this sql was rejected, no value was inserted.

MySQL seems to distinguish NULL and "". Do I have to avoid first sql such as "values ()"?

4

2 回答 2

1

一个好的做法是始终使列不为空,并在创建表时设置默认值。

在插入时,如果您不提供值,则将为该列分配默认值。

于 2012-12-27T08:18:10.940 回答
0

我是否必须在设置为 NOT NULL 的字段中设置默认值?

,如果您正在执行INSERTwhere in 您将不会为 NOT NULL 列设置任何值

NO不再需要,如果您在 上为 NOT NULL 的列提供值INSERT

你可以参考这个

http://dev.mysql.com/doc/refman/5.0/en/data-type-defaults.html

于 2012-12-27T07:56:07.927 回答