子表上的外键依赖于父字段
使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为你必须将 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.