Mysql - 归档数据库和维护参考
我有一个表,其中保存杂货信息,例如:
id
typeid
groceryname
grocerydesc
每天晚上我都会将该表复制到一个新表中以对其进行存档。 typeid 字段引用保存杂货类型选项的第二个表:
id
typename
然后在杂货表中,我引用所选类型的 ID。我的问题是,一旦归档,如果杂货类型表中的类型名称被更新,会发生什么?例如,假设杂货类型表中有一个苏打水条目,然后将其存档。现在,该人将苏打水更改为水,现在档案中的所有条目都引用了水,这是不正确的。处理这个问题的最佳方法是什么?我是否应该获取类型名称并将其与杂货条目一起存档,而不是类型 ID?但是我通过文本而不是数值查询存档表,这样不是更慢吗?或者我是否只是告诉他们如果您更改它,它也会更新存档表中的列表?
I have a table that holds grocery information such as:
id
typeid
groceryname
grocerydesc
Each night I copy that table into a new table to archive it. The typeid field references to a second table that holds the grocery type options:
id
typename
Then in the grocery table I reference the id for the type selected. My question is once its archived what happens if the typename is updated in the grocery type table? For example lets say there is an entry for soda in the grocery type table, then its archived. Now the person changed Soda to Water, now all the entries in archive reference to water which is incorrect. What is the best way to handle this? Do I instead grab the type name and archive that along with the grocery entry instead of the type id? But then Im querying the archive table via text rather than a numeric value, wouldnt that be slower? Or do I just tell them if you change it, it will update the listings in the archive table as well?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这取决于您如何在表中设置
ON UPDATE
子句以及您的表使用哪种引擎。如果您设置 ON UPDATE CASCADE 并且您的表是 innodb,例如,如果您更新主表中的值,则引用表中的值也会更新。您可以在此处阅读有关此类行为的更多信息: http://dev.mysql.com/doc/refman/5.5/en/innodb-foreign-key-constraints.html
It depends on how you set the
ON UPDATE
clausole in your tables and what kind of engine have your tables. If you set the ON UPDATE CASCADE and your tables are innodb, for example, if you update the value in the main table then the value is update in the referenced table too.You can read more on this type of behavior here: http://dev.mysql.com/doc/refman/5.5/en/innodb-foreign-key-constraints.html