Data type uniqueidentifier in SQL Server

Source: Internet
Author: User
Tags arithmetic operators string format

What exactly is the data type uniqueidentifier in SQL Server?

This type is typically used as a primary key and can be used to generate a unique value using the NEWID () of the SQL syntax.
What I want to ask is, is this value a long integer data value, or is it a different value? How do I control him in the program?
Welcome to a lot of exchanges.

Excerpt from Help:

uniqueidentifier
The globally unique identifier (GUID).

Gaze
A column or local variable of the uniqueidentifier data type can be initialized to a value using two methods:

Use the NEWID function.


Converts a string constant to, for example, the following form (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 uniqueidentifier value.
The comparison operator can be used with the uniqueidentifier value. However, permutations are not implemented by a bit pattern of two values. The operation that agrees to run on the uniqueidentifier value is only comparable (=, <>, <,;, <=, >=) and checks for null (is null and is not NULL). Do not agree to use other arithmetic operators. All column constraints and attributes (except for IDENTITY) agree with the uniqueidentifier data type.


Using uniqueidentifier data
The uniqueidentifier data type stores a 16-byte binary value that uses the same value as the globally unique identifier (GUID). A GUID is a unique binary number, and no two computers in the world will generate repeated GUID values. GUIDs are primarily used to assign identifiers that must be unique in a network with multiple nodes and multiple computers.

The GUID value of the uniqueidentifier column is usually obtained in the following way:

Call the NEWID function in a Transact-SQL statement, batch, or script.


In your application code, call the application API function or method that returns the GUID value.
The Transact-SQL NEWID functions and application API functions and methods generate a new uniqueidentifier value from the identity number on their NIC and the unique number of the CPU clock. Each NIC has a unique identification number. The uniqueidentifier returned by NEWID is generated using the NIC on the server. The uniqueidentifier returned by the application API functions and methods are generated using the NIC on the client.

uniqueidentifier is generally not defined as a constant, because it is very difficult to guarantee that the uniqueidentifier actually created is unique. There are two methods of specifying uniqueidentifier constants:

string format
' 6f9619ff-8b86-d011-b42d-00c04fc964ff '

binary format
0xff19966f868b11d0b42d00c04fc964ff

The uniqueidentifier data type does not voluntarily generate a new ID for the newly inserted row, as the identity property does. To obtain a new uniqueidentifier value, the table must have a default clause that specifies the NEWID function, or an INSERT statement that uses the NEWID function:

CREATE TABLE myuniquetable
(Uniquecolumn uniqueidentifier DEFAULT NEWID (),
Characters VARCHAR (10))
GO
INSERT into myuniquetable (characters) VALUES (' abc ')
INSERT into Myuniquetable VALUES (NEWID (), ' Def ')
GO

The uniqueidentifier column can include multiple occurrences of the uniqueidentifier value, unless a UNIQUE or PRIMARY KEY constraint is also specified for this column. When there are multiple rows referencing the same primary key in the source table, the foreign key column referencing the uniqueidentifier primary key of the other table will include the individual uniqueidentifier values that appear multiple times.

A table can have more than one uniqueidentifier column. You can specify a uniqueidentifier column with the ROWGUIDCOL property in each table. The ROWGUIDCOL property indicates that the uniqueidentifier value of this column uniquely identifies the row in the table. However, this property does not run this uniqueness. Uniqueness must be run through other mechanisms, such as specifying the PRIMARY KEY constraint for a column. The ROWGUIDCOL property is primarily used for SQL Server replication.

The main advantage of the uniqueidentifier data type is that the values generated by the Transact-SQL NEWID function or application GUID function are unique globally.

The uniqueidentifier data types have several drawbacks:

The value is long and difficult to understand. This makes it difficult for users to type them correctly, and is more difficult to remember.


These values are random, and they cannot accept patterns that make them more meaningful to the user, no matter what.


There is no way to determine the order in which uniqueidentifier values are generated. They do not apply to existing applications that rely on incremental key values.


The uniqueidentifier data type has 16 bytes, which is relatively large compared to other integers such as 4 bytes. This means that indexes built using the uniqueidentifier key may be slower than indexes that are implemented using the INT key.
Assuming that global uniqueness is not required, or if you need a continuously incrementing key, you can consider using the IDENTITY property.


^_^

1. SQL Server Help has a specific answer, find it yourself.
2. Open Help
Index--Enter uniqueidentifier to do so.


UniqueIdentifier is a globally unique identifier (GUID)
NEWID () return type is uniqueidentifier
Examples in SQL Server online Help:
DECLARE @myid uniqueidentifier
Set @myid =newid ()
print ' Value of @myid is ' +cast (@myid as varchar (255))
Each execution of the above program returns different uniqueidentifier

Data type uniqueidentifier in SQL Server

Related Article

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.