1

我试图将以下命令提供给 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),
);  

不确定哪里出了问题,因为新添加的命令中没有抱怨错误。

- 更新 -

好吧,这已经解决了,但还有一个问题。添加下表抛出

无法创建表“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;

任何人都可以帮忙吗?谢谢

4

6 回答 6

3

检查代码的最后一行

FOREIGN KEY(accept_uid) REFERENCES User(uid),

去掉最后的逗号。

于 2013-10-31T09:07:28.777 回答
2

删除,行尾

FOREIGN KEY(accept_uid) REFERENCES User(uid),
--------------------------------------------^
于 2013-10-31T09:06:41.493 回答
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
); 
于 2013-10-31T09:08:54.213 回答
0

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

于 2013-10-31T09:26:33.350 回答
0

幸运的是我解决了第二个问题,外键没有正确,我需要使用

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

参考愿望清单中的主键组合

于 2013-10-31T09:50:18.427 回答
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)
);  
于 2013-10-31T09:59:34.783 回答