lihao has asked for the wisdom of the Perl Monks concerning the following question:
CREATE TABLE `table1` ( ...... `property_id` int(8) REFERENCES `table2` (id) ON UPDATE CASCAD +E ON DELETE NO ACTION, ...... ) ENGINE=InnoDB; CREATE TABLE `table2` ( `id` int(8) PRIMARY KEY AUTO_INCREMENT NOT NULL, `property` varchar(50) ) ENGINE=InnoDB;
When updating `table1`, I need to check `table2` and see if the given `property` exists, if it exists, return its `id` to table1.property_id; otherwise, insert it into table2 and return last_insert_id() to table1.property_id. Currently I am using DBIx::Simple, so I do it the following way (sample code):
my ($pid) = $db->query(<<END_SQL, $property)->list; SELECT `id` FROM `table2` WHERE `property` = ? END_SQL if (not $pid) { $db->query("INSERT INTO `table2` (`property`) VALUES (?)", $pr +operty); $pid = $db->last_insert_id(); }
Since there are many places(tables and columns) in my DB that need to be updated under the FK constraints. My question is: Are there simpler ways to handle this situation nicely without using the above code?
many thanks
lihao
|
|---|