是否可以在 SQLServer 中重命名约束?我不想删除并创建一个新的约束,因为这个约束会影响其他已经存在的约束,我将不得不重新创建/更改这些约束。
You can rename using sp_rename using @objtype = 'OBJECT'
@objtype = 'OBJECT'
This works on objects listed in sys.objects which includes constraints
You can use sp_rename.
sp_rename 'CK_Ax', 'CK_Ax1'
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
answer is true :
exec sp_rename @objname = 'Old_Constraint', @newname = 'New_Constraint', @objtype = 'object'
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'
What about this?:
ALTER TABLE schema.table_name RENAME CONSTRAINT old_fk1 TO new_fk11;
If you having problem when run query to create table with error message "constraint already exist" like me, may be the cause is also like mine.
When our team updating db structure to production environment, we stuck because one new table cannot be create.
We found out that the table is actually an old table that has been renamed, thus the table already exist in production with the old name.
So your option is to rename the table in production.. Or delete it then create new table.