Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6585571
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T16:40:49+00:00 2026-05-25T16:40:49+00:00

I have two MySQL tables with an one-to-many relationship between them. For example: CREATE

  • 0

I have two MySQL tables with an one-to-many relationship between them. For example:

CREATE TABLE test1 (
    pk1 INTEGER AUTO_INCREMENT PRIMARY KEY,
    testvalue1 INTEGER
);

CREATE TABLE test2 (
    pk2 INTEGER AUTO_INCREMENT PRIMARY KEY,
    testvalue2 VARCHAR(50),
    fk2 INTEGER NOT NULL,
    FOREIGN KEY (fk2) REFERENCES test1 (pk1)
);

If I want to insert records in both tables I can first insert a record in the PK table (e.g. INSERT INTO test1 SET testvalue1=100), determine the PK value (e.g. SELECT MAX(pk1) AS LastId FROM test1 or use LAST_INSERT_ID())
and finally use that value to fill the FK column in the second table.

But is it possible to achieve this all in 1 command/query/action? So let’s MySQL fill in the PK- and FK-values using the AUTO INCREMENTs?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T16:40:50+00:00Added an answer on May 25, 2026 at 4:40 pm

    You should use two INSERT commands; or try to use an INSERT-trigger.

    EDIT:

    –An example with trigger:

    CREATE TABLE dept(
      id INT(11) NOT NULL AUTO_INCREMENT,
      dept_name VARCHAR(255) DEFAULT NULL,
      PRIMARY KEY (id)
    )
    ENGINE = INNODB;
    
    CREATE TABLE emp(
      id INT(11) NOT NULL AUTO_INCREMENT,
      emp_name VARCHAR(255) DEFAULT NULL,
      dept_id INT(11) DEFAULT NULL,
      PRIMARY KEY (id),
      CONSTRAINT FK_emp_dept_id FOREIGN KEY (dept_id)
      REFERENCES dept (id) ON DELETE RESTRICT ON UPDATE RESTRICT
    )
    ENGINE = INNODB;
    
    DELIMITER $$
    
    CREATE TRIGGER trigger1
    AFTER INSERT
    ON dept
    FOR EACH ROW
    BEGIN
      INSERT INTO emp VALUES (NULL, 'Someone', NEW.id);
    END
    $$
    
    DELIMITER ;
    
    
    -- Try to add new department.
    INSERT INTO dept VALUES(NULL, 'Sales');
    
    -- Is there new default employee?
    SELECT * FROM emp;
    +----+----------+---------+
    | id | emp_name | dept_id |
    +----+----------+---------+
    |  1 | Someone  |       1 |
    +----+----------+---------+
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have 2 related MySQL tables in a one to many relationship. Customers: cust_id,
I have two tables - 'business' and 'business_contacts'. The business_contact table has a many-to-one
Ok, so I have two tables in MySQL. One table holds customer information, the
I have two tables, in one to many relationship (Quizzes, Comments): a Quiz can
I have two tables: Item and ItemHistory with a one to many relationship. I
So I have two tables, article and comments (which has one-to-many relationship (1 article
I have two mysql tables, one containing details on cars and one containing all
using php and mysql I have two tables, a users table and a profiles
I have 10 tables in my database(MySQL). two of them is given below tbl_state
I have two tables (renamed/refactored for illustrative purposes) with a Many-To-Many relationship in an

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.