How random are constraint names?

  • I've read in a few places that when a constraint name is unspecified the system generates the name randomly, but there's clearly more to this. I mean the hex-looking bit at the end.

    For example, when I run this:

    CREATE DATABASE TestTheory

    GO

    USE TestTheory

    GO

    CREATE TABLE Theory (

    Id int identity(1,1) primary key,

    Data varchar(20),

    LogDate datetime default getdate()

    )

    On my SQL 2008 R2 laptop the primary key is always PK__Theory__3214EC077F60ED59 and the default constraint is always DF__Theory__LogDate__014935CB - not a different random value each time. Not a major issue as if it's a problem we can simply name the objects - but anybody know what's going on here?

  • It must be a hash of some kind based on the server. I got different values than you did. And, if I dropped the table and recreated it, I got different values again.

    "The credit belongs to the man who is actually in the arena, whose face is marred by dust and sweat and blood"
    - Theodore Roosevelt

    Author of:
    SQL Server Execution Plans
    SQL Server Query Performance Tuning

  • That's true, if I drop the database and re-run that script the same values come back, if I drop the table different values are used. But those second values are also repeated following dropping the database, so there's a sequence. Definitely not random!

  • Random or not, they're ugly to read.

    "The credit belongs to the man who is actually in the arena, whose face is marred by dust and sweat and blood"
    - Theodore Roosevelt

    Author of:
    SQL Server Execution Plans
    SQL Server Query Performance Tuning

  • I admit, I name all constraints myself, including DEFAULTs, even though for DEFAULTs that may be deprecated.

    CREATE TABLE Theory (

    Id int identity(1,1) CONSTRAINT Theory__PK primary key,

    Data varchar(20),

    LogDate datetime CONSTRAINT Theory__DF_LogDate default getdate()

    )

    SQL DBA,SQL Server MVP(07, 08, 09) "It's a dog-eat-dog world, and I'm wearing Milk-Bone underwear." "Norm", on "Cheers". Also from "Cheers", from "Carla": "You need to know 3 things about Tortelli men: Tortelli men draw women like flies; Tortelli men treat women like flies; Tortelli men's brains are in their flies".

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

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