0

I was trying to get the name of the PK and FK. However, this is the output that I get: PK: PK__adm_lib___3214EC067E4F7CCA FK: FK__adm_lib_S__fk_Co__7093AB15

5
  • 1
    This looks like the standard naming convention that a tool like SSMS (SQL Server Management Studio) uses when the keys are generated via the UI. Those are likely the correct names of the keys.
    – J.D.
    Commented Jul 1, 2021 at 2:21
  • Is there any work around in querying this @J.D? I just a newbie in terms of this Commented Jul 1, 2021 at 2:49
  • 1
    I don't understand what you're stuck on? You've correctly queried the names of the keys, there's no other names that exist.
    – J.D.
    Commented Jul 1, 2021 at 2:53
  • I just wondering if I can query these in a more readable way like PK_adm_lib__SampleName not PK__adm_lib___3214EC067E4F7CCA? Commented Jul 1, 2021 at 2:55
  • Are you saying you want to change the names?
    – J.D.
    Commented Jul 1, 2021 at 2:58

1 Answer 1

1

The names you found are the actual names of those keys. This happens when the UI in a tool like SSMS (SQL Server Management Studio) is used to create the keys (as opposed to manually scripting them out and choosing the names). I presume the suffix of the name is a GUID of sorts to ensure uniqueness (since two keys can't have the same name in SQL Server).

If you want the names to be more readable when you query them, your only option is to change their names. You can use the system stored procedure sp_rename to rename your keys to a more readable name of your choosing like this: EXEC sp_rename 'SchemaName.TableName.OldKeyName', 'NewKeyName'.

This procedure can be used to rename most objects in SQL Server. Please keep in mind, if you have any other SQL scripts that were referencing the old key name, you'll need to update them to reference the new key name.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.