[英]Check Constraint for Deletion from another table
Question. 题。
I have 2 tables. 我有2张桌子。
A loan table that has a loanID and a collateralId in it. 包含借贷ID和抵押ID的贷款表。
I also have a collateral table with the loanID and CollateralID 我也有一个抵押表,其中包含loanID和ColateralID
The tables are not normalized, much to many headaches I have had, but that is neither here nor there. 表格没有规范化,这让我头疼很多,但这既不是这里也不是那里。
I want to put a check constraint on the collateral table as right now the table is letting records be deleted when there is still a matching collateralID/LoanID match in the loan table 我想对抵押品表设置检查约束,因为当贷款表中仍存在匹配的collateralID / LoanID匹配项时,该表正在删除记录
So the idea is. 所以这个想法是。
If the user tried to delete a record from the collateral table, I want it to reference the loan table and prevent a deletion if the loanID/CollateralId combination is present 如果用户尝试从抵押表中删除记录,则我希望它引用借贷表并防止在存在loanID / CollateralId组合的情况下进行删除
I know this can be done as I have done other Check constraints, but I am having issues with getting what I need out of the syntax. 我知道可以像执行其他Check约束一样完成此操作,但是我在从语法中获取所需内容时遇到了问题。
Not really finding any good examples for referencing another table on the check constraint. 没有真正找到任何好的例子来引用检查约束上的另一个表。
If this needs to be a trigger, then that would work as well, but looking for the best process. 如果这需要触发,那也可以,但是要寻找最佳过程。
Thanks 谢谢
I think this is what you are looking for - a foreign key on collateral that checks against the loan table: 我认为这就是您要寻找的东西-抵押品上的外键可对照贷款表进行检查:
ALTER TABLE collateral
WITH CHECK ADD CONSTRAINT [FK_collateral_loan] FOREIGN KEY(loanid, collateralid)
REFERENCES loan (loanid, collateralid)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.