- Jul 4, 2001
- 163
- 95
- 101
Using SQL it is easy to create one to one and one to many relationships by structuring your tables correctly. I wanted a one to X relationship where X was a value stored in the database, but I also wanted the database to enforce that relationship.
For example the game I'm working on has items with X number of sockets. I wanted the database to enforce the maximum number of X gems to be able to be socketed in an item. In this case X is [Item].[GemSlotCount]. The only way I was able to accomplish this was by using a check constraint (using SQL Server 2014).
Here's my database diagram:
The check constrain runs on INSERT and UPDATE of the [ItemGem] table.
From what I read online it seems there may be a possibility of skipping the check constraint on multi-row inserts, but in my limited testing I was not able to bypass the check constraint. Another possible way of doing this would be triggers, but from what I've read a check constraint should be used before a trigger, if possible.
I really have no idea about performance of this implementation, nor do I have experience load testing such. I know I could do this via code, but I prefer the database to maintain it's own integrity.
My questions are:
Is there any known problem(s) with doing the constraint in this manner?
Is a better way known?
For example the game I'm working on has items with X number of sockets. I wanted the database to enforce the maximum number of X gems to be able to be socketed in an item. In this case X is [Item].[GemSlotCount]. The only way I was able to accomplish this was by using a check constraint (using SQL Server 2014).
Here's my database diagram:

The check constrain runs on INSERT and UPDATE of the [ItemGem] table.
From what I read online it seems there may be a possibility of skipping the check constraint on multi-row inserts, but in my limited testing I was not able to bypass the check constraint. Another possible way of doing this would be triggers, but from what I've read a check constraint should be used before a trigger, if possible.
I really have no idea about performance of this implementation, nor do I have experience load testing such. I know I could do this via code, but I prefer the database to maintain it's own integrity.
My questions are:
Is there any known problem(s) with doing the constraint in this manner?
Is a better way known?