【发布时间】:2013-04-12 05:12:12
【问题描述】:
我目前正在开发一个将语句存储在 2 个表中的数据库,并有另一个表将语句链接在一起。 前 2 个表会在每个条目上自动增加主 ID,但是我需要知道已提供的新 ID 才能将其输入到链接它们的表中。
Table 1
SID | Statement | Language
1 | Have a cup of coffee | English
2 | Have a cup of green tea | English
Table 2
AID | Action
1 | recycle the cup
2 | feel full of cafine
3 | throw away the cup
4 | feel healthy
5 | jump
Table 3 - relationships
SID | AID
1 | 1
1 | 2
1 | 3
2 | 1
2 | 3
2 | 4
所以一个例子是:
INSERT INTO actions(Action) VALUES ('Go to the pub');
INSERT INTO statements(statement, Language) VALUES ('Have a pint', 'English');
关系将是,知道此示例的自动增量值为 3 和 6:
INSERT INTO Relationships(SID,AID) VALUES (3,6);
我需要将值 3 和 6 作为变量插入,例如:
INSERT INTO Relationships(SID,AID) VALUES (id1, id2);
【问题讨论】:
标签: mysql insert foreign-keys primary-key auto-increment