Re: inserting same data into multiple tables question (?)

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Hi,

John W. Holmes wrote:
From: "JeRRy" <jusa_98@xxxxxxxxx>

I want to input the same data into multiple tables in
one query if possible.


It's not. You really have to question your database schema if you need to do
this.

Yeah, I agree w/ John -- you probably want to examine your database schema if you think you need to do that.


If you are building a really complex application that really does require a whole bunch of inserts for adding a user, then you should probably consider using TRIGGERS -- and a database that supports them (e.g. PostgreSQL).

http://www.postgresql.org/docs/7.4/interactive/plpgsql-trigger.html

You would do something like (in Postgres):

CREATE FUNCTION do_other_inserts RETURNS trigger AS '
  BEGIN
    INSERT INTO table2 (username) VALUES (NEW.username);
    INSERT INTO table3 (username) VALEUS (NEW.username);
  END;
' LANGUAGE plpgsql;

CREATE TRIGGER other_inserts AFTER INSERT ON table1 FOR EACH ROW EXECUTE PROCEDURE do_other_inserts();

Of course you'll probably want to also add triggers to handle deleting and updating users. The better/faster solution would be to use integer (user_id rather than username) foreign keys and specify ON UPDATE CASCADE and ON DELETE CASCADE so that the updates and deletes would propogate automatically. You'd probably still need an INSERT trigger, though, if you have related rows which must be added.

I'm assuming that since you asked whether you can INSERT into multiple tables, that this is probably sounding a little overwhelming. I encourage you to spend some time to figure this stuff out now, though, because it will only become a nightmare when you actually have production data & you realize that your data model is inefficient & unscalable.

Hans

--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [PHP Users]     [Postgresql Discussion]     [Kernel Newbies]     [Postgresql]     [Yosemite News]

  Powered by Linux