In SQL Server (or it could be other SQL language I guess), how do you make sure that a table always verifies some complex constraint.
For instance, imagine a table has only column A and B

The constraint I want to enforce, is that all lines that contain the same value for column A (e.g. 'a'), also have the same value for column B (e.g. 'b'). For instance, this isn't allowed:

| A | B | C |
| --- | --- | --- |
| a1 | b | c1 |
| a1 | c | c2 |
| a2 | d | c3 |

But this is allowed:

| A | B | C |
| --- | --- | --- |
| a1 | b | c1 |
| a1 | b | c2 |
| a2 | d | c3 |

This is an example of constraint, but there could be more complex (i.e. depends on more columns etc). Here I think that this constraint could be expressed in terms of primary key etc, but let's assume it can't.

What is the way to enforce this?

What I can think about is, each time I want to do an insert, I instead go through a stored procedure.
The stored procedure would do an atomic transaction and check if the table would verify the constraint if updated, and "reject" the update if the constraint would be violated, but this seems quite complex.