Change the name of the constraint

  • Hi everybody..i got a problem

    i add a constraint PK1

    Use test

    GO

    ALTER TABLE tablo1 ADD CONSTRAINT PK1 PRIMARY KEY (a1)

    GO

    how do I

    Change the name of the constraint as PK1 PK2 Table1 a1 area

  • Teh only way is to drop the constraint and rebuild it. Obviously, that becomes a real problem if you have other tables referencing that one through FKs. It's easier to do the change through SSMS because it'll determine the dependencies and do it all for you.

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.
    "Change is inevitable... change for the better is not".

    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)
    Intro to Tally Tables and Functions

  • thanks...i just started to sql

    I do not know how to.

    and

    I tried these codes:

    USE test

    GO

    EXEC sp_rename N'tablo1.PK1', N'PK2', N'a1';

    GO

    but gives an error message

    Msg 15249, Level 11, State 1, Procedure sp_rename, Line 75

    Error: Explicit @objtype 'a1' is unrecognized.

    If you help for a solution I would be grateful

  • i find script 😎

    Use test

    GO

    sp_rename 'PK1', 'PK2', 'OBJECT'

    GO

  • hallo everyone,nice to meet you

  • johnny1walker (3/15/2012)


    i find script 😎

    Use test

    GO

    sp_rename 'PK1', 'PK2', 'OBJECT'

    GO

    Heh... well I'll be. Books Online has yet another documentation error. Under constraints, it specifically said that PK's needed to be dropped and rebuilt.

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.
    "Change is inevitable... change for the better is not".

    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)
    Intro to Tally Tables and Functions

Viewing 6 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic. Login to reply