Results 1 to 5 of 5
  1. #1
    Join Date
    Jan 2014
    Posts
    2

    Unanswered: Adding column with primary key in existing table

    Hi everyone.....!!!!
    I need your help...., Please help me
    I want to add new primary key into existing table which already has a primary key. But,I do not want to remove the old primary key, since there are many records and the old primary key also have relationship with other table

    When I am using this query:
    alter table hem154
    add indexNO uniqueidentifier default newid()

    alter table hem154
    add CONSTRAINT pk_hem154_indexNo PRIMARY KEY (PK_indexNO)
    go

    Note:
    Hem154 ~ Table name
    indexNo ~ Column Name

    I get this runtime error:
    Msg 1779, Level 16, State 0, Line 1
    Table 'hem154' already has a primary key defined on it.
    Msg 1750, Level 16, State 0, Line 1
    Could not create constraint. See previous errors.


    Please help me, how can I do it???


    Thanks
    Shinta
    Last edited by shin_4r; 01-22-14 at 05:12.

  2. #2
    Join Date
    Jan 2007
    Location
    UK
    Posts
    11,434
    Provided Answers: 10
    A table can only have one primary key.
    A table can only have one clustered index (most often, but not always, the primary key).

    You can create multiple unique keys/indexes on a single table.
    You can create multiple non-clustered keys/indexes on a single table.
    George
    Home | Blog

  3. #3
    Join Date
    Feb 2004
    Location
    In front of the computer
    Posts
    15,579
    Provided Answers: 54
    George is correct, the definitions of PK (Primary Key)] and Clustered Index mean that there are either zero or one of each per table. If you could describe what you are trying to accomplish (the WHY part of your desire), then we can probably suggest an index structure that will satisfy your needs.

    -PatP
    In theory, theory and practice are identical. In practice, theory and practice are unrelated.

  4. #4
    Join Date
    Jan 2014
    Posts
    2
    George & Pat Phelan, thank you for your attention....
    OK! I will describe, I have a Composite Primary Key which is a primary key made from two or more columns (They are have relationship to other Table). in the column which want to added new PK, there are 3 columns as composite PK, and now I want to add new column and also have to set up it as PK joint with composite PK that existing. Can I do it???


    Thanks
    Shinta
    Last edited by shin_4r; 01-21-14 at 22:09.

  5. #5
    Join Date
    Jan 2013
    Posts
    354
    Provided Answers: 1

    Why??

    I have a Composite Primary Key which is a primary key made from two or more columns (They are have relationship to other Table). In the column which I want to add this new PK, there are 3 columns as composite PK, and now I want to add new column and also have to set up it as PK join with the existing composite PK.
    Why? What you showed was not key at all! The proprietary "uniqueidentifier" is not an attribute of the entity in the table and anything else in the schema. How would you validate it? How would you verify it?

    If you were to add the HTM code to a table that is keyed on (longitude, latitude), this could make sense. They both are keys for a location on Earth, but on different scales. You have random garbage.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •