Why can't we use column name “desc” in MySQL?

MySQLMySQLi Database

The desc is a MySQL reserved word, therefore you cannot use it. But, if you still want to set the column name as ‘desc’, you need to use backticks. The backtick notation is (` `).

To understand the above concept, let us create a table. 

The query to create a table is as follows −

mysql> create table useOfDescColumnNameDemo
   -> (
   -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,
   -> Name varchar(20),
   -> `desc` varchar(200) default 'GOOD'
   -> );
Query OK, 0 rows affected (0.60 sec)

Insert some records in the table using insert command. 

The query is as follows −

mysql> insert into useOfDescColumnNameDemo(Name) values('John');
Query OK, 1 row affected (0.17 sec)
mysql> insert into useOfDescColumnNameDemo(Name) values('Bob');
Query OK, 1 row affected (0.16 sec)
mysql> insert into useOfDescColumnNameDemo(Name) values('Carol');
Query OK, 1 row affected (0.13 sec)
mysql> insert into useOfDescColumnNameDemo(Name) values('Sam');
Query OK, 1 row affected (0.13 sec)

Display all records from the table using a select statement.

 The query is as follows −

mysql> select *from useOfDescColumnNameDemo;

Here is the output −

+----+-------+------+
| Id | Name  | desc |
+----+-------+------+
| 1  | John  | GOOD |
| 2  | Bob   | GOOD |
| 3  | Carol | GOOD |
| 4  | Sam   | GOOD |
+----+-------+------+
4 rows in set (0.00 sec)

Here is the query to use backticks with a select statement −

mysql> select `desc` from useOfDescColumnNameDemo;

The following is The output −

+------+
| desc |
+------+
| GOOD |
| GOOD |
| GOOD |
| GOOD |
+------+
4 rows in set (0.00 sec)
raja
Published on 01-Apr-2019 10:38:07
Advertisements