I created a small test table with 2 integer columns pkId1
and pkId2
. I set the primary key as a composite that includes both columns, (pkId1
is first, then pkId2
).
Now I inserted some values, in the following order: (1,1)
, (1,2)
, (2,1)
and (2,2)
. I was expecting (2,1)
to fail, but it did not. How can I force it to fail ?
I could check it into my application, but I want to do it in SQL side.
Also, I thought about using a stored procedure, but I would like to know if there a setting to set for the composite key in the designer of the table.
Edit
Order of pkId1
and pkId2
do matter. For example, if (2,1) was already in the table, (1,2) should not be accepted. The reason for this is because the row represents a link between two entities. Thus, it should be read there is a link from 2 to 1 for (2,1) and a link from 1 to 2 for (1,2).
As for same value in both columns (1,1), (2,2), etc., they are accepted, because they are a special case.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…