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.
谢谢。
3 个解决方案
#1
23
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:
对于没有显式DEFAULT子句的NOT NULL列的数据输入,如果INSERT或REPLACE语句不包含该列的值,或者UPDATE语句将列设置为NULL,则MySQL将根据有效的SQL模式处理该列。时间:
- If strict SQL mode is not enabled, MySQL sets the column to the implicit default value for the column data type.
- 如果未启用严格的SQL模式,MySQL会将列设置为列数据类型的隐式默认值。
- 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.- 如果启用了严格模式,则事务表会发生错误,并且会回滚该语句。对于非事务性表,会发生错误,但如果多行语句的第二行或后续行发生这种情况,则会插入前面的行。
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.- 对于数字类型,缺省值为0,但对于使用AUTO_INCREMENT属性声明的整数或浮点类型,缺省值是序列中的下一个值。
- 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”.
- 对于TIMESTAMP以外的日期和时间类型,默认值为该类型的相应“零”值。对于表中的第一个TIMESTAMP列,默认值为当前日期和时间。请参见第10.3节“日期和时间类型”。
- For string types other than ENUM, the default value is the empty string. For ENUM, the default is the first enumeration value.
- 对于ENUM以外的字符串类型,默认值为空字符串。对于ENUM,默认值是第一个枚举值。
#2
0
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.
最好将所有列声明为“not null”,并根据需要提供默认约束。
In the "books" example above, if you "insert" without specifying PubID, the PubID will be zero.
在上面的“书籍”示例中,如果您在不指定PubID的情况下“插入”,则PubID将为零。
In the same example, if you "insert" without specifying ID or Name ... you'll get an error.
在同一示例中,如果您在不指定ID或名称的情况下“插入”,则会出现错误。
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
)
#3
-1
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 function or mysql function, null value is returned as null
still.
上面的查询将禁止null,并在未提供值时分配空字符串。在phpmysqladmin中,blank = empty。通过PHP mysqli函数或mysql函数,null值仍然返回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;
#1
23
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:
对于没有显式DEFAULT子句的NOT NULL列的数据输入,如果INSERT或REPLACE语句不包含该列的值,或者UPDATE语句将列设置为NULL,则MySQL将根据有效的SQL模式处理该列。时间:
- If strict SQL mode is not enabled, MySQL sets the column to the implicit default value for the column data type.
- 如果未启用严格的SQL模式,MySQL会将列设置为列数据类型的隐式默认值。
- 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.- 如果启用了严格模式,则事务表会发生错误,并且会回滚该语句。对于非事务性表,会发生错误,但如果多行语句的第二行或后续行发生这种情况,则会插入前面的行。
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.- 对于数字类型,缺省值为0,但对于使用AUTO_INCREMENT属性声明的整数或浮点类型,缺省值是序列中的下一个值。
- 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”.
- 对于TIMESTAMP以外的日期和时间类型,默认值为该类型的相应“零”值。对于表中的第一个TIMESTAMP列,默认值为当前日期和时间。请参见第10.3节“日期和时间类型”。
- For string types other than ENUM, the default value is the empty string. For ENUM, the default is the first enumeration value.
- 对于ENUM以外的字符串类型,默认值为空字符串。对于ENUM,默认值是第一个枚举值。
#2
0
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.
最好将所有列声明为“not null”,并根据需要提供默认约束。
In the "books" example above, if you "insert" without specifying PubID, the PubID will be zero.
在上面的“书籍”示例中,如果您在不指定PubID的情况下“插入”,则PubID将为零。
In the same example, if you "insert" without specifying ID or Name ... you'll get an error.
在同一示例中,如果您在不指定ID或名称的情况下“插入”,则会出现错误。
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
)
#3
-1
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 function or mysql function, null value is returned as null
still.
上面的查询将禁止null,并在未提供值时分配空字符串。在phpmysqladmin中,blank = empty。通过PHP mysqli函数或mysql函数,null值仍然返回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;