MySQL 将约束表示为计算列?
Columns: id INT, provice_id INT, is_province BIT
Foreign key: province_id references City (id)
我想表达这样一个概念:当id == Province_id
时,is_province == 1
。我该如何表达这一点?也许有计算列或其他约束?
编辑:好吧,这么多评论,我将解释为什么有 is_province 属性。我知道这是多余的,但该表将容纳大约 8000 个条目,因此空间浪费根本不重要。我添加 is_province 因为(使用 DOctrine2)我可以更轻松地查询省份:
$provinces->findByIsProvince(true)
是的,我将省份和城市混合在一个表中,因为省份实际上是一个城市并共享所有属性它的。
所以我的问题仍然是,如何强制执行 if (id == Province_id) then is_province == 1
的约束?
Columns: id INT, provice_id INT, is_province BIT
Foreign key: province_id references City (id)
I'd like to express the concept: when id == province_id
then is_province == 1
. How can i express this? Maybe with a calculated column or another constraint?
EDIT: ok so many comments, i will explain why there is is_province attribute. I know it's redundant, but the table will hold about 8000 entries so the wast of space is not important at all. I'm adding is_province because (using DOctrine2) i can query for provinces more easly:
$provinces->findByIsProvince(true)
And yes, i'm mixing provinces and cities in one table, because a province is actually a city and share all attributes of it.
So my question is still, how can enforce constraint that if (id == province_id) then is_province == 1
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果额外将其存储在表中,该信息将是多余的。它是视图的完美候选者。
这将为您提供
is_province
的TRUE
/FALSE
。如果您想要 1 / 0,则替换:有关如何创建视图的更多信息,请参见 手动。
The information would be redundant if you stored it in the table additionally. It's a perfect candidate for a view.
That gives you
TRUE
/FALSE
foris_province
. If you want 1 / 0, then substitute:More on how to create views in the manual.
可能你不需要 is_province
您可以通过添加
WHERE id = Province_id
来选择省份may be your dont need is_province
your could just select provinces by add
WHERE id = province_id