我继续得到SQL语法错误,我不知道为什么

时间:2017-11-20 13:52:09

标签: mysql sql foreign-keys syntax-error primary-key

这是我的代码:

CREATE TABLE Doctor (
    ssn CHAR(11) PRIMARY KEY,
    name CHAR(30),
    specialty CHAR(30),
    yearsOfExperience INTEGER
);

CREATE TABLE Pharmacy (
    pharm_id CHAR(11) PRIMARY KEY,
    name CHAR(30),
    address CHAR(30),
    phone INTEGER
);

CREATE TABLE Pharm_co (
    name CHAR(30) PRIMARY KEY,
    phone INTEGER
);

CREATE TABLE Pri_Phy_Patient (
    ssn CHAR(11) PRIMARY KEY,
    name CHAR(20),
    age INTEGER,
    address CHAR(20),
    phy_ssn CHAR(11),
    FOREIGN KEY (phy_ssn) REFERENCES Doctor(ssn)
);

CREATE TABLE Make_Drug (
    trade_name CHAR(20) PRIMARY KEY,
    pharm_co_name CHAR(30) PRIMARY KEY,
    formula VARCHAR(100)
    FOREIGN KEY (pharm_co_name) REFERENCES Pharm_co(name)
);

CREATE TABLE Prescription ( 
    pre_id NUMBER(10) PRIMARY KEY, 
    status CHAR(20), --  canceled/ready/pending/completed 
    drop_off_time TIMESTAMP, 
    pick_up_time TIMESTAMP, 
    ssn CHAR(11),  -- patient’s ssn
    phy_ssn CHAR(11),
    pre_date CHAR(11),
    quantity INTEGER,
    trade_name CHAR(20),
    name CHAR(11),
    pharm_co_name CHAR(30),
    FOREIGN KEY (ssn) REFERENCES Pri_Phy_Patient(ssn),
    FOREIGN KEY (phy_ssn) REFERENCES Doctor(ssn),
    FOREIGN KEY (trade_name) REFERENCES Make_Drug(trade_name),
    FOREIGN KEY (pharm_co_name) REFERENCES Make_Drug(pharm_co_name)
);

CREATE TABLE Contract (
    pharm_id CHAR(11) PRIMARY KEY,
    start_date CHAR(11),
    end_date CHAR(11),
    text VARCHAR(4000),
    supervisor CHAR(20),
    pharm_co_name CHAR(30) PRIMARY KEY,,
    FOREIGN KEY (pharm_id) REFERENCES Pharmacy(pharm_id),
    FOREIGN KEY (pharm_co_name) REFERENCES Pharm_co(name)
);

每当我尝试构建架构时:

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 'FOREIGN KEY (pharm_co_name) REFERENCES Pharm_co(name))' at line 5

我不确定为什么它一直给我语法错误。我很确定这是外键的正确语法。我该怎么做才能解决这个问题?我尝试改变一些东西,但我似乎无法让它发挥作用。

3 个答案:

答案 0 :(得分:1)

在此声明中:

CREATE TABLE Make_Drug (
    trade_name CHAR(20) PRIMARY KEY,
    pharm_co_name CHAR(30) PRIMARY KEY,
    formula VARCHAR(100) <-- missing comma
   FOREIGN KEY (pharm_co_name) REFERENCES Pharm_co(name)
);

您在formula VARCHAR(100)

之后错过了逗号

答案 1 :(得分:0)

你错过了一个逗号。正确的 -

CREATE TABLE Make_Drug (
    trade_name CHAR(20) PRIMARY KEY,
    pharm_co_name CHAR(30) PRIMARY KEY,
    formula VARCHAR(100), <-- add this comma
    FOREIGN KEY (pharm_co_name) REFERENCES Pharm_co(name)
);

答案 2 :(得分:0)

您不能将两列声明为主键,内联。试试这个:

CREATE TABLE Make_Drug (
    trade_name CHAR(20),
    pharm_co_name CHAR(30),
    formula VARCHAR(100),
    PRIMARY KEY (trade_name, pharm_co_name),
    FOREIGN KEY (pharm_co_name) REFERENCES Pharm_co(name)
);

这也修复了丢失的逗号。