SQL Server 跨表约束
我有一个 SQL Server 数据库,其中包含一个 Apartment
表(其中包含 FloorNum
列和 BuildingID
列)和一个 ApartmentBuilding
表(带有NumFloors
列)。有没有办法设置约束(使用 SQL Server UI)来检查 Apartment.FloorNum
是否大于 ApartmentBuilding.NumFloors
?
我尝试了这个:
FloorNum > ApartmentBuilding.NumFloors
但现在我意识到我必须以某种方式加入 BuildingID
上的列,但我不知道如何在约束内做到这一点。
感谢您的帮助!
I have a SQL Server database with an Apartment
table (which has columns FloorNum
and BuildingID
) and an ApartmentBuilding
table (with column NumFloors
). Is there any way to set up a constraint (using the SQL Server UI) to check that Apartment.FloorNum
is greater than ApartmentBuilding.NumFloors
?
I tried this:
FloorNum > ApartmentBuilding.NumFloors
but now I realize that I somehow have to join the columns on the BuildingID
, but I have no idea how to do that within a constraint.
Thanks for your help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您不能使用 CHECK CONSTRAINT 来执行此操作,因为它需要来自另一个表的数据。您可以使用 INSERT/UPDATE 触发器。
You can't do this with a CHECK CONSTRAINT since it requires data from another table. You would handle this with an INSERT/UPDATE trigger.