【问题标题】:Updating Foreign Key Tables更新外键表
【发布时间】:2013-10-23 08:11:57
【问题描述】:

我有一段时间没有使用 SQL,而且我似乎不记得更新外键表是如何工作的。您是否必须将元素插入两个表中?如果我这样做,我将如何获得正确的主键?我似乎不记得了,我尝试查找一些基本教程。教程主要是外键实际上没有将数据插入表中。

例如,如果我有:

Table File
fileId - Primary Key
fileName - Normal column.
fileUrl - Normal column.

Table fileWord
fileId, wordId - Primary Key, Foreign Key
count - Normal column.

Table word
wordId - Primary Key
word - Normal Column

我是先插入word,再插入文件,再插入fileWord?

【问题讨论】:

    标签: mysql sql


    【解决方案1】:

    word,fileword,然后是file。

    table word has no foreign keys - safe to insert.(first/second)
    table file has no foreign keys, safe to insert. (first/second)
    table fileword has one foreign key referring to word - safe to insert after table word. (third)
    

    【讨论】:

    • 对不起,我搞砸了。我修复了我有外键的地方。你觉得你可以再看一遍吗。我只会插入到文件中,然后单词先对吗?
    • 是的,没错。文件,单词或单词,文件后跟文件字。
    • 我唯一的另一个问题是,如果 word 和 file 中的这些 id 自动递增,你如何插入到 fileword 中?
    • 从文件和单词中检索您最后插入的 id,然后放入文件中。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-06-22
    • 1970-01-01
    • 1970-01-01
    • 2015-03-05
    • 2010-12-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多