简体   繁体   中英

Mysql alter table error message

I am trying to alter MySQL table to add columns to the end of existing columns. But for some reasons MySQL does not like the query I am running.

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.

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; )

As I see it, you have 2 choices to line up the ordering visually after comments from 1 to 19.

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.

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 );

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.

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. That's wrong as it's not same as CREATE statement and so the error you are receiving. You will have to make it separate ALTER statement like

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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