Evgenij Reznik
Evgenij Reznik

Reputation: 18594

Null or empty instead of 0

I created a new table with:

CREATE TABLE `test`.`tab1` ( `id` INT NOT NULL AUTO_INCREMENT , `name` VARCHAR(50) NULL , `age` INT NULL , PRIMARY KEY (`id`));

When I insert a new row, but want to keep age empty, I do:

INSERT INTO `tab1` (`id`, `Name`, `Age`) VALUES (NULL, 'Peter', '');

I get this entry:

| id | name  | age |
|----|-------|-----|
| 1  | Peter | 0   |

Why the age column just doesn't remain empty or NULL?
How can I set a default value, so that it just remains empty, when no value is specified while inserting?

Upvotes: 0

Views: 3153

Answers (1)

juergen d
juergen d

Reputation: 204766

Since the column is of type int the DB tries to convert the string '' to a number. That would be 0.

Just use null instead of '' if you don't have a value. That is what null is for.

Upvotes: 3

Related Questions