Say I have two tables, Parent and Child. Parent has a MaxChildren (int) field and Child has an Enabled (bit) field and a ParentID (int) field linking back to the parent record.
I'd like to have a constraint such that there can't be more than MaxChildren records for each parent where Enabled = 1. This would mean that any attempt to insert or update any record in the Child table will fail if it goes over the applicable MaxChildren value, or any attempt to lower MaxChildren to below the current number of applicable Child records will fail.
I'm using MS SQL Server, but I'm hoping there's a standard SQL way.
This is Standard SQL-92 entry level syntax i.e. uses 'vanilla' syntax such as foreign keys and row level
CHECK
constraints that are widely implemented in SQL products (though notably not mySQL):I suggest you avoid using bit flag columns. Rather, you could have a second table without the restriction on
MaxChildren
then imply the Enabled column based on which table a row appears in. You'd probably want three tables to model this: a supertype table for all children with a subtype tables for Enabled. You could then create aVIEW
toUNION
the two subtypes with an implied Enabled column e.g.