I have two tables: parent and child. One of the child column references parent PRIMARY KEY with FOREIGN KEY constraint - child.parent_id references parent.parent_id.
Foreign key column in child tables allows NULLs values because when child will be created parent may be not known yet. But when info about parent will be assigned/updated i want to have referential integrity.
And now the question:
How to pass null value from HTML form to MySQL? Quotes '' or "" not working "foreign key constraint fails" probably because it passes empty string instead of null value. Or maybe do I need to do some additional checking in PHP and convert it to null ?
CREATE TABLE IF NOT EXISTS `parent` (
`parent_id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(20) NOT NULL,
`surname` varchar(20) NOT NULL,
PRIMARY KEY (`parent_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;
CREATE TABLE IF NOT EXISTS `child` (
`child_id` int(11) NOT NULL AUTO_INCREMENT,
`child_name` varchar(20) NOT NULL,
`parent_id` int(11) DEFAULT NULL,
PRIMARY KEY (`child_id`),
KEY `fk_parent_id` (`parent_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;
ALTER TABLE `child`
ADD CONSTRAINT `fk_parent_id` FOREIGN KEY (`parent_id`) REFERENCES `parent` (`parent_id`);