复杂的逻辑约束
我试图在具有以下特征的架构上强制执行数据正确性:
表:
|SAVE_TYPE|PROFILE|USERNAME|DATA|
根据保存类型,将使用配置文件列或用户名列。想象一下,如果想要在配置文件范围内保存数据,那么我将插入以下行:
|SAVE_TYPE|PROFILE|USERNAME|DATA|
|PROFILE |PROF. 1|Mr. X |123 |
该行意味着数据将可用于配置文件 PROF。 1 并且由用户 X 先生插入。另一方面,在这种情况下:
|SAVE_TYPE|PROFILE|USERNAME|DATA|
|USER |NULL |Mr. X |456 |
此行意味着数据仅适用于 X 先生。
我不知道实现约束的最佳方法是什么这种行为,可能是我应该使用另一个表结构(我愿意更改架构)。 现在我拥有的是 SAVE_TYPE、PROFILE 和 USERNAME 列的唯一约束,但这现在是完全正确的。我的模型接受的数据错误:
SAVE_TYPE 上的多个实例 -> USER 为同一用户。
|SAVE_TYPE|PROFILE|USERNAME|DATA|
|USER |PROF. 1|Mr. X |456 |
|USER |PROF. 2|Mr. X |456 |
|USER |PROF. 3|Mr. X |456 |
SAVE_TYPE 上的多个实例 -> PROFILE 为相同的配置文件。
|SAVE_TYPE|PROFILE|USERNAME|DATA|
|PROFILE |PROF. 1|Mr. X |123 |
|PROFILE |PROF. 1|Mr. Y |123 |
|PROFILE |PROF. 1|Mr. Z |123 |
预先感谢,希望我已经把事情说清楚了:)
I'm trying to enforce data correctness on an schema of the following characteristics:
Table:
|SAVE_TYPE|PROFILE|USERNAME|DATA|
Depending on the save type the profile column or the username column will be used. Imagine if a want to save the data at a profile scope then I'll insert the following row:
|SAVE_TYPE|PROFILE|USERNAME|DATA|
|PROFILE |PROF. 1|Mr. X |123 |
This row will mean that the data will be available for the profile PROF. 1 and was inserted by the user Mr. X. On the other hand in this case:
|SAVE_TYPE|PROFILE|USERNAME|DATA|
|USER |NULL |Mr. X |456 |
This row means that the data will be available only for Mr. X.
I don't know what would be the best way to implement the constraint for this behaviour, may be I should be using another table structure (I'm open to change the schema).
For now what I have is a unique constraint of the SAVE_TYPE, PROFILE and USERNAME columns, but this is now completely correct. Wrong data that my model accepts:
Multiple instances on SAVE_TYPE -> USER for the same user.
|SAVE_TYPE|PROFILE|USERNAME|DATA|
|USER |PROF. 1|Mr. X |456 |
|USER |PROF. 2|Mr. X |456 |
|USER |PROF. 3|Mr. X |456 |
Multiple instances on SAVE_TYPE -> PROFILE for the same profile.
|SAVE_TYPE|PROFILE|USERNAME|DATA|
|PROFILE |PROF. 1|Mr. X |123 |
|PROFILE |PROF. 1|Mr. Y |123 |
|PROFILE |PROF. 1|Mr. Z |123 |
Thanks in advance, hope I've made things clear :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
“这一行意味着数据将可用于配置文件 PROF.1,并且由用户 X 先生插入。”
和
“这一行意味着数据仅适用于 X 先生。”
如果您的“行的含义”如此不同,那么这表明您手头的内容实际上应该是两个不同的表。
就关系理论而言:每个相关变量(“表”)都有一个关联的外部谓词(“表中的行实际意味着什么”)。因此,如果您有两个不同的谓词,那么您也应该有两个不同的相关变量。
"This row will mean that the data will be available for the profile PROF. 1 and was inserted by the user Mr. X."
and
"This row means that the data will be available only for Mr. X."
If your "meanings for rows" are SO different, then this is an indication that what you have at hand should really be two distinct tables.
In terms of relational theory : each relvar ("table") has ONE associated external predicate ("what the rows in the table actually mean"). Therefore if you have two distinct predicates, you should also have two distinct relvars.