0

I'm trying to adding a column into a table whose fields are empty, neither "0" nor "null".

"ALTER TABLE `mytable` ADD COLUMN `newcolumn` INT NOT NULL"

makes all fields "0". I also tried DEFAULT='' option but then the column doesn't even added. So how to achieve really blank fields?

3
  • You have set default value for existing data of INTEGER type Commented Jan 16, 2013 at 13:15
  • i don't think an integer couldn't be a number nor null. Commented Jan 16, 2013 at 13:15
  • Does "null" in mysql table mean the null constant or the word null? Commented Jan 16, 2013 at 13:28

5 Answers 5

1

You are adding an INT field. It may be NULL or 0, but it can't be the empty string, since that's a string, and not a number.

Sign up to request clarification or add additional context in comments.

Comments

1

You have specified NOT NULL. This means that the fields cannot be null i.e. empty.

If you want "really blank" fields, then change the column specification to:

 "ALTER TABLE `mytable` ADD COLUMN `newcolumn` INT NULL"

Comments

1

You can't.

The only valid values for the INTEGER column are numbers and NULL. You cannot simply define DEFAULT '' since '' is already an empty string.

ALTER TABLE `mytable` ADD COLUMN `newcolumn` INT DEFAULT 0

Comments

0

Just tested this and works.

ALTER TABLE  `__test` ADD  `test_column` INT NULL

3 Comments

OP said: 'neither "0" nor "null".'
I think i misunderstood the "null". Does it mean echo $column (suppose it retrieves the null field) gives n-u-l-l word or the null constant?
NULL means nothing. It's not an empty string, it's less than that. It's the lack of an assigned value. The equivalent in PHP would be a variable that isn't set.
0

I try this code:

create table test( a int )

insert into test(a) values (1),(2),(3)

ALTER TABLE test ADD b INT NOT NULL DEFAULT 0

Comments

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.