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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 16:57:57  来源:igfitidea点击:

Adding a new SQL column with a default value

sqlmysql

提问by Matt Elhotiby

I am looking for the syntax to add a column to a MySQL database with a default value of 0

我正在寻找将列添加到 MySQL 数据库的语法,默认值为 0

Reference

参考

回答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_definitionsearch 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 0at the end of your ALTER TABLE <table> ADD COLUMN <column> <type>statement

只需default 0ALTER 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;

回答by Max Toro

If you are learning it's helpful to use a GUI like SQLyog, make the changes using the program and then see the History tab for the DDL statements that made those changes.

如果您正在学习使用像SQLyog这样的 GUI 会有所帮助,请使用该程序进行更改,然后查看进行这些更改的 DDL 语句的历史选项卡。