Will “create table table” work in MySQL since we cannot use reserved words as table name?

MySQLMySQLi Database

Let us first see a case wherein we use “create table table” while creating a table. An error will arise −

mysql> create table table(
   Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,
   FirstName varchar(100)
);

This will produce the following output i.e. error −

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 'table(
   Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,
   FirstName varchar(100)
)' at line 1

As you can see above, the word “table” is a reserved keyword, and we cannot use it as the name of the table. Therefore, to fix this, you need to wrap the table name using backticks.

Let us create the table again and set the table name as `table` with backticks −

mysql> create table `table`(
   Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,
   FirstName varchar(100)
);
Query OK, 0 rows affected (0.81 sec)

Insert some records in the table using insert command −

mysql> insert into `table`(FirstName) values('Chris');
Query OK, 1 row affected (0.15 sec)
mysql> insert into `table`(FirstName) values('Robert');
Query OK, 1 row affected (0.15 sec)
mysql> insert into `table`(FirstName) values('David');
Query OK, 1 row affected (0.15 sec)
mysql> insert into `table`(FirstName) values('Mike');
Query OK, 1 row affected (0.13 sec)

Display all records from the table using select statement −

mysql> select *from `table`;

This will produce the following output −

+----+-----------+
| Id | FirstName |
+----+-----------+
|  1 | Chris     |
|  2 | Robert    |
|  3 | David     |
|  4 | Mike      |
+----+-----------+
4 rows in set (0.00 sec)
raja
Published on 26-Aug-2019 08:45:55
Advertisements