0

I need to add a primary key on a table "usernames"

I have 3 columns in it :

userid int(10)
username char(20)
user char(50)

and the primary key is set on 'username' field and i used it as a foreign key to link it to another table. Now i need to add primary key on 'userid' field also... so i tried out :

alter table `usernames` drop primary key, add primary key(userid,username);

and i get an error saying

ERROR 1553 (HY000): Cannot drop index 'PRIMARY":needed in a foreign key constraint 

is there any possible way to do this ??

4 Answers 4

5

There is:

  1. Drop the FK constraint
  2. Drop PK Constraint
  3. Create New PK
  4. Add Unique Constraint on the name column
  5. Recreate FK

Raj

1
  • 1
    Best to script out the fk before you drop it, so that it is easier to recreate!
    – HLGEM
    Sep 5, 2013 at 11:05
0

I assume you use MySql (despite you tag your question as Sql Server).

You can decide to:

Disable all check and try to remove primary key but the new must have the same name

or

Drop foreign key constraints referred to your primary key and then remove your primary key and finally re-add foreign keys

0

You have to drop the foreign key in order to modify the primary key

0

As implied by the name, a table can only have at most one PRIMARY key. Whilst it can have other UNIQUE keys, which have a similar effect, they really ought not be used for foreign-key relations.

So, you have two choices:

  • Retain your existing schema (perhaps username is a perfectly good natural key such that you need not bother with a synthetic one, in which case you can consider dropping the userid column altogether); or

  • Making userid your PRIMARY key, in which case username should not be used for foreign key relations. I outline below a method for doing this "offline" (where the database is guaranteed not to be altered by any other process during the transition); should you be working "online", you will need to add further steps to ensure integrity is preserved:

    1. Add a new userid column for the foreign key in all child tables and drop the existing foreign key constraint:

      ALTER TABLE foo
        ADD COLUMN userid INT(10),
        DROP FOREIGN KEY fk_name;
      
    2. Change the primary key in your usernames tables (should you wish for the database to enforce a uniqueness constraint over username, you can define a UNIQUE key instead):

      ALTER TABLE usernames
        DROP PRIMARY KEY,
        ADD  PRIMARY KEY (userid),
        ADD  UNIQUE  KEY (username);  -- optional
      
    3. Update the child tables to contain the relevant userid from the parent:

      UPDATE foo JOIN usernames USING (username)
        SET foo.userid = usernames.userid;
      
    4. Add the new foreign key constraints and drop the old username columns from the child tables:

      ALTER TABLE foo
        ADD FOREIGN KEY (userid) REFERENCES usernames (userid),
        DROP COLUMN username;
      

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.