Question.
I have 2 tables.
A loan table that has a loanID and a collateralId in it.
I also have a collateral table with the loanID and CollateralID
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
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
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.
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)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.