MySQL 如果没有提供默认值,字段的默认值是多少?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8558260/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 22:06:26  来源:igfitidea点击:

What is the default value for a field if no default value is provided?

mysqlphpmyadmindefault-value

提问by lollercoaster

I hope this isn't a dumb question. You can set a default value for all variables or a function for when it is inserted. but if the field is not required to insert and you don't allow null values, what is the "blank" value that you see in phpMyAdmin? in a query is it returned as empty string, etc?

我希望这不是一个愚蠢的问题。您可以在插入时为所有变量或函数设置默认值。但是如果不需要插入该字段并且您不允许空值,那么您在 phpMyAdmin 中看到的“空白”值是多少?在查询中它是否以空字符串等形式返回?

just trying to figure it out, I want to query for all records such that the value for a specific column in that record is not "empty" or blank or whatever.

只是想弄清楚,我想查询所有记录,以便该记录中特定列的值不是“空”或空白或其他。

thanks.

谢谢。

回答by Question Overflow

Referring to the manual,

参考手册

For data entry for a NOT NULL column that has no explicit DEFAULT clause, if an INSERT or REPLACE statement includes no value for the column, or an UPDATE statement sets the column to NULL, MySQL handles the column according to the SQL mode in effect at the time:

  • If strict SQL mode is not enabled, MySQL sets the column to the implicit default value for the column data type.
  • If strict mode is enabled, an error occurs for transactional tables and the statement is rolled back. For nontransactional tables, an
    error occurs, but if this happens for the second or subsequent row of a multiple-row statement, the preceding rows will have been inserted.

对于没有显式 DEFAULT 子句的 NOT NULL 列的数据条目,如果 INSERT 或 REPLACE 语句不包含该列的值,或者 UPDATE 语句将该列设置为 NULL,MySQL 会根据当时生效的 SQL 模式处理该列时间:

  • 如果未启用严格 SQL 模式,MySQL 会将列设置为列数据类型的隐式默认值。
  • 如果启用了严格模式,事务表会发生错误并回滚语句。对于非事务性表,
    会发生错误,但如果多行语句的第二行或后续行发生这种情况,则前面的行将被插入。

So your question now may be, what are the implicit default values for the various column data types? Here you go:

所以您现在的问题可能是,各种列数据类型的隐式默认值是什么?干得好:

Implicit defaults are defined as follows:

  • For numeric types, the default is 0, with the exception that for integer or floating-point types declared with the AUTO_INCREMENT
    attribute, the default is the next value in the sequence.
  • For date and time types other than TIMESTAMP, the default is the appropriate “zero” value for the type. For the first TIMESTAMP column in a table, the default value is the current date and time. See Section 10.3, “Date and Time Types”.
  • For string types other than ENUM, the default value is the empty string. For ENUM, the default is the first enumeration value.

隐式默认值定义如下:

  • 对于数字类型,默认值为 0,但对于使用 AUTO_INCREMENT
    属性声明的整数或浮点类型,默认值为序列中的下一个值。
  • 对于 TIMESTAMP 以外的日期和时间类型,默认值为该类型的适当“零”值。对于表中的第一个 TIMESTAMP 列,默认值为当前日期和时间。请参见第 10.3 节,“日期和时间类型”。
  • 对于 ENUM 以外的字符串类型,默认值为空字符串。对于 ENUM,默认值为第一个枚举值。

回答by paulsm4

There IS no default value unless you specify one (i.e. unless you define a "default constraint" for the column in question).

除非您指定一个(即,除非您为相关列定义“默认约束”),否则没有默认值。

Here's an example for adding a default on an existing column:

这是在现有列上添加默认值的示例:

ALTER TABLE dbo.customer ALTER COLUMN contactname SET DEFAULT 'Unknown'

Here's an example creating the table with a default:

这是使用默认值创建表的示例:

CREATE TABLE Books (
  ID SMALLINT NOT NULL,
  Name VARCHAR(40) NOT NULL,
  PubID SMALLINT NOT NULL DEFAULT 0
)

It's good practice to declare ALL columns "not null", and provide default constraints as appropriate.

将所有列声明为“非空”并根据需要提供默认约束是一种很好的做法。

In the "books" example above, if you "insert" without specifying PubID, the PubID will be zero.

在上面的“books”示例中,如果您在未指定 PubID 的情况下“插入”,则 PubID 将为零。

In the same example, if you "insert" without specifying ID or Name ... you'll get an error.

在同一个例子中,如果你在没有指定 ID 或 Name 的情况下“插入”......你会得到一个错误。

If you want MySQL to auto-assign an ID, use this syntax instead:

如果您希望 MySQL 自动分配 ID,请改用以下语法:

CREATE TABLE Books (
  ID SMALLINT NOT NULL AUTO_INCREMENT,
  Name VARCHAR(40) NOT NULL,
  PubID SMALLINT NOT NULL DEFAULT 0
)

回答by ajreal

If you want to disallow null :-

如果你想禁止 null :-

alter table YOUR_TABLE modify column COLUMN varchar(255) not null default '';

The above query will disallow null and assign an empty string when the value is not supplied.
In phpmysqladmin, blank = empty.
Via PHP mysqli functionor mysql function, null value is returned as nullstill.

上面的查询将禁止 null 并在未提供值时分配一个空字符串。
在 phpmysqladmin 中,blank = empty.
通过 PHP mysqli 函数mysql 函数null value is returned as null仍然。

Once you have apply the query, you can easily filter that by using

应用查询后,您可以使用

select ... from YOUR_TABLE 
where COLUMN != "";        <-- no need to check is null
                           <-- because the first query already enforce not null

However, is best for you do this before perform the alter :-

但是,最好在执行更改之前执行此操作:-

update YOUR_TABLE set COLUMN = ""
where COLUMN is null;