tags:

views:

76

answers:

4

is it possible to insert values into different tables using mysql qyery??

A: 

There is no one query that can do that, but assuming you're working with PHP, you can get the primary key of the last inserted row using mysql_insert_id() and then you can construct the second query with that field.

Raveren
+1  A: 

Yes, but you need to create custom functions that insert values to other tables as a side-effect.

The basic idea is that you want to insert the result of the function into the table you are inserting other data

insert into table (resulting_value) values (my_function(parameters))

You can read more about stored functions from the MySQL documentation

Aleksi
How is that a 'yes'? You run an arbitrary piece of code that the query only *triggers*.
Raveren
It's a a yes with quotes? You can use a single query where after the query more than one table is updated and the integrity constraints between these tables are enforced. And I think that was the desired outcome. And using functions this way, the triggers are explicit in the query.
Aleksi
A: 

you could maybe do it using a trigger(s) (i.e. you issue one SQL insert statement and subsequent inserts are carried out in the database), as long as

  1. your subsequent insert values can be derived from the value of the preceding insert
  2. you don't mind living with the problems that such an approach might give rise to (unclear error messages when a trigger fails, silent insert failures, performance issues etc.etc.)
davek
A: 

The Insert command just allows one target table for data to be inserted...

INSERT INTO target_table () VALUES();

A good workaround can be done while using Triggers.

Björn