Generating GUID

后端 未结 4 1063
囚心锁ツ
囚心锁ツ 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:08

    RFC41221: «Do not assume that UUIDs are hard to guess; they should not be used as security capabilities (identifiers whose mere possession grants access), for example. A predictable random number source will exacerbate the situation».

    In simple task incremental uint64 better.

    NOT USE GUID! If need security.

    http://social.msdn.microsoft.com/Forums/en/netfxbcl/thread/b37b3438-90f4-41fb-adb9-3ddba16fe07c

    0 讨论(0)
  • 2020-12-31 08:16

    SQL Server has the creation of GUID's built in. There is no need to write a separate stored procedure for this.

    You can use

    • NEWID()
    • NEWSEQUENTIALID()

    The key difference between both procedures would be that the sequential GUID should be used if it is for a primary clustered key.

    I'm not sure why you would want the database engine to remember the previous generated GUID.

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-12-31 08:26

    My preference is to create GUID in the application not the db.

    • Simplifies the retrieval of rows after insertion.
    • Easier domain/business layer unit testing.
    • Faster. At least for entity framework. http://blogs.msdn.com/b/adonet/archive/2010/06/28/performance-impact-of-server-side-generated-guids-in-ef.aspx
    0 讨论(0)
提交回复
热议问题