Rename a CHECK Constraint in SQL Server using T-SQL

You can use the sp_rename system stored procedure to rename a CHECK constraint in SQL Server.

The purpose of this stored procedure is to allow you to rename user-created objects in the current database. So you can also use it to rename other objects such as tables, columns, alias data types, etc.

Syntax

The syntax of sp_rename goes like this:

sp_rename [ @objname = ] 'object_name' , [ @newname = ] 'new_name'   
    [ , [ @objtype = ] 'object_type' ]

So your code could go something like this:

sp_rename 'schema_name.old_constraint_name', 'new_constraint_name';

Note that the Microsoft documentation states that when renaming constraints you must qualify the existing name with the schema name.

The following example demonstrates how to rename a CHECK constraint.

Example 1 – Choose a Constraint to Rename

First, let’s list out the CHECK constraints in the database, so we can decide which one to rename.

SELECT 
  name,
  SCHEMA_NAME(schema_id) AS 'Schema',
  OBJECT_NAME(parent_object_id) AS 'Table',
  definition
FROM sys.check_constraints;

Result:

+-----------------+----------+----------------+----------------------------------------+
| name            | Schema   | Table          | definition                             |
|-----------------+----------+----------------+----------------------------------------|
| chkPrice        | dbo      | ConstraintTest | ([Price]>(0))                          |
| chkValidEndDate | dbo      | ConstraintTest | ([EndDate]>=[StartDate])               |
| chkTeamSize     | dbo      | ConstraintTest | ([TeamSize]>=(5) AND [TeamSize]<=(20)) |
| chkJobTitle     | dbo      | Occupation     | ([JobTitle]<>'Digital Nomad')          |
+-----------------+----------+----------------+----------------------------------------+

Let’s rename the second constraint (chkValidEndDate).

Let’s remove the Valid part so that it becomes just chkEndDate.

Example 2 – Rename the Constraint

Run this to rename the constraint.

sp_rename 'dbo.chkValidEndDate', 'chkEndDate';

So the old (existing) name comes first, followed by the new name.

So now if I select all CHECK constraints again, I get the following result.

SELECT 
  name,
  SCHEMA_NAME(schema_id) AS 'Schema',
  OBJECT_NAME(parent_object_id) AS 'Table',
  definition
FROM sys.check_constraints;

Result:

+-------------+----------+----------------+----------------------------------------+
| name        | Schema   | Table          | definition                             |
|-------------+----------+----------------+----------------------------------------|
| chkPrice    | dbo      | ConstraintTest | ([Price]>(0))                          |
| chkEndDate  | dbo      | ConstraintTest | ([EndDate]>=[StartDate])               |
| chkTeamSize | dbo      | ConstraintTest | ([TeamSize]>=(5) AND [TeamSize]<=(20)) |
| chkJobTitle | dbo      | Occupation     | ([JobTitle]<>'Digital Nomad')          |
+-------------+----------+----------------+----------------------------------------+

The constraint’s name has been changed.

As mentioned, the sp_rename stored procedure isn’t limited to just CHECK constraints. You can use exactly the same syntax to rename a foreign key constraint, a table, column, alias data type, or any other user-defined object.

However, Microsoft recommends against renaming stored procedures, triggers, user-defined functions, or views. In such cases, Microsoft recommends you instead, drop the object and re-create it with the new name.