Naming Keys

  • Another 'newbie' question I'm afraid.

    Is there a reason why SQL generates Key names such as FK__Datasourc__Conta__47DBAE45.

    Is there any harm in me changing the name of the keys as I create them to, for example FK__Datasource__Contactid?

  • http://msdn.microsoft.com/en-us/library/ms188351.aspx

    Regards,

    Willem
    http://wschampheleer.wordpress.com[/url]

  • I can't say I've ever given any thought as to why MS names things the way it does (that way lies madness), but it probably generates some type of GUID for the end of it in order to create a unique name.

    As to naming a FK - when you create it you can give it whatever unique name you wish. We specify the Foreign Key names here within our table-creation scripts and try to give them names that make it obvious what fields on which tables they are referring.

    If you look at BOL for Create Table the example of foreign key implementation has a somewhat more user-friendly name than the example you have come across.

    BrainDonor.

  • Thanks for the reply.

    When I create a table I simply state "references OTHERTABLE(Field)". Thus generating a seemingly random FK name.

    I'll check out BOL.

    Thanks again!

  • You can always specify your own name for the constraint using the following syntax

    col1 int CONSTRAINT [yourname] FOREIGN KEY REFERENCES othertable(PK))

    Regards,

    Willem
    http://wschampheleer.wordpress.com[/url]

Viewing 5 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic. Login to reply