Currently when a new row is inserted into my users table I have 1 row inserted into each of these other tables respectively:
profiles
user_settings
user_activity
Currently I’m doing if from the PHP:
$query = mysql_query('INSERT INTO `users` blah blah...');
if($query) {
mysql_query('INSERT INTO `profiles` blah blah...');
mysql_query('INSERT INTO `user_settings` blah blah...');
mysql_query('INSERT INTO `user_activity` blah blah...');
}
How can this be done via stored procedures? So that this work will be done by the database server and not the PHP code. Also is there any benefits to doing this from stored procedures vs from code?
I think what you are looking for are
triggersnotstored procedures. Withtriggersyou can execute a query after or before an insert/update/delete query.http://dev.mysql.com/doc/refman/5.1/en/create-trigger.html
Advantages: It is stored and invoked directly in the DB and I suppose it is more performant as well.
Example: