无法在mySQL中构建架构

时间:2015-10-12 12:32:07

标签: mysql sqlfiddle

我有mysql架构

 CREATE TABLE foo(_id INTEGER PRIMARY KEY, name TEXT);

insert into foo (_id, name) values (1, "Foo A");
insert into foo (_id, name) values (2, "Foo B");

CREATE TABLE bar (_id INTEGER PRIMARY KEY, title TEXT);

insert into bar (_id, title) values (1, "Bar A");
insert into bar (_id, title) values (2, "Bar B");

CREATE TABLE bridge
(
    foo_id INT NOT NULL,  
    bar_id INT NOT NULL,  
    FOREIGN KEY (foo_id) REFERENCES foo(_id) ON UPDATE CASCADE,  
    FOREIGN KEY (bar_id) REFERENCES bar(_id) ON UPDATE CASCADE
)  

在此http://www.sqlfiddle.com/#!9/b0e77f,效果很好但是当我尝试在桥中插入数据时

INSERT INTO bridge (foo_id, bar_id) values (1, 1);
INSERT INTO bridge (foo_id, bar_id) values (2, 1);

它说

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 'INSERT INTO bridge (job_id, cate_id) values (1, 1)' at line 9

1 个答案:

答案 0 :(得分:1)

您需要在CREATE TABLE bridge(...) ;之后添加分号:

CREATE TABLE foo(_id INTEGER PRIMARY KEY, name TEXT);

insert into foo (_id, name) values (1, "Foo A");
insert into foo (_id, name) values (2, "Foo B");

CREATE TABLE bar (_id INTEGER PRIMARY KEY, title TEXT);

insert into bar (_id, title) values (1, "Bar A");
insert into bar (_id, title) values (2, "Bar B");

CREATE TABLE bridge
(
    foo_id INT NOT NULL,  
    bar_id INT NOT NULL,  
    FOREIGN KEY (foo_id) REFERENCES foo(_id) ON UPDATE CASCADE,  
    FOREIGN KEY (bar_id) REFERENCES bar(_id) ON UPDATE CASCADE
) ;  -- HERE

INSERT INTO bridge (foo_id, bar_id) values (1, 1);
INSERT INTO bridge (foo_id, bar_id) values (2, 1);

SQLFiddle中,每个语句都应以终结符(;)结尾。我的another answer

中的更多信息