MySQL Mysql整数默认值0
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20172626/
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
Mysql integer default value 0
提问by user3026704
I have 4 integer columns in my table. They are not required to be filled. So some of them may be filled, some not. When they are not filled, mysql add 0 to that column. I tried to change column default value to NULL and it told Invalid default value. Is there any way to get empty row without having there the zero?
我的表中有 4 个整数列。它们不需要填写。因此,其中一些可能会被填满,有些则不会。当它们未填充时,mysql 将 0 添加到该列。我试图将列默认值更改为 NULL,它告诉无效默认值。有没有办法在没有零的情况下获得空行?
回答by user4035
"Is there any way to get empty row without having there the zero?"
“有没有办法在没有零的情况下获得空行?”
To have NULL in the column by default use the following syntax in create table:
要在列中默认为 NULL,请在创建表中使用以下语法:
`column` int(10) unsigned DEFAULT NULL,
To alter the existing column:
要更改现有列:
ALTER TABLE table_name CHANGE COLUMN `column_name` `column_name` int(10) unsigned DEFAULT NULL;
回答by peterm
If your columns are NULL'able then it should work just fine
如果您的列可以为 NULL,那么它应该可以正常工作
mysql> CREATE TABLE Table1 -> (id int not null auto_increment primary key, -> `col1` int, `col2` int, `col3` int, `col4` int); Query OK, 0 rows affected (0.03 sec) mysql> mysql> INSERT INTO Table1 (`col1`, `col2`, `col3`, `col4`) -> VALUES (1, 1, 1, 1); Query OK, 1 row affected (0.03 sec) mysql> mysql> INSERT INTO Table1 () VALUES(); Query OK, 1 row affected (0.00 sec) mysql> SELECT * FROM table1; +----+------+------+------+------+ | id | col1 | col2 | col3 | col4 | +----+------+------+------+------+ | 1 | 1 | 1 | 1 | 1 | | 2 | NULL | NULL | NULL | NULL | +----+------+------+------+------+ 2 rows in set (0.00 sec)
回答by Krish R
In that case you need to change your datatype into varchar
and add default value NULL.
在这种情况下,您需要将数据类型更改为varchar
并添加默认值 NULL。
ALTER TABLE <table_name>
ALTER COLUMN <column_name1> <datatype1> <constraint1>
ALTER TABLE <table_name>
ALTER COLUMN <column_name1> <datatype1> <constraint1>
回答by nullop
You have to alter your column to allow NULL values. This question has already been answered before: How do I modify a MySQL column to allow NULL?
您必须更改列以允许 NULL 值。这个问题之前已经回答过: 如何修改 MySQL 列以允许 NULL?