Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

mysql

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 ,
`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`
like image 338
andyg Avatar asked Jun 06 '11 05:06

andyg


People also ask

How do I Fix an unknown column error in SQL?

To fix the error in your SELECT statement, you need to make sure that the column (s) you specified in your SQL statement actually exists in your database table. Because the error above says that user_name column is unknown, let’s check the users table and see if the column exists or not.

What is the MySQL alter column query?

The MySQL ALTER COLUMN query is a MySQL statement that is responsible to change the structure of a table, either for adding a table column, altering the column, renaming the column, removing the column or renaming the table itself.

How to fix MySQL error 1054-unknown column error?

Why the #1054 - Unknown column error occurs in MySQL and how to fix it? Let’s see when the #1054 error occurs in MySQL. While inserting a varchar value, if you will forget to add single quotes, then this error will arise. You need to use single quotes around the string value to fix this error as shown below −

What happens if column name is wrong in MySQL?

When you specify column names in an INSERT statement, then the error can be triggered on an INSERT statement because of a wrong column name, just like in the SELECT statement. First, you need to check that you have the right column names in your statement.


2 Answers

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`;
like image 90
ratibus Avatar answered Sep 27 '22 19:09

ratibus


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

I submitted this question as an example.

like image 20
phazei Avatar answered Sep 27 '22 21:09

phazei