如何在 MySQL 中使用特殊字元作為列名?


使用反引號包含列名將允許你使用特殊字元。我們首先建立一個表格 −

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)

使用插入命令,向表格中插入一些記錄 −

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)

使用 select 語句,顯示錶格中的所有記錄 −

mysql> select *from DemoTable;

輸出

這將生成以下輸出 −

+------------+--------------+-------------+
| Student-Id | Student-Name | Student-Age |
+------------+--------------+-------------+
|          1 | Chris        | 21          |
|          2 | Mike         | 19          |
|          3 | Bob          | 18          |
+------------+--------------+-------------+
3 rows in set (0.00 sec)

更新於: 30-6 月-2020

2K+ 次瀏覽

開啟你的職業道路

透過完成課程獲得認證

開始
廣告