I have the following tables: CREATE TABLE group_systems ( group_name, system_name, section_name, created_date, decom_date, status (Active, Deactivate) )
CREATE TABLE systems ( system_name, section_name )
A system is identified by the key (system_name, section_name). There can be dup system names but no dup section name.
In the groups table, I want to enforce the constraint that only one system in a section in a group can be active. However, because the groups table is also a history table, I can't just use the unique constraint (group_name, section_name, system_name). I have to use a check constraint that runs a subquery. There's also some additional constraints that are subqueries.
The problem is that inserting a benchmark of 100k records takes a long time (due to the subqueries).
Is it better to build another table active_systems_for_groups that references back to the group_systems table? That way, I can add the unique constraint to active_systems_for_groups that enforces only one active system per section per group and keep building complex constraints by adding more tables.
Is there a better way to handle complex check constraints?
CHECKconstraints that support subqueries?! The only one I know of is Access (ACE, Jet, whatever) and I know SQL Server can 'fake it' using a UDF. If it is anything else, I'd appreciate learning which SQL it is. Firebird (as suggested by your handle) doesn't AFAIK.