How to set constraint sql 2 colume, Data is not available elsewhere and must be used only How to set constraint sql 2 colume, Data is not available elsewhere and must be used only.
Example is OK




NAME
REV
ID
PI




A
0
1
PA001


A
1
1
PA001




Example is Error




NAME
REV
ID
PI




A
0
1
PA001


A
1
1
PA002




When ID = 1 then PI is PA001 Only
We can't insert Any data to table except PA001 when ID = 1
The requirement.is unclear. More examples/better specification is needed..
However, i asume that after an ID is &quot; added &quot; to PI, this becomes &quot; paired&quot; and can't be paired with different ID/PI.

create a pairing table (ID,PI) with PK on Id and Unique on PI. Then add FK from your table to this new table either on (PI,ID) or an identity column

There is no simple constraint that would do what you need. What can be used is a CHECK constraint with an function that would do the check by counting ID/PI pairs with same halves. However this is not very optimal when it comes to performance