MySQL 添加具有默认值的新 SQL 列
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3569347/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Adding a new SQL column with a default value
提问by Matt Elhotiby
回答by Mark Byers
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
在页面下方查找搜索语法:
column_definition clauses use the same syntax for ADD and CHANGE as for CREATE TABLE. See Section 12.1.17, “CREATE TABLE Syntax”.
column_definition 子句对 ADD 和 CHANGE 使用与 CREATE TABLE 相同的语法。请参阅第 12.1.17 节,“创建表语法”。
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.
注意那里的 DEFAULT 一词。
回答by Lekensteyn
Like this?
像这样?
ALTER TABLE `tablename` ADD `new_col_name` INT NOT NULL DEFAULT 0;
回答by Eton B.
Simply add default 0
at the end of your ALTER TABLE <table> ADD COLUMN <column> <type>
statement
只需default 0
在ALTER TABLE <table> ADD COLUMN <column> <type>
语句末尾添加
回答by Jon Black
table users (user_id int unsigned PK, username varchar(32))
表用户(user_id int unsigned PK,username varchar(32))
alter table users add column verified tinyint unsigned default 0
回答by vpgodara
This will work for ENUM type as default value
这将适用于 ENUM 类型作为默认值
ALTER TABLE engagete_st.holidays add column `STATUS` ENUM('A', 'D') default 'A' AFTER `H_TYPE`;
回答by itzmebibin
You can try this,
你可以试试这个
ALTER TABLE table_name ADD column_name INT DEFAULT 0;
回答by sandeep kumar
ALTER TABLE my_table ADD COLUMN new_field TinyInt(1) DEFAULT 0;
回答by Roman Rabinovich
Another useful keyword is FIRST and AFTER if you want to add it in a specific spot in your table.
如果您想将其添加到表格中的特定位置,另一个有用的关键字是 FIRST 和 AFTER。
ALTER TABLE `table1` ADD COLUMN `foo` AFTER `bar` INT DEFAULT 0;
回答by MAnoj Sarnaik
Try This :)
尝试这个 :)
ALTER TABLE TABLE_NAME ADD COLUMN_NAME INT NOT NULL DEFAULT 0;