表中的默认行 - 最佳实践sqlite [英] Default row in table - best practice sqlite

查看:75
本文介绍了表中的默认行 - 最佳实践sqlite的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在此之后发表意见。我有一个SQLite表,其中包含电子邮件帐户详细信息,其中一行将是默认帐户。哪个是标记默认行的更好的设计?



a)有一个单独的表(最多1行)有一个字段,这是默认行的rowid在Accounts表中



b)在Accounts表中有一个'IsDefault'列(可能是索引的),带有一个触发器,如果​​它被设置,它会清除现有默认行中的标志。



是否还取决于Accounts表的预期大小(会很小)?



欢呼,



Mick



我尝试过:



目前使用单独的表方法 - 只是想知道这是否是最佳做法?

解决方案

在我看来,单独的表是更美丽的解决方案。虽然触发器将允许您确保只有一个记录标记为默认,但使用单独的表解决方案,它不会首先发生,并且在将另一个记录声明为新的默认记录时无需更新以前的默认记录。我自己使用它:我有一个表DefaultValue,其中包含一个带有外键的记录,其中几个表必须定义默认值。



PS :此外,默认记录的查询不需要表扫描(或索引),这可能很重要,如果您需要在一个表中定义一个默认记录,而该表记录的记录比当前情况多一些。

Just after opinions here. I have an SQLite table that holds e-mail account details, one row of which will be the default account. Which would be the better design to mark the default row?

a) Have a separate table (max 1 row) with one field, which is the rowid of the default row in the Accounts table
or
b) Have an 'IsDefault' column (probably indexed) in the Accounts table, with a trigger so that if it is set, it clears the flag in the existing default row.

Does is also depend on the expected size of the Accounts table (will be small)?

Cheers,

Mick

What I have tried:

Currently using the separate table method - just wondering if this is best practice or not?

解决方案

In my opinion the separate table is the "more beautiful" solution. While triggers would allow you to ensure that there's only ever one record marked as default, with the separate table solution it can't happen in the first place and there's no need to update the former default record when declaring another one as the new default. I'm using it myself: I have a table "DefaultValue" which holds one record with foreign keys to several other tables for which defaults have to be defined.

PS: Also the query for the default record doesn't require a table scan (or index) that way which might matter if you need to define a default record on a table that holds a few more records than in your current case.


这篇关于表中的默认行 - 最佳实践sqlite的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆