Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> MySQL

ข้อผิดพลาดเมื่อตั้งชื่อตารางว่า "references" ใน MySQL

ไม่สามารถตั้งชื่อตารางว่า references ได้ เพราะเป็นคำสงวน (reserved keyword) ต้องห่อด้วยแบ็กทิค (backticks) เช่น `references`

สร้างตารางด้วย backticks

mysql> create table `references`(Subject text);
Query OK, 0 rows affected (0.44 sec)

เพิ่มข้อมูลตัวอย่าง

mysql> insert into `references` values('Introduction To MySQL');
Query OK, 1 row affected (0.28 sec)
mysql> insert into `references` values('Introduction To MongoDB');
Query OK, 1 row affected (0.15 sec)
mysql> insert into `references` values('Introduction To Spring and Hibernate');
Query OK, 1 row affected (0.13 sec)
mysql> insert into `references` values('Introduction To Java');
Query OK, 1 row affected (0.18 sec)

แสดงข้อมูลทั้งหมด

mysql> select * from `references`;

ผลลัพธ์

+--------------------------------------+
| Subject                              |
+--------------------------------------+
| Introduction To MySQL                |
| Introduction To MongoDB              |
| Introduction To Spring and Hibernate |
| Introduction To Java                 |
+--------------------------------------+
4 rows in set (0.00 sec)

คำอธิบาย: references เป็นคำสงวนใน MySQL (ใช้ใน FOREIGN KEY ... REFERENCES) ต้องใช้ backticks ทุกครั้งที่อ้างอิง (CREATE, INSERT, SELECT, ALTER, DROP)