I have a similar database with two tables as follow:
+----------------------+ +------------------------+
|Persons | |phones |
+----------------------+ +------------------------+
|id int +-----+ |id int |
|name varchar(100) | +---->+person_id int |
|allowed tinyint(1) | |number int |
+----------------------+ +------------------------+
One person could have as many phones as he wants but he has to be allowed to it (allowed > 0).
So, I created both tables using
CREATE TABLE `phones` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`persons_id` int(11) DEFAULT NULL,
`phonenumber` int(5) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `phones_ibfk_1` (`persons_id`),
CONSTRAINT `phones_ibfk_1` FOREIGN KEY (`persons_id`) REFERENCES `persons` (`id`) ON DELETE NO ACTION ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8`
But this doesn't check what I want. What I want to know if it is possible make a query like ALTER TABLE phones ADD CONSTRAINT chk_person CHECK (persons.allowed > 0)
. Is this possible? There's another alternative?