Rename a constraint in SQL Server?

前端 未结 5 593
迷失自我
迷失自我 2020-12-14 14:33

Is it possible to rename a constraint in SQL Server? I don\'t want to have to delete and create a new one because this constraint affects other already existing constraints

相关标签:
5条回答
  • 2020-12-14 14:46

    I know this is an old question, but I just found the following to be very helpful, in addition to the other great answers:

    If the constraint to be renamed has a period in it (dot), then you need to enclose it in square brackets, like so:

    sp_rename 'schema.[Name.With.Period.In.It]', 'New.Name.With.Period.In.It'
    
    0 讨论(0)
  • 2020-12-14 14:48

    After some more digging, I found that it actually has to be in this form:

    EXEC sp_rename N'schema.MyIOldConstraint', N'MyNewConstraint', N'OBJECT'
    

    Source

    0 讨论(0)
  • 2020-12-14 14:50

    answer is true :

    exec sp_rename 
    @objname = 'Old_Constraint',
    @newname = 'New_Constraint',
    @objtype = 'object'
    
    0 讨论(0)
  • 2020-12-14 14:52

    You can rename using sp_rename using @objtype = 'OBJECT'

    This works on objects listed in sys.objects which includes constraints

    0 讨论(0)
  • 2020-12-14 14:56

    You can use sp_rename.

    sp_rename 'CK_Ax', 'CK_Ax1'
    
    0 讨论(0)
提交回复
热议问题