GUID (Global Unique Identifier) Global Unique Identifier, which is a 16-byte binary value generated by the ID number (each NIC has a unique identification number) and a unique number of CPU clocks.
The GUID format is "xxxxxxx-xxxxxxxxxxxxxxxxxxxxxxx", where each x is a hexadecimal number in the range of 0-9 or A-F. For example: 6F9619FF-8B86-D011-B42D-00c04FC964FF is an effective GUID value.
Any two computers in the world do not generate duplicate GUID values. GUID is mainly used to assign an identifier that must have uniqueness in a network or system with multiple nodes, multiple computers. On the Windows platform, the GUID app is very wide range: registry, class and interface identification, database, and even automatic generated machine names, directory names, etc.
When I develop ASP.NET applications, I use a large number of ID columns of type GUID as a keyword (key) of each entity table. Because of its unique, easy-to-produce features, give application processing give more benefits.
1. Use GUID in SQL Server
If the column type is specified as UNIQUEIDENTIFIER in the table definition of SQL Server, the value of the column is the GUID type.
The newID () function in SQL Server can generate a unique value of the GUID, and several ways to use this function are as follows:
1) as a column default value
Set the default value of the column of UNIQUEIDENTIFIER to NEWID () so that this column GUID value is automatically generated when the new row is inserted into the table. 2) Use T-SQL
Use a NewID () function in T-SQL, such as "INSERT INTO TABLE (ID, ...) VALUES (NEWID (), ...)" to generate this column of GUID values.
3) Get the GUID value in advance
Due to special features, the ID value of the new row is needed in advance, and you can use the following C # code to get the value of the GUID in advance, and then store it into the database:
Sqlcommand cmd = new sqlcommand (); cmd.commandtext = "SELECT newID ()"; string rowid = (string) cmd.executescalar (); cmd.commandtext = "Insert Into Table (ID, ...) VALUES (@ID) , ...) cmd.Parameters.add ("@ ID", sqldbtype.uniqueIndentifier) .value = new guid (rowid); cmd.executenoQuery ();
The UNIQUEIDentifier value cannot be arithmetic operation, but it can be done (significant) comparison operation and NULL check; it can't be like the Identity column, you can know the order of addition time of each row, can only increase other time or time stamp columns To do this.
2, use GUID in .NET
GUID is very wide in .NET, and .NET Framework provides a special GUID infrastructure.
The commonality of the GUID structure includes:
1) Guid.newguid ()
Generate a new GUID unique value
2) guid.toString ()
Convert GUID values into strings for easy processing
3) Constructor GUID (String)
The GUID structure is generated by String, where string can be uppercase, or lowercase, can contain both ends "{}" or "()", and even omitting the "-", the constructor of the GUID structure has a lot. Other constructors are not commonly used. At the same time, in order to apply the need for the use of GUID in the database, .NET Framework also provides a SQLGUID structure, which is similar to the GUID structure, which is only the same processing mode, and the last 6 bytes of the SQLGUID calculated value. The GUID calculates all 16 bytes, which may have a certain impact on the sort of the UniqueIdentifier column in SQL Server, of course, this sorting significance is not large.
The .NET Framework can use class GuidConverter to provide a type converter that converts the GUID structure with various other representations.
3, the advantages and disadvantages of GUID
1) Advantages
· Compared to the Identity column, UNIQUEIDENTIFIER columns can beware of newly added line IDs in advance through the newID () function, which provides a very convenient for the application's subsequent processing.
· Easy to transplant in the database, other databases do not necessarily have an Identity column, and the GUID column can be converted to other databases as a character pattern, while the GUID value generated in the application is stored in the database, which does not bring the original data It is affected.
· Easy to initialize the database, if the application is to load some initial data, the Identity column is more troublesome, and the UNIQUEIDENTIFIER column does not need any processing, directly loaded with T-SQL.
· Easier to permanently identify certain objects or constants, such as class ClassID, instance identification of objects, contacts in UDDI, service interface, TMODEL identification, etc.
2) Disadvantages
· The GUID value is longer, it is not easy to remember and entered, and this value is random, no order, so you should pay attention to the occasion, it is best not to use it as your email address J
· The value of GUID has 16 bytes, relatively larger than other integers such as 4 bytes. This means that if you use the UNIQUEIDENTIFIER key in the database, you may bring two negative impacts: the storage space is increased; the index time is slow.
In combination, the convenience of the advantages of GUID far exceeds the impact of its shortcomings. With the continuous development of interconnection and integration technology such as WebService, its unique identification features make it get more and more wide, in your It should also be considered in the application.
2004-03-23 10:57:00 By MosLes [Comments: 6)]