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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T23:22:17+00:00 2026-06-06T23:22:17+00:00

I have a table test with an auto incremented id and an arbitrary number

  • 0

I have a table “test” with an auto incremented id and an arbitrary number of columns.

I want to make a copy of a row in this table with all columns the same except for the id of course.

Is there a way to do this without naming all columns?

I thought INSERT... SELECT... ON DUPLICATE KEY would help me until I realised that it never makes an INSERT ON DUPLICATE, it just updates the existing row.

  • 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-06T23:22:19+00:00Added an answer on June 6, 2026 at 11:22 pm

    Let us say your table has following fields:

    ( pk_id int not null auto_increment primary key,
      col1 int,
      col2 varchar(10)
    )
    

    then, to copy values from one row to the other row with new key value,
    following query may help

    insert into my_table( col1, col2 ) select col1, col2 from my_table where pk_id=?;
    

    This will generate a new value for pk_id field and copy values from col1, and col2 of the selected row.

    You can extend this sample to apply for more fields in the table.

    UPDATE:
    In due respect to the comments from JohnP and Martin –

    We can use temporary table to buffer first from main table and use it to copy to main table again.
    Mere update of pk reference field in temp table will not help as it might already be present in the main table. Instead we can drop the pk field from the temp table and copy all other to the main table.

    With reference to the answer by Tim Ruehsen in the referred posting:

    CREATE TEMPORARY TABLE tmp SELECT * from my_table WHERE ...;
    ALTER TABLE tmp drop pk_id; # drop autoincrement field
    # UPDATE tmp SET ...; # just needed to change other unique keys
    INSERT INTO my_table SELECT 0,tmp.* FROM tmp;
    DROP TEMPORARY TABLE tmp;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this test table with one row entry and 2 indexes, the first
I have this table Test as Id, SomeValue, SomeText contains about a mill records,
I work with Oracle 11g. I have one table: create table test (one number(2),
I have a table which stores test results like this: user | score |
I have table with following structure and test records: +--------+--------+---------+ | vid | number
I have a number of tables that together make up a Test that somebody
I have a table like this: create table `test` ( `id` bigint(20) NOT NULL
So I have a table like this: create table `test` ( `testId` int(11) not
I have table to test score data that I need to pivot and I
I have a table defined by the following SQL: CREATE TABLE test ( id

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.