开发者

MySQL unknown column error when using ALTER, don't understand behaviour

开发者 https://www.devze.com 2023-03-10 20:51 出处:网络
I was wondering if someone could help me. I have a odd behaviour while issueing a ALTER command. The command comes from MySQL Workbench sync and it is failing. I have a table with fields:

I was wondering if someone could help me.

I have a odd behaviour while issueing a ALTER command. The command comes from MySQL Workbench sync and it is failing. I have a table with fields:

`id`  int(11) NOT NULL AUTO_INCREMENT ,
`text`  varchar(255) CHARACTER SET latin1 COLLATE latin1_swedish_ci NOT NULL ,
`updated`  datetime NULL DEFAULT NULL ,
`remote_addr`  varchar(45) CHARACTER SET latin1 COLLATE latin1_swedish_ci NULL DEFAULT NULL ,
开发者_JS百科`http_user_agent`  varchar(255) CHARACTER SET latin1 COLLATE latin1_swedish_ci NULL DEFAULT NULL ,
`user_id`  int(11) NULL DEFAULT NULL ,
`category`  varchar(20) CHARACTER SET latin1 COLLATE latin1_swedish_ci NULL DEFAULT NULL ,
`created`  datetime NULL DEFAULT NULL ,
PRIMARY KEY (`id`)

And I want to issue the ALTER command:

ALTER TABLE `logs`
ADD COLUMN `updated` DATETIME NULL DEFAULT NULL AFTER `created`,
CHANGE COLUMN `created` `created` DATETIME NULL DEFAULT NULL AFTER `category`

I get in response:

Unknown column 'created' in 'logs'

But

ALTER TABLE `logs`
ADD COLUMN `updated` DATETIME NULL DEFAULT NULL AFTER `created`

works by itself, and:

ALTER TABLE `logs`
CHANGE COLUMN `created` `created` DATETIME NULL DEFAULT NULL AFTER `category`

also works by itself.

I don't understand why when both are combined in one query it doesn't work and says that 'created' doesn't exist. I know that it definately exists.

Note that I'm not worried about the change column for 'created', it is generated by MWB when comparing and preparing to sync. But was just wondering why both actions can't be put on one query.

I am using MySQL 5.5.8

Update

I actually can do multiple clauses okay. I have been doing it on other tables just fine.

I forgot to mention this. But when I remove the AFTER part it works.

So this does not work:

ALTER TABLE `logs`
ADD COLUMN `updated` DATETIME NULL DEFAULT NULL AFTER `created`,
CHANGE COLUMN `created` `created` DATETIME NULL DEFAULT NULL AFTER `category`

But this does:

ALTER TABLE `logs`
ADD COLUMN `updated` DATETIME NULL DEFAULT NULL,
CHANGE COLUMN `created` `created` DATETIME NULL DEFAULT NULL AFTER `category`


I had the same problem. I solved it by doing the CHANGE COLUMN (or MODIFY COLUMN) before ADD COLUMN.

In your example that would give the following SQL statement :

ALTER TABLE `logs`
CHANGE COLUMN `created` `created` DATETIME NULL DEFAULT NULL AFTER `category`,
ADD COLUMN `updated` DATETIME NULL DEFAULT NULL AFTER `created`;


This appears to be a bug: http://bugs.mysql.com/bug.php?id=60650

I submitted this question as an example.


From the Documentation

You can issue multiple ADD, ALTER, DROP, and CHANGE clauses in a single ALTER TABLE statement, separated by commas. This is a MySQL extension to standard SQL, which permits only one of each clause per ALTER TABLE statement. For example, to drop multiple columns in a single statement, do this:

ALTER TABLE t2 DROP COLUMN c, DROP COLUMN d;

So you are only allowed to issue

ALTER TABLE t CHANGE ..., CHANGE ...

Not a combination of different modification statements. Don't know, if the 5.5.8 has changed that behaviour, though.

0

精彩评论

暂无评论...
验证码 换一张
取 消