用一条语句在 MySQL 中添加多列
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27195977/
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 multiple columns in MySQL with one statement
提问by Jason12
I am trying to add multiple columns to an existing table in phpMyAdmin, but I keep getting the same error:
我正在尝试向 phpMyAdmin 中的现有表添加多列,但我不断收到相同的错误:
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax ...
#1064 - 你的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册以获取正确的语法...
I am writing:
我正在写:
ALTER TABLE `WeatherCenter`
ADD COLUMN
BarometricPressure SMALLINT NOT NULL,
CloudType VARCHAR(70) NOT NULL,
WhenLikelyToRain VARCHAR(30) NOT NULL;
I have referred to past posts on StackOverflow, and I am following the experts' recommendation, so why am I getting an error?
我已经参考了 StackOverflow 上的过去帖子,并且我遵循了专家的建议,那么为什么我会收到错误消息?
回答by ashkufaraz
回答by Darren
You need to specify multiple ADD COLUMN
您需要指定多个 ADD COLUMN
ALTER TABLE `WeatherCenter`
ADD COLUMN BarometricPressure SMALLINT NOT NULL,
ADD COLUMN CloudType VARCHAR(70) NOT NULL,
ADD COLUMN WhenLikelyToRain VARCHAR(30) NOT NULL;
回答by stealth
You can alter a table and add multiple columns in one statement by doing it like this.
您可以通过这样做来更改表并在一个语句中添加多个列。
alter table WeatherCenter add column (BarometricPressure SMALLINT NOT NULL, CloudType VARCHAR(70) NOT NULL, WhenLikelyToRain VARCHAR(30) NOT NULL);
回答by Abhishek
This will help you:
这将帮助您:
alter table A add first_name varchar(10),last_name varchar(10);
回答by ravi teja
alter table table_name add (product varchar(20) not null, price int(10))
alter table table_name add (product varchar(20) not null, price int(10))
this is also working fine
这也工作正常
回答by Damian Jauregui
As you're adding columns to an existing table I don't think you're meant to declare NOT NULL in the statement. Also, you don't need to use ADD COLUMN, you can just use ADD.
当您向现有表添加列时,我认为您不应该在语句中声明 NOT NULL。此外,您不需要使用ADD COLUMN,您可以只使用ADD。
ALTER TABLE WeatherCentre
ADD BarometricPressure SMALLINT,
ADD CloudType VARCHAR(70),
ADD WhenLikelyToRain VARCHAR(30);
回答by MontyPython
This is from Official MySQL Documentation
这是来自官方 MySQL 文档
ALTER TABLE tbl_name
[alter_specification [, alter_specification] ...]
[partition_options]
alter_specification:
table_options
| ADD [COLUMN] col_name column_definition
[FIRST | AFTER col_name]
| ADD [COLUMN] (col_name column_definition,...)
Possible duplicate of alter table add MULTIPLE columns AFTER column1
更改表的可能重复项在column1 之后添加多个列