SQL语法创建表中的错误

时间:2013-10-31 09:04:23

标签: mysql sql

我试图将以下命令提供给MySQL CLI ,这似乎很好,但是当我再添加一个表时,它抱怨语法错误。

以下是命令

CREATE TABLE IF NOT EXISTS User(
        uid INT,
        name VARCHAR(64) UNIQUE,
        birthday date,
        PRIMARY KEY(uid)
) ENGINE = InnoDB ;

CREATE TABLE IF NOT EXISTS UserEmail(
        uid INT,
        email VARCHAR(64),
        PRIMARY KEY(uid, email),
        FOREIGN KEY(uid) REFERENCES User(uid)
);

但是,如果我想再添加一个表,它表示PRIMARY KEY(uid)所在行的')'附近有语法错误。

CREATE TABLE IF NOT EXISTS User(
        uid INT,
        name VARCHAR(64) UNIQUE,
        birthday date,
        PRIMARY KEY(uid)
) ENGINE = InnoDB ;

CREATE TABLE IF NOT EXISTS UserEmail(
        uid INT,
        email VARCHAR(64),
        PRIMARY KEY(uid, email),
        FOREIGN KEY(uid) REFERENCES User(uid)
);

CREATE TABLE friendship(
        invite_uid  INT,
        accept_uid  INT,
        start_date  DATE,
        PRIMARY KEY(invite_uid, accept_uid),
        FOREIGN KEY(invite_uid) REFERENCES User(uid),
        FOREIGN KEY(accept_uid) REFERENCES User(uid),
);  

由于新添加的命令中没有抱怨错误,因此不确定哪里出错了。

- UPDATE -

那是固定的,但还有一个问题。 添加以下表格抛出

无法创建表“estore.contains”(错误号:150)

CREATE TABLE contains(
        uid INT,
        wid INT,
        pid INT,
        PRIMARY KEY(uid, wid, pid),
        FOREIGN KEY(uid) REFERENCES User(uid),
        FOREIGN KEY(wid) REFERENCES Wishlist(wid),          
        FOREIGN KEY(pid) REFERENCES Product(pid)        
);

- 更新2 -

我要添加的完整表格

CREATE TABLE IF NOT EXISTS User(
        uid INT,
        name VARCHAR(64) UNIQUE,
        birthday date,
        PRIMARY KEY(uid)
) ENGINE = InnoDB ;

CREATE TABLE IF NOT EXISTS UserEmail(
        uid INT,
        email VARCHAR(64),
        PRIMARY KEY(uid, email),
        FOREIGN KEY(uid) REFERENCES User(uid)
);

CREATE TABLE friendship(
        invite_uid  INT,
        accept_uid  INT,
        start_date  DATE,
        PRIMARY KEY(invite_uid, accept_uid),
        FOREIGN KEY(invite_uid) REFERENCES User(uid),
        FOREIGN KEY(accept_uid) REFERENCES User(uid)
);  

CREATE TABLE Seller(
        sid INT,
        name VARCHAR(64),
        PRIMARY KEY(sid)
);

CREATE TABLE Product(
        pid INT,
        sid INT,
        name VARCHAR(64),
        description TEXT,
        price DOUBLE,
        PRIMARY KEY(pid),
        FOREIGN KEY(sid) REFERENCES Seller(sid)
);

CREATE TABLE buy(
        uid INT,
        pid INT,
        time DATE,
        PRIMARY KEY(uid, pid),
        FOREIGN KEY(uid) REFERENCES User(uid),
        FOREIGN KEY(pid) REFERENCES Product(pid)
);

CREATE TABLE Wishlist(
        uid INT,
        wid INT,
        start_time DATE,
        end_time DATE,
        PRIMARY KEY(uid,wid),
        FOREIGN KEY(uid) REFERENCES User(uid)
);

CREATE TABLE conntains(
        uid INT,
        wid INT,
        pid INT,
        PRIMARY KEY(uid, wid, pid),
        FOREIGN KEY(uid) REFERENCES User(uid),
        FOREIGN KEY(wid) REFERENCES Wishlist(wid),          
        FOREIGN KEY(pid) REFERENCES Product(pid)        
)ENGINE = InnoDB;

有人可以帮忙吗? THX

6 个答案:

答案 0 :(得分:3)

检查代码的最后一行

FOREIGN KEY(accept_uid) REFERENCES User(uid),

删除末尾的逗号。

答案 1 :(得分:2)

删除,行尾

FOREIGN KEY(accept_uid) REFERENCES User(uid),
--------------------------------------------^

答案 2 :(得分:2)

第三张桌子上有一个额外的逗号。

CREATE TABLE IF NOT EXISTS User(
        uid INT,
        name VARCHAR(64) UNIQUE,
        birthday date,
        PRIMARY KEY(uid)
) ENGINE = InnoDB ;

CREATE TABLE IF NOT EXISTS UserEmail(
        uid INT,
        email VARCHAR(64),
        PRIMARY KEY(uid, email),
        FOREIGN KEY(uid) REFERENCES User(uid)
);

CREATE TABLE friendship(
        invite_uid  INT,
        accept_uid  INT,
        start_date  DATE,
        PRIMARY KEY(invite_uid, accept_uid),
        FOREIGN KEY(invite_uid) REFERENCES User(uid),
        FOREIGN KEY(accept_uid) REFERENCES User(uid), <---- Extra comma
); 

答案 3 :(得分:0)

@Daniel,您不能将名称 CONTAINS 用于表,因为它是一个SQL关键字。请使用其他名称创建表格。

答案 4 :(得分:0)

幸运的是我想出了第二个问题,外键不正确,我需要使用

        FOREIGN KEY(uid, wid) REFERENCES Wishlist(uid, wid),            

引用愿望清单中的主键组合

答案 5 :(得分:0)

FOREIGN KEY(accept_uid) REFERENCES User(uid),<-- Comma should be removed

试试这个:

CREATE TABLE friendship(
        invite_uid  INT,
        accept_uid  INT,
        start_date  DATE,
        PRIMARY KEY(invite_uid, accept_uid),
        FOREIGN KEY(invite_uid) REFERENCES User(uid),
        FOREIGN KEY(accept_uid) REFERENCES User(uid)
);