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

วิธีสร้างตารางชื่อ "order" ใน MySQL (คำสงวน)

order เป็นคำสงวน (reserved keyword) ใน MySQL ไม่สามารถตั้งชื่อตารางได้โดยตรง ต้องใช้แบ็กทิก (`) ครอบชื่อตาราง

ไวยากรณ์:

CREATE TABLE `order` ( yourColumnName1 dataType, yourColumnName2 dataType, ... );

สร้างตาราง:

mysql> create table `order` ( Id int, Price int ); Query OK, 0 rows affected (0.66 sec)

แทรกข้อมูล:

mysql> insert into `order` values(1,200); Query OK, 1 row affected (0.21 sec) mysql> insert into `order` values(2,100); Query OK, 1 row affected (0.17 sec) ... (แทรกต่อจนครบ 10 แถว)

แสดงข้อมูล:

mysql> select * from `order`;

ผลลัพธ์:

+------+-------+ | Id | Price | +------+-------+ | 1 | 200 | | 2 | 100 | | 3 | 300 | | 4 | 1200 | | 5 | 1000 | | 6 | 7000 | | 7 | 900 | | 8 | 10000 | | 9 | 1100 | | 10 | 500 | +------+-------+ 10 rows in set (0.00 sec)

หากไม่ใช้แบ็กทิก จะ error:

mysql> select * from order; ERROR 1064 (42000): You have an error in your SQL syntax... near 'order' at line 1

ดังนั้น ต้องใช้แบ็กทิกครอบชื่อตาราง/คอลัมน์ที่เป็นคำสงวน