【问题标题】:could not be able to build schema in mySQL无法在 mySQL 中构建模式
【发布时间】:2015-10-12 12:32:07
【问题描述】:

我有 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

【问题讨论】:

标签: mysql sqlfiddle


【解决方案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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-04-09
    • 2014-03-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多