Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Insert NULL value into INT column

Tags:

mysql

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

like image 736
misho Avatar asked Mar 17 '11 11:03

misho


People also ask

Can we insert NULL in integer column?

You can insert NULL value into an int column with a condition i.e. the column must not have NOT NULL constraints.

How do you store NULL in integer?

You just put NULL instead of value in INSERT statement. Show activity on this post. Show activity on this post. The optimal solution for this is provide it as '0' and while using string use it as 'null' when using integer.

How do you insert a null value?

You also can specify the NULL keyword in the VALUES clause to indicate that a column should be assigned a NULL value. The following example inserts values into three columns of the orders table: INSERT INTO orders (orders_num, order_date, customer_num) VALUES (0, NULL, 123);

How do I insert a null value into a NOT NULL column?

Code Inspection: Insert NULL into NOT NULL column You cannot insert NULL values in col1 and col2 because they are defined as NOT NULL. If you run the script as is, you will receive an error. To fix this code, replace NULL in the VALUES part with some values (for example, 42 and 'bird' ).


2 Answers

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); 
like image 62
sheikhjabootie Avatar answered Oct 12 '22 08:10

sheikhjabootie


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)

like image 20
RichardTheKiwi Avatar answered Oct 12 '22 06:10

RichardTheKiwi