开发者

Duplicate rows across several tables on insert in MySQL

开发者 https://www.devze.com 2023-01-26 11:58 出处:网络
currently I\'m facing a rather strange requirement, but nevertheless, it has to be solved! We have several tables, with the following names:

currently I'm facing a rather strange requirement, but nevertheless, it has to be solved!

We have several tables, with the following names:

file_1, file_2, file_3.

If a user inserts a row into file_1, it has to be duplicated in file_2 & file_3. If a user inserts a row into file_2, the same has to happen on file_1 & file_3. I already tried to create a trigger like that:

CREATE TRIGGER duplicatepictures BEFORE INSERT ON wp_ngg_pictures
  FOR EACH ROW
    INSERT INTO wp_2_ngg_pictures(post_id开发者_如何学Python, galleryid, filename, description, alttext, imagedate, exclude, sortorder, meta_data) 
    VALUES (NEW.post_id, NEW.galleryid, NEW.filename, NEW.description, NEW.alttext, NEW.imagedate, NEW.exclude, NEW.sortorder, NEW.meta_data);

But this doesn't work out, as no row is inserted, if this trigger is added to more than one table.

The whole thing has to be solved in SQL, as the program which enters the information into the databse, can't be changed. Any suggestions?

UPDATE:

I forgot to mention, after the rows a inserted, they must be editable indepent from each other.


You could solve it with views (MySQL 5 required).

Create one table that holds all files (e.g. file_all) and insert all existing records.

Then drop file_1, file_2 and file_3:

drop table file_1;
drop table file_2;
drop table file_3;

Then create 3 views:

create view file_1 as select * from file_all;
create view file_2 as select * from file_all;
create view file_3 as select * from file_all;

See the documentation on updatable views


Can file_2 and file_3 be made aliases of file_1?

0

精彩评论

暂无评论...
验证码 换一张
取 消