uniqueidentifier data type

Source: Internet
Author: User
Tags insert microsoft sql server microsoft sql server 2005 string format
Data | data type

Data that you want to produce in this uniquely identifiable format:

6f9619ff-8b86-d011-b42d-00c04fc964ff


What should we do?

================================================================

For:

The uniqueidentifier data type can store a 16-byte binary value that acts as a globally unique identifier (GUID). A GUID is a unique binary number; no two computers in the world generate duplicate GUID values. GUIDs are primarily used to assign identifiers that must be unique in a network that has multiple nodes and multiple computers.

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

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

In the application code, call the application API function or method that returns the GUID.

Transact-SQL NEWID functions and application API functions and methods generate new uniqueidentifier values using their network card identification number plus the unique number of the CPU clock. Each NIC has a unique identification number. The uniqueidentifier value returned by NEWID is generated by using the NIC on the server. The uniqueidentifier values returned by application API functions and methods are generated by using the NIC in the client.

UniqueIdentifier values are usually not defined as constants. You can specify uniqueidentifier constants in the following ways:

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

Binary format: 0xff19966f868b11d0b42d00c04fc964ff

The uniqueidentifier data type does not automatically generate a new ID for the inserted row in the manner of the IDENTITY property. For example, to get a new uniqueidentifier value, the table must have a default clause that specifies the NEWID function or the Newsequentialid function, or the INSERT statement must use the NEWID function.

CREATE TABLE myuniquetable
(Uniquecolumn uniqueidentifier DEFAULT NEWID (),
Characters VARCHAR (10))
Go
INSERT into Myuniquetable (Characters) VALUES (' Uiok ')
INSERT into Myuniquetable VALUES (NEWID (), ' Uiok ')
Go
Attention:

You can use Newsequentialid to generate GUIDs to reduce page contention on leaf-level indexes. Newsequentialid can only be used with DEFAULT constraints on table columns of type uniqueidentifier.

A uniqueidentifier value can appear multiple times in the uniqueidentifier column, unless a UNIQUE or PRIMARY KEY constraint is specified for the column. When multiple rows refer to the same primary key in the source table, individual uniqueidentifier values can appear multiple times in foreign key columns referencing the uniqueidentifier primary key in other tables.

A table can have multiple uniqueidentifier columns. 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, the property does not perform any action that enforces uniqueness. You must use other mechanisms to enforce uniqueness, such as specifying the PRIMARY KEY constraint for a column. The ROWGUIDCOL property is primarily used for Microsoft SQL Server 2005 replication. Merge replication and transactional replication with update subscriptions use the uniqueidentifier column to ensure that rows are uniquely identified in multiple replicas of a table.

The uniqueidentifier data type has the following disadvantages:

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 do not support any pattern that makes them more meaningful to the user.

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.

When uniqueidentifier is 16 bytes, its data type is larger than other data types (for example, 4-byte integers). This means that using the uniqueidentifier key to generate an index is relatively slower than using the INT key to generate an index.

Consider using the IDENTITY property when you do not require global uniqueness or when you prefer to use a key that is incremented by sequence.




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.