How to use special characters in column names with MySQL?

MySQLMySQLi Database

Using backticks around the column name will allow you to use special characters. Let us first create a table −

mysql> create table DemoTable
   -> (
   -> `Student-Id` int NOT NULL AUTO_INCREMENT PRIMARY KEY,
   -> `Student-Name` varchar(100),
   -> `Student-Age` int
   -> );
Query OK, 0 rows affected (0.55 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable(`Student-Name`,`Student-Age`) values('Chris',21);
Query OK, 1 row affected (0.18 sec)

mysql> insert into DemoTable(`Student-Name`,`Student-Age`) values('Mike',19);
Query OK, 1 row affected (0.18 sec)

mysql> insert into DemoTable(`Student-Name`,`Student-Age`) values('Bob',18);
Query OK, 1 row affected (0.21 sec)

Display all records from the table using select statement −

mysql> select *from DemoTable;

Output

This will produce the following output −

+------------+--------------+-------------+
| Student-Id | Student-Name | Student-Age |
+------------+--------------+-------------+
|          1 | Chris        | 21          |
|          2 | Mike         | 19          |
|          3 | Bob          | 18          |
+------------+--------------+-------------+
3 rows in set (0.00 sec)
raja
Published on 09-Jul-2019 17:37:37
Advertisements