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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T11:48:42+00:00 2026-06-10T11:48:42+00:00

I have the following table in MySQL version 5.5.24 DROP TABLE IF EXISTS `momento_distribution`;

  • 0

I have the following table in MySQL version 5.5.24

DROP TABLE IF EXISTS `momento_distribution`;

CREATE TABLE IF NOT EXISTS `momento_distribution`
  (
     `momento_id`       INT(11) NOT NULL,
     `momento_idmember` INT(11) NOT NULL,
     `created_at`       DATETIME DEFAULT NULL,
     `updated_at`       DATETIME DEFAULT NULL,
     `unread`           TINYINT(1) DEFAULT '1',
     `accepted`         VARCHAR(10) NOT NULL DEFAULT 'pending',
     `ext_member`       VARCHAR(255) DEFAULT NULL,
     PRIMARY KEY (`momento_id`, `momento_idmember`),
     KEY `momento_distribution_FI_2` (`momento_idmember`),
     KEY `accepted` (`accepted`, `ext_member`)
  )
ENGINE=InnoDB
DEFAULT CHARSET=latin1;

It has lots of data with many-to-one relations with two other tables with ondelete=restrict and onupdate=restrict.

Now, I need to change the structure and introduce separate primary key in the table, while still keeping existing relations and data. For that, I executed the following query:

ALTER TABLE  `momento_distribution` ADD  `id` INT( 11 ) NOT NULL FIRST;
ALTER TABLE  `momento_distribution` DROP PRIMARY KEY , ADD PRIMARY KEY (  `id` );

Unfortunately, my second query failed with the following error:

1062 – Duplicate entry ‘0’ for key ‘PRIMARY’

Can someone please point out the issue? I guess that the issue is the existing relation, but I don’t want to lose the existing relation or data, that has several thousand rows. Is there any way to do this without losing data?

EDIT:
By viewing data, I got that the newly created column has the value ‘0’ in it. Probably this is not allowing to change the Primary Key due to duplicate records (in new Primary Key)

I have more than 8,000 rows, so I can’t change it manually. Is there any way to assign rowid to a new Primary Key?

  • 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-10T11:48:43+00:00Added an answer on June 10, 2026 at 11:48 am

    Run the following query in the mysql console:

    SHOW CREATE TABLE momento_distribution
    

    Check for the line that looks something like

    CONSTRAINT `momento_distribution_FK_1` FOREIGN KEY (`momento_id`) REFERENCES `momento` (`id`)
    

    It may be different, I just put a guess as to what it could be. If you have a foreign key on both ‘momento_id’ & ‘momento_idmember’, you will get two foreign key names. The next step is to delete the foreign keys. Run the following queries:

    ALTER TABLE momento_distribution DROP FOREIGN KEY momento_distribution_FK_1
    ALTER TABLE momento_distribution DROP FOREIGN KEY momento_distribution_FK_2
    

    Be sure to change the foreign key name to what you got from the CREATE TABLE query. Now you don’t have any foreign keys so you can easily remove the primary key. Try the following:

    ALTER TABLE  `momento_distribution` DROP PRIMARY KEY
    

    Add the required column as follows:

    ALTER TABLE  `momento_distribution` ADD  `id` INT( 11 ) NOT NULL  PRIMARY KEY AUTO_INCREMENT FIRST
    

    This query also adds numbers so you won’t need to depend on @rowid. Now you need to add the foreign key back to the earlier columns. For that, first make these indexes:

    ALTER TABLE  `momento_distribution` ADD INDEX (  `momento_id` )
    ALTER TABLE  `momento_distribution` ADD INDEX (  `momento_idmember` )
    

    Now add the foreign keys. Change the Reference Table/column as you need:

    ALTER TABLE  `momento_distribution` ADD FOREIGN KEY ( `momento_id`) REFERENCES  `momento` (`id`) ON DELETE RESTRICT ON UPDATE RESTRICT 
    ALTER TABLE  `momento_distribution` ADD FOREIGN KEY ( `momento_idmember`) REFERENCES  `member` (`id`) ON DELETE RESTRICT ON UPDATE RESTRICT 
    

    Hope that helps. If you get any errors, please edit the question with the structure of the reference tables & the error code(s) that you are getting.

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

Sidebar

Related Questions

my MySQL Server (version: 5.0.51a-3ubuntu5.8) I have the following InnoDB table: CREATE TABLE `kontostaende`
I have the following script to create a table in MySQL version 5.1 which
I have the following table in MySQL (version 5): id int(10) UNSIGNED No auto_increment
I have the following table in MYSQL: CREATE TABLE 't001_prospeccao' ( 'pro_id' bigint(20) NOT
I have the following MySQL (version 5.1) table (InnoDB): Username varchar(50) Sequence int(11) FileType
I use the following sql statement in mysql: CREATE TABLE User( userId int NOT
I have the following MySQL table: +---------+------------+------+--------+------+---------+------------+-------+---------+----------+------------+------------+ | Version | Yr_Varient | FY |
CREATE TABLE `pastebin` ( `pid` int(11) NOT NULL auto_increment, `poster` varchar(16) default NULL, `posted`
I am trying to create the following table below, CREATE TABLE IF NOT EXISTS
I have the following table: mysql> SELECT * FROM `bright_promotion_earnings`; +----+----------+------------+----------+-------+ | 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.