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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T10:19:04+00:00 2026-05-12T10:19:04+00:00

I have two already-existing tables which look (in part) roughly like this: CREATE TABLE

  • 0

I have two already-existing tables which look (in part) roughly like this:

CREATE TABLE parent (
    old_pk CHAR(8) NOT NULL PRIMARY KEY
) ENGINE=InnoDB;

CREATE TABLE child (
    parent_key CHAR(8),
    FOREIGN KEY (parent_key) REFERENCES parent(old_pk)
        ON UPDATE CASCADE ON DELETE CASCADE
) ENGINE=InnoDB;

I want to add a new auto-incrementing integer id column to parent and use it as the primary key instead, while still keeping old_pk as a unique key and allowing other tables like child to reference it in foreign key contraints. Unfortunately, simply saying ALTER TABLE parent DROP PRIMARY KEY doesn’t work:

Error Code : 1025

Error on rename of ‘./data/#sql-4013_70f5e’ to ‘./data/parent’ (errno: 150)

Some googling suggests that this is due to the existing foreign key reference from child. In essence, I need a way to tell MySQL “use this other column as the primary key, but don’t forget the unique-key-ness of the original one”. Is there any way to accomplish this, other than just dropping the key constraints from child and reinstating them afterwards?

Assume that I must alter the tables in place, rather than creating copies with the same data and swapping them in later. I’ve tried using SET FOREIGN_KEY_CHECKS = 0 before altering the table, but it does not seem to help.

  • 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-12T10:19:05+00:00Added an answer on May 12, 2026 at 10:19 am

    Add an index (it could even be UNIQUE) to old_pk before dropping the primary key:

    mysql> CREATE TABLE parent (
        ->     old_pk CHAR(8) NOT NULL PRIMARY KEY
        -> ) ENGINE=InnoDB;
    Query OK, 0 rows affected (0.00 sec)
    
    mysql> CREATE TABLE child (
        ->     parent_key CHAR(8),
        ->     FOREIGN KEY (parent_key) REFERENCES parent(old_pk)
        ->         ON UPDATE CASCADE ON DELETE CASCADE
        -> ) ENGINE=InnoDB;
    Query OK, 0 rows affected (0.00 sec)
    
    mysql> INSERT INTO parent VALUES ('a');
    Query OK, 1 row affected (0.01 sec)
    
    mysql> CREATE INDEX old_pk_unique ON parent (old_pk);
    Query OK, 1 row affected (0.01 sec)
    Records: 1  Duplicates: 0  Warnings: 0
    
    mysql> ALTER TABLE parent DROP PRIMARY KEY;
    Query OK, 1 row affected (0.01 sec)
    Records: 1  Duplicates: 0  Warnings: 0
    
    mysql> INSERT INTO child VALUES ('a');
    Query OK, 1 row affected (0.00 sec)
    
    mysql> SHOW CREATE TABLE parent;
    +--------+------------------------------------------------------------------------------------------------------------------------------+
    | Table  | Create Table                                                                                                                 |
    +--------+------------------------------------------------------------------------------------------------------------------------------+
    | parent | CREATE TABLE `parent` (
      `old_pk` char(8) NOT NULL,
      KEY `old_pk_unique` (`old_pk`)
    ) ENGINE=InnoDB DEFAULT CHARSET=latin1 |
    +--------+------------------------------------------------------------------------------------------------------------------------------+
    1 row in set (0.00 sec)
    
    mysql> INSERT INTO child VALUES ('b');
    ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint fails (`test/child`, CONSTRAINT `child_ibfk_1` FOREIGN KEY (`parent_key`) REFERENCES `parent` (`old_pk`) ON DELETE CASCADE ON UPDATE CASCADE)
    
    mysql> INSERT INTO parent VALUES ('b');
    Query OK, 1 row affected (0.00 sec)
    
    mysql> INSERT INTO child VALUES ('b');
    Query OK, 1 row affected (0.01 sec)
    
    mysql> ALTER TABLE parent ADD id INT;
    Query OK, 2 rows affected (0.00 sec)
    Records: 2  Duplicates: 0  Warnings: 0
    
    mysql> UPDATE parent SET id = 1 WHERE old_pk = 'a';
    Query OK, 1 row affected (0.01 sec)
    Rows matched: 1  Changed: 1  Warnings: 0
    
    mysql> UPDATE parent SET id = 2 WHERE old_pk = 'b';
    Query OK, 1 row affected (0.00 sec)
    Rows matched: 1  Changed: 1  Warnings: 0
    
    mysql> ALTER TABLE parent ADD PRIMARY KEY (id);
    Query OK, 2 rows affected (0.00 sec)
    Records: 2  Duplicates: 0  Warnings: 0
    
    mysql> SHOW CREATE TABLE parent;
    +--------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
    | Table  | Create Table                                                                                                                                                                             |
    +--------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
    | parent | CREATE TABLE `parent` (
      `old_pk` char(8) NOT NULL,
      `id` int(11) NOT NULL default '0',
      PRIMARY KEY  (`id`),
      KEY `old_pk_unique` (`old_pk`)
    ) ENGINE=InnoDB DEFAULT CHARSET=latin1 |
    +--------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
    1 row in set (0.00 sec)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have to insert the output of this code into an already existing table.
I have two fields that I'd like to match. (already done the validation functions
I have a university project which is already 99% completed. It consists of two
The question has two parts, one of which I already have the answer for.
i have two tables, one 'master' and one 'child' table. Each table has a
I have two columns in table users namely registerDate and lastVisitDate which consist of
I have a case where I have a table (t1) which contains items like
This is a sample scenario. I have an existing database consisting of tables as
I need to split an existing table in to two or more tables on
I have a table which contains a two column( start_time and end_time ).I am

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.