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 7832425
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T12:13:22+00:00 2026-06-02T12:13:22+00:00

The purpose of this is to copy some rows from one environment to another

  • 0

The purpose of this is to copy some rows from one environment to another without overwriting existing rows.

Sample DB:

INSERT INTO `school` (school_id,name) VALUES (15,'Middle');
INSERT INTO `class` (class_id,school_id,name) VALUES (12,15,'Sample');

The idea is school_id and class_id are auto-increments and class has a Foreign Key link back to school. But I want to dump just these rows and insert them into another database that already has a school_id of 15.

It might be something that could look like:

INSERT INTO `school` (name) VALUES ('Middle');
INSERT INTO `class` (school_id,name) VALUES (LAST_INSERT_ID(),'Sample');

But that would just be for this simple example. Imagine if I had 50 classes, 25 students in each, and a few hundred grades for each student/class combo. You could see how the LAST_INSERT_ID() might not work without storing it in a series of variables.

What would be the proper tool to do this kind of operation? Can mysqldump do anything this smart?

  • 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-06-02T12:13:26+00:00Added an answer on June 2, 2026 at 12:13 pm

    You can do this:

    • Find MAX school_id in the target school table –

      SELECT MAX(school_id) INTO @max_school_id FROM school;

    • Change all school_id values in source tables (school, class) – add MAX school_id from the previous point –

      UPDATE school SET school_id = school_id + @max_school_id + 1;

    It might be very usefull to add ON UPDATE CASCADE action to the foreign key, it will help to change school_id in the child table automatically, e.g. –

    ALTER TABLE class
      DROP FOREIGN KEY FK_name;
    ALTER TABLE class
      ADD CONSTRAINT FK_name FOREIGN KEY (school_id)
        REFERENCES school(school_id) ON UPDATE CASCADE;
    
    • Make dump and import.

    Explanation and example:

    Create source tables:

    CREATE TABLE school(
      school_id INT PRIMARY KEY AUTO_INCREMENT,
      name VARCHAR(20)
    );
    
    INSERT INTO school (school_id, name) VALUES
      (1, 'Middle1'),
      (2, 'Middle2'),
      (3, 'Middle3'),
      (15, 'Middle');
    
    CREATE TABLE class(
      class_id INT(11) NOT NULL,
      school_id INT(11) DEFAULT NULL,
      name VARCHAR(20) DEFAULT NULL,
      PRIMARY KEY (class_id),
      CONSTRAINT FK_class_school_school_id FOREIGN KEY (school_id)
      REFERENCES school (school_id) ON DELETE RESTRICT ON UPDATE CASCADE
    )
    ENGINE = INNODB;
    
    INSERT INTO class (class_id, school_id, name) VALUES (11, 1, 'Sample1');
    INSERT INTO class (class_id, school_id, name) VALUES (12, 15, 'Sample');
    

    Create target tables:

    CREATE TABLE school(
      school_id INT PRIMARY KEY AUTO_INCREMENT,
      name VARCHAR(20)
    );
    
    INSERT INTO school (school_id, name) VALUES
      (1, 'Top'),
      (2, 'Middle'),
      (3, 'Bottom'),
      (15, 'Top');
    
    CREATE TABLE class(
      class_id INT(11) NOT NULL,
      school_id INT(11) DEFAULT NULL,
      name VARCHAR(20) DEFAULT NULL,
      PRIMARY KEY (class_id),
      CONSTRAINT FK_class_school_school_id FOREIGN KEY (school_id)
      REFERENCES school (school_id) ON DELETE RESTRICT ON UPDATE CASCADE
    )
    ENGINE = INNODB;
    
    INSERT INTO class (class_id, school_id, name) VALUES (10, 2, 'Sample2');
    INSERT INTO class (class_id, school_id, name) VALUES (12, 15, 'Sample');
    

    Update source tables, increment id values:
    We should update all unique values, in our case we have to update class_id in the class table and school_id in the school table.

    Find max class_id for the TARGET class table

    SELECT MAX(class_id) + 1000 FROM class; -- This will return => 1012
    

    Increment all SOURCE class_id values class_id + 1012

    UPDATE class SET class_id = class_id + 1012;
    

    Find max school_id for the TARGET school table

    SELECT max(school_id) + 1000 FROM school; -- This will return =>1015
    

    Increment all SOURCE school_id values school_id + 1015

    UPDATE school SET school_id = school_id + 1015;
    

    That is all. We can dump source tables:

    INSERT INTO school VALUES
      (1016, 'Middle1'),
      (1017, 'Middle2'),
      (1018, 'Middle3'),
      (1030, 'Middle');
    
    INSERT INTO class VALUES
      (1023, 1016, 'Sample1'),
      (1024, 1030, 'Sample');
    

    Now we can easily run this script against the target database.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to insert some copy-right information into the beginning of some source
I have requirement to disable copy/paste/cut operations on a textbox. For this purpose I
The purpose of this code is to pull upgrade.zip from a central server, extract
I'm porting some Java code to C#, and I ran into this idiom used
[purpose] This simple command sequence runs expected in the Windows' CMD shell: dir &
What is the purpose this and why this be added when we add new
EDIT: Purpose of this Website: Its called Utopiapimp.com. It is a third party utility
My whole purpose of this is to get something looking like this: I want
What kind of tools or techniques exist for this purpose?
What kind of tools and techniques exist for this purpose?

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.