需要帮助添加约束来检查 SQL Server 2000 中表之间的值
我有两个表,我想使用数据库对它们实施约束。
表 A 与表 B 具有一对多关系,即表 B 的许多行恰好属于表 A 的一个行。表 B 具有其所属的表 A 主键的列。无论如何,我想向表A和表B添加一列,其中表A记录中的值必须等于相应表B记录中的值。
我想告诉 SQL Server 2000 不允许更新或添加表 B 记录,除非姊妹列中的值相同。我该怎么做?我尝试使用检查约束,但它不喜欢子查询。我必须冒险进入触发器的世界吗?还有别的办法吗?
任何帮助将不胜感激。
谢谢艾萨克
I have two tables and I want to use the database to enforce a constraint against them.
Table A has a one to many relationship with table B, that is many table B rows belong to exactly one table A row. Table B has a column of the Table A primary key that it belongs to. Anyway, I want to add a column to both table A and table B where the value of it in a table A record must equal the value of it in the corresponding table B records.
I want to tell SQL Server 2000 to disallow updates or additions of table B records unless the values in the sister columns are the same. How can I do this? I tried using a check constraint but it doesn't like subqueries. Will I have to venture into the world of triggers? Is there another way?
Any help would be appreciated.
Thanks
Isaac
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您尝试执行的操作听起来像是外键关系 - 但您似乎已经拥有了该关系(表 B 有一列表 A 主键)。
我认为您无法定义这样的约束 - 据我所知,
CHECK
约束不能跨越表。所以最后,您可能需要在两个表上使用触发器来实现这一点。What you're trying to do sounds like a foreign key relationship - but you already seem to have that (Table B has a column of Table A primary key).
I don't think you can define such a constraint -
CHECK
constraints cannot span tables, as far as I know. So in the end, you will probably need to use triggers instead on both tables to achieve this.