sql - Constraining number of rows to a range -
if have 2 tables, let's doors , house.
the tables connected through foreign key (doors house). constraints 1 house has 2 - 10 doors.
what best way enforce on data level?
create table houses (id int primary key) create table doors ( id int primary key, house_id int references houses(id) ) create trigger doorscondition on doors instead of insert declare @max int, @min int select @max = max(i), @min = min(i) ( select count(1) ( select house_id doors house_id = (select house_id inserted) union select house_id inserted) subquery group house_id) subquery2 if (@max <= 10 , @min >= 2) insert doors select * inserted else print 'the insert violates business constraint' and corresponding trigger delete. can merge them one, i'm not sure it's worth it, things considered, in simple scenario.
Comments
Post a Comment