query not work for single quote apostrophe - mysql

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.

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 insert brackets into a column name of my SQL table?

I am currently trying to insert brackets into the column name of my table. However, that results in an error when I run my script.
The format of my table in my script previously reads:
cursor.execute("CREATE TABLE IF NOT EXISTS table (date date, voltage decimal (2,2))")
I then made changes to this part of the script to add brackets to my table column name. It now reads:
cursor.execute("CREATE TABLE IF NOT EXISTS table (date date, voltage(V) decimal (2,2))")
After adding the brackets i.e. (V), the script fails to run.
The error I get is:
SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '(V) decimal (2,2))' at line 1
How can I add brackets to the column name without obtaining an error?
If you want to use special characters in the name of a database, table, or column, put the name in backticks.
CREATE TABLE IF NOT EXISTS table (
date date,
`voltage(V)` decimal (2,2)
)
You'll also need to use the backticks in all queries that refer to the column, so it will probably annoy all your other programmers.
See When to use single quotes, double quotes, and backticks in MySQL for more information about quoting in MySQL.

MySQL: change column name currently named with a key word

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

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

#1064 - You have an error in your SQL syntax

SQL query i'm running in phpMyAdmin :
INSERT INTO `companies`(`companyId`, `companyName`, `companyImage`)
VALUES ([1],[Example Company],[image.jpg])
phpMyAdmin generates this, i'm just changing the values.
SQL database:
1 companyId int(100)
2 companyName varchar(100)
3 companyImage varchar(100)
Error 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 '[1],[Example Company],[image.jpg])' at line 1
Use quotes, not brackets, around strings. Integers and floats don't require anything but MySQL allows quotes around those as well.
INSERT INTO `companies`(`companyId`, `companyName`, `companyImage`)
VALUES (1, 'Example Company', 'image.jpg')
Use backticks to escape column and table name.
Use quotes to limit strings.
Use nothing to limit numbers.
Don't use brackets at all in MySQL.