I am looking for the syntax to add a column to a MYSQL database with a default of 0 and for the life of me i have no idea how to read this
A:
table users (user_id int unsigned PK, username varchar(32))
alter table users add column verified tinyint unsigned default 0
f00
2010-08-25 19:10:12
+3
A:
Try this:
ALTER TABLE table1 ADD COLUMN foo INT DEFAULT 0;
From the documentation that you linked to:
ALTER [ONLINE | OFFLINE] [IGNORE] TABLE tbl_name
alter_specification [, alter_specification] ...
alter_specification:
...
ADD [COLUMN] (col_name column_definition,...)
...
To find the syntax for column_definition
search a bit further down the page:
column_definition clauses use the same syntax for ADD and CHANGE as for CREATE TABLE. See Section 12.1.17, “CREATE TABLE Syntax”.
And from the linked page:
column_definition:
data_type [NOT NULL | NULL] [DEFAULT default_value]
[AUTO_INCREMENT] [UNIQUE [KEY] | [PRIMARY] KEY]
[COMMENT 'string']
[COLUMN_FORMAT {FIXED|DYNAMIC|DEFAULT}]
[STORAGE {DISK|MEMORY|DEFAULT}]
[reference_definition]
Notice the word DEFAULT there.
Mark Byers
2010-08-25 19:11:00
A:
Like this?
ALTER TABLE `tablename` ADD `new_col_name` INT NOT NULL DEFAULT 0;
Lekensteyn
2010-08-25 19:11:15
A:
Simply add default 0
at the end of your ALTER TABLE <table> ADD COLUMN <column> <type>
statement
Eton B.
2010-08-25 19:11:28