MySQL 将 NULL 值插入 INT 列

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5338529/
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 19:09:02  来源:igfitidea点击:

Insert NULL value into INT column

mysql

提问by misho

How can I insert NULL value into INT column with MySQL? Is it possible?

如何使用 MySQL 将 NULL 值插入到 INT 列中?是否可以?

回答by sheikhjabootie

If the column has the NOT NULLconstraint then it won't be possible; but otherwise this is fine:

如果该列具有NOT NULL约束,则不可能;但除此之外,这很好:

INSERT INTO MyTable(MyIntColumn) VALUES(NULL);

回答by RichardTheKiwi

2 ways to do it

2 种方法

insert tbl (other, col1, intcol) values ('abc', 123, NULL)

插入 tbl (other, col1, intcol) 值 ('abc', 123, NULL)

or just omit it from the column list

或者只是从列列表中省略它

insert tbl (other, col1) values ('abc', 123)

插入 tbl (other, col1) 值 ('abc', 123)

回答by Billy

Does the column allow null?

该列是否允许为空?

Seems to work. Just tested with phpMyAdmin, the column is of type int that allows nulls:

似乎工作。刚刚用 phpMyAdmin 测试过,该列的类型是允许空值的 int:

INSERT INTO `database`.`table` (`column`) VALUES (NULL);

回答by Andrey

If column is not NOT NULL(nullable).

如果列不是NOT NULL(可为空)。

You just put NULLinstead of value in INSERTstatement.

您只需NULLINSERT语句中放入而不是值。

回答by Piyush Dangre

Just use the insert query and put the NULL keyword without quotes. That will work-

只需使用插入查询并放置不带引号的 NULL 关键字。那可行-

INSERT INTO `myDatabase`.`myTable` (`myColumn`) VALUES (NULL);

回答by vineeth

The optimal solution for this is provide it as '0' and while using string use it as 'null' when using integer.

对此的最佳解决方案是将其提供为“0”,并在使用字符串时将其用作“空”,而在使用整数时。

ex:

前任:

INSERT INTO table_name(column_name) VALUES(NULL);