MySQL - 无意义语法错误

时间:2018-01-29 22:33:43

标签: mysql database

我是编程新手,无法理解我得到的语法错误。

CREATE SCHEMA IF NOT EXISTS donutsrus;

CREATE TABLE IF NOT EXISTS DONUT (
    Donut_ID            INT             NOT NULL, UNIQUE
    Donut_Name          VARCHAR (20)    NOT NULL, UNIQUE
    Description         VARCHAR (20)    NOT NULL, UNIQUE
    Unit_Price          NUMERIC (4,2)   NOT NULL
    PRIMARY KEY (Donut_ID));

CREATE TABLE IF NOT EXISTS SALES_ORDER (
    Sales_Order_ID      INT             NOT NULL, UNIQUE
    Sales_Order_Date    DATE            NOT NULL
    Sales_Order_Notes   VARCHAR (100)
    Customer_ID         INT             NOT NULL, UNIQUE
    PRIMARY KEY (Sales_Order_ID)
    FOREIGN KEY (Customer_ID));

CREATE TABLE IF NOT EXISTS CUSTOMER (
    Customer_ID         INT             NOT NULL, UNIQUE
    Last_Name           VARCHAR (20)    NOT NULL
    First_Name          VARCHAR (20)    NOT NULL
    Address             VARCHAR (50)    NOT NULL
    Apt                 VARCHAR (5)
    City                VARCHAR (20)    NOT NULL
    State               CHAR (2)        NOT NULL
    Zip                 NUMBER (5)      NOT NULL
    Home_Phone          NUMBER (10)
    Mobile_Phone        NUMBER (10)
    Other_Phone         NUMBER (10)
    PRIMARY KEY (Customer_ID));

CREATE TABLE IF NOT EXISTS ORDER_LINE_ITEM (
    Donut_ID            INT             NOT NULL, UNIQUE
    Sales_Order_ID      INT             NOT NULL, UNIQUE
    Quantity            INT             NOT NULL
    PRIMARY KEY (Donut_ID, Sales_Order_ID)
    FOREIGN KEY (Donut_ID, Sales_Order_ID));

Donut表格上,我Syntax error: extraneous input found - expected 'opening parenthesis'VARCHAR Donut_Name。在接下来的20后的闭幕式上,我得到Syntax error: extraneous input found - expected 'closing parenthesis'

在我Order Line Item的{​​{1}}表上Syntax Error: 'INT' (int) is not valid input at this position之后Sales_Order_ID。我也不明白为什么它在其他代码行上没问题,但不是这个。

1 个答案:

答案 0 :(得分:1)

UNIQUE之前的所有逗号都是错误的。逗号用于分隔列,而不是用于分隔特定列的选项。

CREATE TABLE IF NOT EXISTS DONUT (
    Donut_ID            INT             NOT NULL UNIQUE,
    Donut_Name          VARCHAR (20)    NOT NULL UNIQUE,
    Description         VARCHAR (20)    NOT NULL UNIQUE,
    Unit_Price          NUMERIC (4,2)   NOT NULL,
    PRIMARY KEY (Donut_ID));

CREATE TABLE IF NOT EXISTS CUSTOMER (
    Customer_ID         INT             NOT NULL UNIQUE,
    Last_Name           VARCHAR (20)    NOT NULL,
    First_Name          VARCHAR (20)    NOT NULL,
    Address             VARCHAR (50)    NOT NULL,
    Apt                 VARCHAR (5),
    City                VARCHAR (20)    NOT NULL,
    State               CHAR (2)        NOT NULL,
    Zip                 NUMBER (5)      NOT NULL,
    Home_Phone          NUMBER (10),
    Mobile_Phone        NUMBER (10),
    Other_Phone         NUMBER (10),
    PRIMARY KEY (Customer_ID));

CREATE TABLE IF NOT EXISTS SALES_ORDER (
    Sales_Order_ID      INT             NOT NULL UNIQUE,
    Sales_Order_Date    DATE            NOT NULL,
    Sales_Order_Notes   VARCHAR (100),
    Customer_ID         INT             NOT NULL,
    PRIMARY KEY (Sales_Order_ID),
    FOREIGN KEY (Customer_ID) REFERENCES CUSTOMER (Customer_ID));

CREATE TABLE IF NOT EXISTS ORDER_LINE_ITEM (
    Donut_ID            INT             NOT NULL,
    Sales_Order_ID      INT             NOT NULL,
    Quantity            INT             NOT NULL,
    PRIMARY KEY (Donut_ID, Sales_Order_ID),
    FOREIGN KEY (Donut_ID) REFERENCES DONUT (Donut_ID),
    FOREIGN KEY (Sales_Order_ID) REFERENCES SALES_ORDER (Sales_Order_ID));

顺便说一句,主键不需要UNIQUE选项,它们会自动变为唯一。在ORDER_LINE_ITEM中,如果主键为(Donut_ID, Sales_Order_ID),您可能不希望自己将这些列中的每一列都设置为唯一。这可以防止同一个甜甜圈处于多个订单或具有多个甜甜圈类型的订单。同样,Customer_ID中的SALES_ORDER不应该是唯一的。顾客显然可以有多个订单。

您的FOREIGN KEY行缺少外键的引用。并且需要在引用的表之后创建带有外键的表,因此我将SALES_ORDER向下移动到CUSTOMER之后。