8

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 adds 0 to that column. I tried to change the column default value to NULL and it told "Invalid default value".

Is there any way to get an empty row without having there zero?

3
  • Please show CREATE TABLE statement for your table and how you insert new rows. Commented Nov 24, 2013 at 8:40
  • Are you sure the field is nullable? Commented Nov 24, 2013 at 8:40
  • possible duplicate of How do I modify a MySQL column to allow NULL? Commented Nov 24, 2013 at 10:41

4 Answers 4

5

"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:

`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;
Sign up to request clarification or add additional context in comments.

Comments

1

If your columns are NULL'able then it should work just fine

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)

Comments

0

In that case you need to change your datatype into varchar and add default value NULL.

ALTER TABLE <table_name> ALTER COLUMN <column_name1> <datatype1> <constraint1>

1 Comment

As long as the datatype is non-numerical, any form of String is fine too.
0

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?

1 Comment

That does not answer the question as it concerns a text column not an integer column

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.