Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to rename FK in MS SQL

Tags:

sql-server

Trying to rename foring key in MS SQL using

sp_rename 'FK_Catalog_Equipment__Equiements_Catalog_Client_Clients','FK_Catalog_Equipment__Equipments_Catalog_Client_Clients','OBJECT'

or

sp_rename 'Table_Name.FK_Catalog_Equipment__Equiements_Catalog_Client_Clients','FK_Catalog_Equipment__Equipments_Catalog_Client_Clients','OBJECT'

but both commands result in

Msg 15248, Level 11, State 1, Procedure sp_rename, Line 359
Either the parameter @objname is ambiguous or the claimed @objtype (OBJECT) is wrong.

What I do wrong?

like image 578
Mikhail Sidorov Avatar asked Sep 21 '16 09:09

Mikhail Sidorov


People also ask

Can we change the name of foreign key?

You can change foreign keys for your table or nickname. A foreign key is a column or set of columns in a table or nickname whose values are required to match at least one primary key value of a row of its parent table or nickname.

How do I change foreign key value?

to update an entity attribute Identifier as a foreign key, you have to remove the referential integrity of the First table means which needs to update and also need to delete all the data of this Table. Then it can be done.

How do you rename a constraint in SQL Server?

sp_rename automatically renames the associated index whenever a PRIMARY KEY or UNIQUE constraint is renamed. If a renamed index is tied to a PRIMARY KEY constraint, the PRIMARY KEY constraint is also automatically renamed by sp_rename . sp_rename can be used to rename primary and secondary XML indexes.

How do you rename an object in SQL?

RENAME OBJECT [::] [ [database_name . [ schema_name ] . ] | [ schema_name . ] ] table_name COLUMN column_name TO new_column_name.


1 Answers

Foreign key constraints are individual objects, which is why you can't have a FK with the same name as other objects. Therefore, their name is relevant to the schema.

Copying from sp_rename's documentation example:

sp_rename 'HumanResources.FK_Employee_Person_BusinessEntityID', 'FK_EmployeeID';  

will rename FK_Employee_Person_BusinessEntityID found in the HumanResources schema to FK_EmployeeID

If the schema is missing, SQL Server looks for objects in the user's default schema, which is often the dbo schema. If the FK is created in a different schema, you need to specify it explicitly

like image 88
Panagiotis Kanavos Avatar answered Sep 21 '22 16:09

Panagiotis Kanavos