Can we give underscore in a MySQL table name?


You cannot give underscore in table name. If you still want to create a new table with underscore, surround it using backticks, not single quotes.

However, let us first try to set quotes around a table name with underscore. Following is an example −

mysql> create table 'Demo_Table725'(
   ClientId int NOT NULL AUTO_INCREMENT PRIMARY KEY,
   ClientName varchar(100),
   ClientAge int,
   ClientCountryName varchar(100),
   isMarried boolean
);

This will produce the following output i.e. an error since we haven’t used backtick −

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 ''Demo_Table725'
(
ClientId int NOT NULL AUTO_INCREMENT PRIMARY KEY,
ClientName v' at line 1

Now, we will fix the above and create the same table with backtick −

mysql> create table `Demo_Table725`(
   ClientId int NOT NULL AUTO_INCREMENT PRIMARY KEY,
   ClientName varchar(100),
   ClientAge int,
   ClientCountryName varchar(100),
   isMarried boolean
);
Query OK, 0 rows affected (0.58 sec)

Insert some records in the table using insert command −

mysql> insert into `Demo_Table725`(ClientName,ClientAge,ClientCountryName,isMarried) values('John',34,'US',true);
Query OK, 1 row affected (0.40 sec)
mysql> insert into `Demo_Table725`(ClientName,ClientAge,ClientCountryName,isMarried) values('Chris',28,'UK',false);
Query OK, 1 row affected (0.25 sec)

Display all records from the table using select statement −

mysql> select *from `Demo_Table725`;

This will produce the following output -

+----------+------------+-----------+-------------------+-----------+
| ClientId | ClientName | ClientAge | ClientCountryName | isMarried |
+----------+------------+-----------+-------------------+-----------+
| 1        | John       | 34        | US                | 1         |
| 2        | Chris      | 28        | UK                | 0         |
+----------+------------+-----------+-------------------+-----------+
2 rows in set (0.00 sec)

Updated on: 22-Aug-2019

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements