38

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

6 Answers 6

52

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

INSERT INTO MyTable(MyIntColumn) VALUES(NULL);
Sign up to request clarification or add additional context in comments.

Comments

16

2 ways to do it

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

or just omit it from the column list

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

Comments

4

Does the column allow null?

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

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

Comments

3

If column is not NOT NULL (nullable).

You just put NULL instead of value in INSERT statement.

Comments

0

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

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

Comments

-3

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

ex:

INSERT INTO table_name(column_name) VALUES(NULL);

1 Comment

0 and null has very different meanings. Null could mean we do not have the data. 0 means we know it is zero. Or lets imagine a referecence to user_id. 0 will point to record with id 0 while null means there is no user.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.