无法使用两个外键创建连接表sqlite3

时间:2017-03-16 22:13:01

标签: sql sqlite

尝试使用两个外键创建连接表后,出现以下错误。

Error: near "user_id": syntax error

以下是我创建连接表的代码:



sqlite> CREATE TABLE reviews (
   ...> id INTEGER PRIMARY KEY,
   ...> stars INT,
   ...> comment TEXT,
   ...> business_id INT,
   ...> FOREIGN KEY (business_id) REFERENCES businesses(id),
   ...> user_id INT,
   ...> FOREIGN KEY (user_id) REFERENCES users(id)
   ...> );




以下是其他表格:



CREATE TABLE users (
   ...> id INTEGER PRIMARY KEY,
   ...> first_name TEXT,
   ...> last_name TEXT
   ...> );
   
   CREATE TABLE businesses(
   ...> id INTEGER PRIMARY KEY,
   ...> name VARCHAR(250)
   ...> );




1 个答案:

答案 0 :(得分:2)

约束应该在所有列定义之后:

CREATE TABLE reviews (
    id INTEGER PRIMARY KEY,
    stars INT,
    comment TEXT,
    business_id INT,
    user_id INT,
    FOREIGN KEY (business_id) REFERENCES businesses(id),
    FOREIGN KEY (user_id) REFERENCES users(id)
);

Here是一个SQL小提琴。