Generating GUID

后端 未结 4 1082
囚心锁ツ
囚心锁ツ 2020-12-31 08:02

I am thinking to use a GUID in my .net app which uses SQL Server. Should I be writing a stored procedure which generates the GUID on each record entered or should I be direc

4条回答
  •  耶瑟儿~
    2020-12-31 08:19

    No, your assumption is wrong: the database won't be remembering anything - so there's no benefit from that point of view.

    If you're using the GUID as your primary key / clustering key in SQL Server, which is a bad idea to begin with (see here, here or here why that's the case), you should at least use the newsequentialid() function as default constraint on that column.

    CREATE TABLE YourTable(ColumnA uniqueidentifier DEFAULT NEWSEQUENTIALID()) 
    

    That way, the database would generate pseudo-sequential GUID's for your PK and thus would make the negative effects of using a GUID as PK/CK at least bearable....

    If you're not using the GUID as your primary key, then I don't see any benefit in creating that GUID on the server, really.

提交回复
热议问题