Reprint GUID Introduction

Source: Internet
Author: User
Tags table definition

Reprint http://www.cnblogs.com/illele/archive/2008/02/25/1080554.html

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.

Here's a function to generate a GUID:

private String GetGuid ()
{
System.Guid GUID = new GUID ();
GUID = Guid.NewGuid ();
String str = GUID. ToString ();
return str;
}

Randomly generate the following string:
E92b8e30-a6e5-41f6-a6b9-188230a23dd2

Format Description:

System.Guid.NewGuid (). ToString (format)

format specifier
Format of the return value


N 32 bits:
Xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
such as: E92B8E30A6E541F6A6B9188230A23DD2

D 32 digits separated by hyphens:
Xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
such as: E92B8E30-A6E5-41F6-A6B9-188230A23DD2

A 32-digit number enclosed in curly braces, separated by hyphens:
{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}
such as: {E92B8E30-A6E5-41F6-A6B9-188230A23DD2}

P enclosed in parentheses, 32 digits separated by hyphens:
(XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX)
such as: (E92B8E30-A6E5-41F6-A6B9-188230A23DD2)

Reprint GUID Introduction

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.