GUID (Global unique identifier) Globally unique identifier, which is a 16-byte binary value generated by the identity number on the network card (each network card has a unique identification number) and the unique number of the CPU clock.
The format of the GUID is "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx", where each x is a hexadecimal number in the range of 0-9 or a-f. For example, 6F9619FF-8B86-D011-B42D-00C04FC964FF is a valid GUID value.
No two computers in the world will generate duplicate GUID values. GUIDs are primarily used to assign identifiers that must be unique in a network or system that has multiple nodes, multiple computers, and more than one computer. On the Windows platform, GUIDs are widely used: Registry, class and interface identifiers, databases, even automatically generated machine names, directory names, and so on.
When I developed the ASP, I used a large number of ID columns of type GUID as keywords (keys) for each entity table. Because of its unique, easy-to-produce features, it brings many benefits to application processing.
1. Using GUIDs 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 produce a GUID unique value, and several ways to use this function are as follows:
1) as the default value for the column
Set the default value of the uniqueidentifier column to NewID () so that when new lines is inserted into the table, this column GUID value is automatically generated.
2) using T-SQL
Use the NewID () function in T-SQL, such as "INSERT into Table (ID,...) VALUES (NewID (),...) " To generate the GUID value for this column.
3) Get the GUID value in advance
For special functions, you need to know the ID value of the new row beforehand, or you can use the following C # code to get the value of the GUID in advance and store it in 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.uniqueidentifier). Value = new Guid (RowID);
Cmd. Executenoquery ();
The uniqueidentifier value cannot perform arithmetic operations, but it can carry out (insignificant) comparison operations and NULL checks; it cannot, like an IDENTITY column, be aware of the order in which each row is incremented, and can only be done by adding other time or timestamp columns.
2. Using GUIDs in. NET
GUIDs are widely used in. NET, and the. NET Framework provides a dedicated GUID infrastructure.
Common methods for GUID structure include:
1) Guid.NewGuid ()
To generate a new GUID unique value
2) guid.tostring ()
Converts a GUID value into a string for easy handling
3) constructor Guid (string)
The GUID structure is generated by string, where string can be uppercase or lowercase, can contain both ends of the delimiter "{}" or "()", or even omit the middle "-", the GUID structure has a lot of constructors, other construction usages are not commonly used.
At the same time, the. NET Framework provides a SQLGUID structure to suit the needs of using GUIDs in the database, which is similar to the GUID structure, except that the two methods of sorting (COMPARETO) are handled differently, SqlGuid computes the last 6 bytes of a value. While the Guid calculates all 16 bytes, this difference may have some effect on the ordering of uniqueidentifier columns in SQL Server, which is of course not very meaningful.
Class Guidconverter can be used in the. NET Framework to provide a type converter that converts a GUID structure to a variety of other representations.
3. The advantages and disadvantages of GUIDs
1) Advantages
- Compared to the IDENTITY column, the uniqueidentifier column can know the new row ID in advance with the NewID () function, which provides great convenience for subsequent processing of the application.
- Easy to migrate databases, other databases do not necessarily have an IDENTITY column, and GUID columns can be converted to other databases as character columns, while storing the GUID values generated in the application into the database, it does not affect the original data.
- Easy to initialize the database, if the application to load some initial data, the IDENTITY column processing is more cumbersome, and the uniqueidentifier column without any processing, directly loaded with T-SQL.
- It facilitates the permanent identification of certain objects or constants, such as ClassID of classes, instance identities of objects, contacts in UDDI, service interfaces, tmodel identity definitions, and so on.
2) Disadvantages
- The GUID value is longer, not easy to remember and input, and this value is random, no order, so use to pay attention to the occasion, it is best not to try to use it as your e-mail address J
- The GUID has a value of 16 bytes, which is relatively large compared to other integers such as 4 bytes. This means that if you use the uniqueidentifier key in your database, you may have two negative effects: increased storage space and slower indexing time.
- From: http://blog.csdn.net/shouyenet1/article/details/4160210
Asp. NET in the GUID class