子表上的外键依赖于父字段

发布于 2024-08-31 03:39:28 字数 523 浏览 1 评论 0原文

使用 SQL2005/2008。 假设我有一个 Hotel 表 (HotelID, HotelName) 和一个 Room 表 (RoomID, HotelID, RoomName) Hotel ->; Room 1:M by HotelID RoomID是房间内的PK 现在我有一个合同表(ContractID、HotelID、ContractDescription)和 ContractRate(ContractRateID、ContractID、RoomID、Rate) 已定义所有主键和外键,但是, ¿考虑到 RoomID 只能来自与 Room 中的 HotelID 关联的值,将 ContractRate 中的 RoomID 与 Room 相关联的最佳方法是什么? 目前我在 ContractRate.RoomID 上有一个 FK -> Room.RoomID 但这需要 Room.HotelID 的附加过滤器为 ContractRate -> Contract.HotelID,我可以在应用程序中强制执行,但想知道是否可以在数据库中定义/强制执行(或者是否必须重新定义数据库) 最好的问候, 胡安罗

Using SQL2005/2008.
Let's say I have a Hotel table (HotelID, HotelName) and a Room table (RoomID, HotelID, RoomName) Hotel -> Room 1:M by HotelID RoomID is PK inside Room
Now I have a Contract table (ContractID, HotelID, ContractDescription) and ContractRate (ContractRateID, ContractID, RoomID, Rate)
All primaries and foreign keys defined but,
¿What's the best way to relate RoomID in ContractRate with Room, taking in account that RoomID can only by from the values asociated with HotelID in Room?
Currently I have a FK on ContractRate.RoomID -> Room.RoomID but this needs the additional filter of Room.HotelID being ContractRate -> Contract.HotelID, wich I can enforce at the application but would like to know if can be defined/enforced at the database (or if I have to redefine the DB)
Best regads,
Juanro

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

情泪▽动烟 2024-09-07 03:39:28

我认为你必须将 RoomID 放入合同和 ContractRate 表中以进行规范化。
但在当前情况下,没有人进行规范化,因此按照我的建议将 hotelID 和 RoomID 都放入两个表中。

I think you have to put RoomID in both contract and ContractRate table for normilization.
but in current scenario nobody doing the Normilization so put both hotelID and RoomID in the both table as per my suggestion.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文