GUID basic concepts and GUID basic usage in C #, SQL Server

Source: Internet
Author: User
Tags string format

The globally unique identifier (guid,globally unique Identifier) is a numeric identifier generated by the algorithm with a binary length of 128 bits. Ideally, no computer or computer cluster will generate two identical GUIDs. The total number of GUIDs has reached 2^128 (3.4x10^38), so the probability of randomly generating two identical GUIDs is very small, but not 0, and the algorithm used to generate GUIDs usually adds non-random parameters (such as time) to ensure that this repetition does not occur.

The GUID string format is "Xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx", where x represents any character in 0-9 or A-Z. For example, 5FF62D65-492F-4B30-81B4-D0C23A4560DD is a GUID string.

Generate GUIDs in C # (default lowercase):

// generate a new GUID Guid guid1 = guid.newguid (); Console.WriteLine (GUID1. ToString ()); // using a GUID string to construct a GUID New Guid (GUID1. ToString ()); Console.WriteLine (Guid2. ToString ()); Console.ReadLine ();

Generate GUID with SQL Server (default capitalization):

Select NEWID ()

The data type in the SQL Server database table is the uniqueidentifier column that stores the GUID string.

Disadvantages:

The 1.GUID value is longer, is not easy to remember and input, and this value is random, no order.

The 2.GUID value has 16 bytes, which is relatively large compared to other integers such as 4 bytes. This means that if you use the uniqueidentifier column as the primary key in your database, you may have two negative effects: increased storage space and slower indexing time.

Advantages:

1. 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.

2. 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 target data.

3. 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.

4. Make it easy to permanently identify certain objects or constants, such as the ClassID of a class, the instance identity of an object, and so on.

GUID basic concepts and GUID basic usage in C #, SQL Server

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.