简体   繁体   English

MySQL Alter Table错误消息

[英]Mysql alter table error message

I am trying to alter MySQL table to add columns to the end of existing columns. 我试图更改MySQL表以将列添加到现有列的末尾。 But for some reasons MySQL does not like the query I am running. 但是由于某些原因,MySQL不喜欢我正在运行的查询。

Here is my query below. 这是我下面的查询。

alter table hdds add 
hdd1 VARCHAR(100), 
hdd2 VARCHAR(100), 
hdd3 VARCHAR(100), 
hdd4 VARCHAR(100), 
hdd5 VARCHAR(50), 
hdd6 VARCHAR(100), 
hdd6 VARCHAR(100), 
hdd7 VARCHAR(100), 
hdd8 VARCHAR(100), 
hdd9 VARCHAR(100), 
hdd10 VARCHAR(100), 
hdd11 VARCHAR(100), 
hdd12 VARCHAR(100), 
hdd13 VARCHAR(100), 
hdd14 VARCHAR(100), 
hdd15 VARCHAR(100), 
hdd16 VARCHAR(100), 
hdd17 VARCHAR(100), 
hdd18 VARCHAR(100), 
hdd19 VARCHAR(100),
after comments;

I am getting following error message. 我收到以下错误消息。

#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'hdd2 VARCHAR(100), 
    hdd3 VARCHAR(100), 
    hdd4 VARCHAR(100), 
    hdd5 ' at line 3 

Don't know where I have gone wrong. 不知道我哪里出了问题。

works like a charm: 奇迹般有效:

create table t111
(   id int auto_increment primary key,
    comments varchar(1000) not null
);

alter table t111
add column hdd19 VARCHAR(100) after comments, 
add column hdd18 VARCHAR(100) after comments, 
add column hdd17 VARCHAR(100) after comments, 
add column hdd16 VARCHAR(100) after comments, 
add column hdd15 VARCHAR(50) after comments, 
add column hdd14 VARCHAR(100) after comments, 
add column hdd13 VARCHAR(100) after comments, 
add column hdd12 VARCHAR(100) after comments, 
add column hdd11 VARCHAR(100) after comments, 
add column hdd10 VARCHAR(100) after comments, 
add column hdd9 VARCHAR(100) after comments, 
add column hdd8 VARCHAR(100) after comments, 
add column hdd7 VARCHAR(100) after comments, 
add column hdd6 VARCHAR(100) after comments, 
add column hdd5 VARCHAR(100) after comments, 
add column hdd4 VARCHAR(100) after comments, 
add column hdd3 VARCHAR(100) after comments, 
add column hdd2 VARCHAR(100) after comments, 
add column hdd1 VARCHAR(100) after comments;

describe t111;

Plus you had a typo, trying to add hdd6 twice. 另外,您输入错误,尝试两次添加hdd6。

Edit: 编辑:

I reversed the order of the columns from 19 to 1 so they all lined up after the comments bubbling them down (as seen in the describe t111; ) 我将列的顺序从19反转为1,因此它们在comments冒泡之后都全部对齐(如describe t111;

As I see it, you have 2 choices to line up the ordering visually after comments from 1 to 19. 正如我所看到的,您有2个选择可以在1到19的comments后直观地排列顺序。

1. You can do it the way I did, with 19 first after comments, then 18 after comments (shoving down 19 below it), then 17 after comments shoving down 18 and 19 ... 1 after comments shoving down 2 thru 19. 1.您可以按照我的方式进行操作,先在评论后添加19个,然后在评论后添加18个(将其下方的19个向下移动),然后在评论后将18和19分别向下移动17个...在评论从2个减少到19个之后,再进行1个操作。

or 要么

2. You can do it 1 thru 19, and have to modify the after comments chunk individually in the `comments part' and it would look like this: create table t111 ( id int auto_increment primary key, comments varchar(1000) not null ); 2.您可以从1到19进行操作,并且必须在“注释”部分中单独修改after comments块,如下所示:创建表t111(id int auto_increment主键,注释varchar(1000)不为null) ;

alter table t11
add column hdd1 VARCHAR(100) after comments, 
add column hdd2 VARCHAR(100) after hdd1, 
add column hdd3 VARCHAR(100) after hdd2,
...
add column hdd19 VARCHAR(100) after hdd18;

I chose choice 1. , maybe not the best and fastest, but it satisfied my thirst for bubble-down curiosity. 我选择了选择1. ,也许不是最好的和最快的选择,但它满足了我对泡沫下降的好奇心的渴望。

And all of this hinges on anyone caring in the least how they look, but in your case it probably should, since you have so many and error-prone. 所有这些都取决于任何人都在乎他们的外表,但是在您的情况下却应该如此,因为您有很多人并且容易出错。

NO, you can't add multiple columns like that in a single ALTER statement. 不,您不能在单个ALTER语句中添加多个列。 That's wrong as it's not same as CREATE statement and so the error you are receiving. 这是错误的,因为它与CREATE语句不同,因此您将收到错误。 You will have to make it separate ALTER statement like 您将不得不将其单独ALTER语句,例如

alter table hdds add hdd1 VARCHAR(100) after comments;
alter table hdds add hdd2 VARCHAR(100) after comments;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM