Sql Fiddle Demo Link
You can try adding the following insert statement
INSERT INTO `member_infos` (`ID`, `Username`, `Password`, `EmailAddr`, `Type_ID`, `Salt_ID`) VALUES (2, 'ewsew', 'ew', 'ewq', 2, 2);
at the end of all this code. You will not be allowed this insert because type_id=2 0 entries are allowed for member_ifos . Similary, you will get an error while pasting using type_id=5 after pasting five reocrds.
I added a trigger because you need validation against every new record, not some static value. So this is not the same question when dual might work like some SO questions like MySQL conditional insert because you need new.type_id, which is only available in triggers
Below is the full code
CREATE TABLE IF NOT EXISTS `member_infos` ( `ID` bigint(8) unsigned zerofill NOT NULL AUTO_INCREMENT, `Username` varchar(30) NOT NULL, `Password` char(41) NOT NULL, `EmailAddr` varchar(100) NOT NULL, `Type_ID` int(10) unsigned NOT NULL, `Salt_ID` bigint(8) unsigned zerofill NOT NULL, PRIMARY KEY (`ID`), UNIQUE KEY `Username` (`Username`), KEY `Type_ID` (`Type_ID`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=4 ; -- Triggers `member_infos` DROP TRIGGER IF EXISTS `validate_Member_infos_insert`; DELIMITER // CREATE TRIGGER `validate_Member_infos_insert` BEFORE INSERT ON `member_infos` FOR EACH ROW begin declare lim int;declare cnt int; select member_limit into lim from member_types where id=new.Type_id; select count(*) into cnt from Member_Infos where type_id=new.Type_id; if cnt>=lim then select `Member Limit exeeded for this type` into lim from member_infos; end if; end // DELIMITER ; CREATE TABLE IF NOT EXISTS `member_types` ( `ID` int(10) unsigned NOT NULL AUTO_INCREMENT, `Name` varchar(20) NOT NULL, `Description` varchar(255) NOT NULL, `Member_Limit` tinyint(2) unsigned NOT NULL DEFAULT '0', PRIMARY KEY (`ID`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=6 ; INSERT INTO `member_types` (`ID`, `Name`, `Description`, `Member_Limit`) VALUES (1, 'General Member', 'Description of this membership.', 0), (2, 'Extended Member', 'Description of this membership.', 0), (3, 'Premium Member', 'Description of this membership.', 0), (4, 'General Administrato', 'Description of this membership.', 5), (5, 'Master Administrator', 'Description of this membership.', 1); INSERT INTO `member_infos` (`ID`, `Username`, `Password`, `EmailAddr`, `Type_ID`, `Salt_ID`) VALUES (1, 'ewsew', 'ew', 'ewq', 5, 2);
Note. Some may improve / remove error message #1054 - Unknown column 'Member Limit exeeded for this type' in 'field list' , please. This exception is necessary because it blocks the insertion into the required condition. But the improvement will be appreciated. I could not figure it out.