SQL 创建 table 的语法问题

SQL syntax issues with creating table

我在编写 DDL 脚本来创建 table 时遇到问题。我编写了以下代码来创建 EMPLOYEECLIENT table:

CREATE TABLE EMPLOYEE (
    Employee_id INT NOT NULL AUTO_INCREMENT,
    fname VARCHAR (15) NOT NULL,
    minit VARCHAR (1),
    lname VARCHAR (20) NOT NULL,
    address VARCHAR (55),
    start_date date,
    Job_type VARCHAR (20),
    PRIMARY KEY (Employee_id)
);


CREATE TABLE CLIENT (
    Client_id INT NOT NULL Auto_Increment,
    Rest_name VARCHAR (25) NOT NULL,
    Pri_contact_name VARCHAR (25) NOT NULL,
    Pri_phone CHAR (13) NOT NULL,
    Pri_email VARCHAR (35) NOT NULL,
    Cust_addr VARCHAR (50) NOT NULL,
    PRIMARY KEY (Client_id)
);

这些 table 的创建没有任何问题。但是,当我尝试使用以下代码创建 ORDER table 时:

CREATE TABLE ORDER (
    Order_id INT NOT NULL AUTO_INCREMENT,
    Order_Placed CHAR (10) NOT NULL,
    Delivered_on CHAR (10) NOT NULL,
    Total VARCHAR (8) NOT NULL,
    Ship_addr VARCHAR (50) NOT NULL,
    Order_status VARCHAR (35) NOT NULL,
    PRIMARY KEY (Order_id)
);

我收到此错误:

at line 27: 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 'ORDER ( Order_id int NOT NULL AUTO_INCREMENT,
Order_Placed char(10) NOT NULL' at line 1.

我不确定为什么我在使用之前 table 中有效的相同代码时会收到此错误。

*我暂时忽略了 FK 只是为了找出错误

您需要使用反引号,因为 ORDERreserved keyword:

CREATE TABLE `ORDER` (
    Order_id INT NOT NULL AUTO_INCREMENT,
    Order_Placed CHAR (10) NOT NULL,
    Delivered_on CHAR (10) NOT NULL,
    Total VARCHAR (8) NOT NULL,
    Ship_addr VARCHAR (50) NOT NULL,
    Order_status VARCHAR (35) NOT NULL,
    PRIMARY KEY (Order_id)
);