0

Possible Duplicate:
MySQL Insert into multiple tables? (Database normalization?)

I am looking to insert values into two separate tables with a single query using MySQL. Is this possible?

I have tried Google but it is not coming up with anything like what I am looking for. I have a form a user fills out and it needs to put different field values in different tables, I figured it would be more efficient using one query instead of two.

Community
  • 1
  • 1
Yamaha32088
  • 4,125
  • 9
  • 46
  • 97
  • 3
    I'm not sure but i dont think so , take a look at this answer : http://stackoverflow.com/questions/5178697/mysql-insert-into-multiple-tables-database-normalization – MimiEAM Jan 20 '13 at 00:20

1 Answers1

3

Although I don't see how using one query would really be more efficient IMHO you have at least following options:

  1. Use a STORED PROCEDURE
  2. Use an INSERT TRIGGER (eg on a stagging table)
  3. Assuming that you use PHP for server side scripting technically you can use multi_query() but that posses a great security risk and doesn't make inserts atomic.
peterm
  • 91,357
  • 15
  • 148
  • 157
  • +1. I would say a SP is the OP's best choice. [Documentation](http://php.net/manual/en/mysqli.quickstart.multiple-statement.php) on multi-query. – Kermit Jan 20 '13 at 01:01