此查詢中的 MySQL 語法錯誤是什麼 – 建立帶有保留關鍵字的表?
假設我們嘗試建立一個名為“groups”的表,它在 MySQL 中是保留關鍵字,不能使用“groups”,因為 groups 是 MySQL 中的保留關鍵字。
使用命名為“groups”的表建立時發生以下錯誤 −
mysql> create table groups −> ( −> id int, −> name varchar(40) −> ); 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 'groups ( id int, name varchar(40) )' at line 1
為了建立帶有保留關鍵字的表,你需要使用反引號(``)的概念。
讓我們建立一個表 −
mysql> create table `groups` -> ( −> id int, −> name varchar(40) −> ) −> ; Query OK, 0 rows affected (3.08 sec)
在表中插入一些記錄,使用插入命令 −
mysql> insert into `groups` values(10,'John'); Query OK, 1 row affected (0.30 sec) mysql> insert into `groups` values(11,'Bob'); Query OK, 1 row affected (0.32 sec) mysql> insert into `groups` values(12,'Mike'); Query OK, 1 row affected (0.40 sec)
使用 select 語句顯示錶中的記錄
mysql> select *from `groups`;
這將產生以下輸出 −
+------+------+ | id | name | +------+------+ | 10 | John | | 11 | Bob | | 12 | Mike | +------+------+ 3 rows in set (0.04 sec)
廣告