Hello Guys

I want to insert into two tables with one query in mysql

the first table contains a primary key and other stuff
the second table contains a foreign key and other stuff

I'm wondering how this would happen ?

and if so how would be the query of updating ?

Recommended Answers

All 4 Replies

If I'm not mistaken this will work fine:

INSERT INTO TableOne(Id, Name) VALUES(1, 'Name 01'); INSERT INTO TableTwo(Id, IdFK, Name) Values(1, 1, 'Name 01 01');
commented: thanks man, It worked with me but I want to know how to do it with trigger +0

You can make a trigger on insert in first table to make an insert in the secound one.

how this could happen mr.catalinetu ?
can you give me an example ?
I don't know too much about trigger

here is an example of trigger

DELIMITER $$

USE `db_name`$$

DROP TRIGGER IF EXISTS `insert_data`$$

CREATE
    TRIGGER `insert_data` AFTER INSERT ON table_1 
    FOR EACH ROW BEGIN 
    INSERT INTO table_2 (col_1, col_2) VALUES ('val_1','val_2'); 
END;
$$

DELIMITER ;
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.