1. Get rid of all advertisements and get unlimited access to documents by upgrading to Premium Membership. Upgrade to Premium Now and also get a Premium Badge!

primary key problem

Discussion in 'SQL PL/SQL' started by shymaa, Feb 5, 2009.

  1. shymaa

    shymaa Active Member

    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    55
    hi all, i build a table using sql plus, and i assign one field to be primary key.now i want to modify the primary key to be assigned to another field.how i can do this?
     
  2. tyro

    tyro Forum Genius

    Messages:
    368
    Likes Received:
    20
    Trophy Points:
    260
    Location:
    India
    create a table:
    Code (Text):

    create table testing(
    key1    number,
    key2    number
    )
     
    Add Primary Key to column Key1
    Code (Text):

    ALTER TABLE testing ADD (
      CONSTRAINT XYZ_PK
     PRIMARY KEY
     (KEY1))
     
    Now drop the primary key
    Code (Text):

     ALTER TABLE testing
    drop CONSTRAINT XYZ_PK;
     
    Now change the primary key to column key2
    Code (Text):

    ALTER TABLE testing ADD (
      CONSTRAINT XYZ_PK
     PRIMARY KEY
     (KEY2))
    Hope this helped :)
     
  3. debasisdas

    debasisdas Active Member

    Messages:
    46
    Likes Received:
    3
    Trophy Points:
    90
    Location:
    Bangalore, India
    you need to drop the PrimaryKey and and recreate the same on the desired column.
     
  4. Sadik

    Sadik Community Moderator Forum Guru

    Messages:
    1,906
    Likes Received:
    252
    Trophy Points:
    1,455
    Hi, Debasis, nice to see you here :)

    You might want to reply to this thread: Should there be a separate article section