MySQL: change column name currently named with a key word - mysql

I have a table "stats" in MySQL and one of the columns is named "AS".
Now I want to change its name, and I try to run
ALTER TABLE stats CHANGE COLUMN AS NEW_NAME varchar(5);
The error is
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'AS NEW_NAME varchar(5)'
I tried putting '' or "" around columns names but it didn't work. How can I fix it?

Use backticks:
ALTER TABLE stats CHANGE `COLUMN` AS `NEW_NAME` varchar(5);

You need to use backticks, Using backticks permits you to use alternative characters.
Use below query:
ALTER TABLE stats CHANGE `COLUMN` AS `NEW_NAME` varchar(5);
Backticks are to be used for table and column identifiers, but are only necessary when the identifier is a MySQL reserved keyword, or when the identifier contains whitespace characters or characters beyond a limited set (see below) It is often recommended to avoid using reserved keywords as column or table identifiers when possible, avoiding the quoting issue.
Hope it will help you..

Try the following query:
ALTER TABLE stats CHANGE COLUMN `AS` `NEW_NAME` varchar(5);

Related

mysql syntax error for update rows

update amazon-crawler set `flag_images`= '0' where `id`='966'
i get #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 '-crawler set flag_images= '0' where id='966'' at line 1
why syntax error?
amazon-crawler is the table
flag images and id are columns
My guess is that the hyphen in the table name is causing problems, because it is an arithmetic operator. Try also escaping the table name:
UPDATE `amazon-crawler` SET `flag_images`= '0' WHERE `id` = '966';
Note that you should try to avoid using backticks in your query, unless absolutely needed. Using backticks means that any name would potentially work, even one which happens to be a MySQL reserved keyword. Also, I'm guessing that flag_images and id are numeric columns, in which case you should be comparing them against numbers, not strings. So I would write your update as this:
UPDATE `amazon-crawler` SET flag_images= 0 WHERE id = 966;
Here, only the table name has to appear in backticks.

How do I select a column whose name is a reserved word?

I have a column in MySQL database called "from".
But my query throws an error.
What am I doing wrong?
SELECT name, to, from
FROM table1
Error Code: 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 'from FROM tables' at line 1
Use backticks in order to select columns with reserved words as the column name:
SELECT `name`, `to` ,`from`
FROM table1
From the mysql documentation:
Reserved words are permitted as identifiers if you quote them as
described in Section 9.2, “Schema Object Names"
Table 9.2 provides the list of keywords and reserved words, which includes FROM.
Therefore:
select `from` from tablename
Also from that page:
Exception: A word that follows a period in a qualified name must be an identifier, so it need not be quoted even if it is reserved
so this will work as well:
select tablename.from from tablename

Altering a table column using change

Altering a table column using change is not working but using modify the same query statement works fine.
With change it fails:
alter table users change name varchar(100);
Error Code: 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 'varchar(100)' at line 1 0.000 sec
With Modify it works.
alter table users modify name varchar(100);
If you use CHANGE then you have to specify a new name for the column, so the following should work:
alter table users change name newname varchar(100);
Look at the alter specification for CHANGE: http://dev.mysql.com/doc/refman/5.1/en/alter-table.html
The syntax for CHANGE is different than MODIFY.
From the documentation:
CHANGE [COLUMN] old_col_name new_col_name column_definition
So, in your case, you should use:
alter table users change name name varchar(100);

query not work for single quote apostrophe

phpmyadmin query not work for single quote / apostrophe.
Not Work
ALTER TABLE 'about_team' CHANGE 'position' 'pp' INT( 11 ) NOT NULL
Work:
ALTER TABLE `about_team` CHANGE `position` `pp` INT( 11 ) NOT NULL
Same query but not work, gives error:
#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 ''about_team' CHANGE 'position' 'pp' INT(11) NOT NULL' at line 1
it is because when you using single quote, it just means it is a STRING. Whereas BACTICK (the second query) means escaping a column.
'about_team' is not equal with `about_team`
'about_team' is STRING
`about_team` is a Table Name
Actually backticks enclosing the names are optional since the names used where not on MySQL Reserved Keyword List.
MySQL Reserved Keywords
Usually, single quotes are used around values while backticks are for table names and column names.

How do I remove a table named following a keyword in mysql?

I accidentally named a table 'order'.
Since 'order by' is a keyword phrase, I am unable to delete this table:
> drop table order;
ERROR 1064 (42000): You have an error in your SQL syntax; ...
How do I drop the table ?
Try this:
drop table `order`;
Tested and working on mysql :)
The default identifier quote character is the backtick (“`”):
drop table `order`;
Using backticks around the table name:
drop table `order`;
will work.