All we need is an easy explanation of the problem, so here it is.
If I try to create a table called test with one column, it works:
mysql> create table test(surname VARCHAR(300)); Query OK, 0 rows affected (0.02 sec) mysql> drop table test; Query OK, 0 rows affected (0.00 sec)
But if I try now to create the same table, but this time the column is called ‘desc’ instead of ‘surname’, I get an error:
mysql> create table test(desc VARCHAR(300)); ERROR 1064 (42000): 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 'desc VARCHAR(300))' at line 1 mysql>
What am I getting wrong here? Is ‘desc’ a reserved keyword in mysql?
How to solve :
I know you bored from this bug, So we are here to help you! Take a deep breath and look at the explanation of your problem. We have many solutions to this problem, But we recommend you to use the first method because it is tested & true method that will 100% work for you.
"ASC" or "DESC" are the reserved keyword in MySQL. you can’t use it for column name. "ASC/DESC" is the keyword used to sort result sets in either ascending or descending order. "ASC" is used as the default.
As mentioned already, "desc" is a reserved word in MySQL used to dictate sorting in the
ORDER BY clause, so you should avoid naming things with it. But if you really must, then you can use backticks around the name
`desc` as per this StackOverflow answer which has a lot of information on reserved words in MySQL.
Note: Use and implement method 1 because this method fully tested our system.
Thank you 🙂