How to rename primary key constraint in SQL Server

28,335

Sometimes you need to explicitly wrap names in square brackets, like this:

sp_rename @objname = N'[Notes].[PK_dbo.Notes]', @newname = N'PK_Notes'

I think it's because of the dot in PK name.

Also, as you see, PK constraints don't need @objtype = 'OBJECT' to be specified.

Share:
28,335

Related videos on Youtube

pkuderov
Author by

pkuderov

I am a computational cognitive neuroscience / reinforcement learning researcher with a lifetime goal of creating artificial intelligence.

Updated on January 09, 2020

Comments

  • pkuderov
    pkuderov over 4 years

    I have a PK constraint on table Notes named PK_dbo.Notes and want to rename it to PK_Notes using SQL Server DDL, i.e. not by using SSMS rename menu option.

    Mentioned in another question's answers queries don't work for me. That thread's answers are also helpful, but don't work too.