Uniqueidentifier insertion usage

Source: Internet
Author: User
Insert statement:
Create Table myuniquetable
(Uniquecolumn uniqueidentifier default newid (),
Characters varchar (10 ))
Go
Insert into myuniquetable (characters) values ('abc ')
Insert into myuniquetable values (newid (), 'def ')
Go

Uniqueidentifier
Guid ).

Note
The uniqueidentifier data type column or local variable can be initialized to a value in two ways:

Use the newid function.
Convert a String constant to the following format (XXXXXXXX-XXXX-xxxxxxxxxxxx, where each X is in a hexadecimal format in the range of 0-9 or a-f Number ). For example, 6f9619ff-8b86-d011-b42d-00c04fc964ff is a valid uniqueidentifier value.
The comparison operator can be used with the uniqueidentifier value. However, the arrangement is not achieved by comparing the bit modes of two values. Only the operations that can be performed on the uniqueidentifier value can be compared (=, <>,<, >,<=, >=) and checked null (is null and is not null ). Other arithmetic operators are not allowed. All column constraints and attributes (except identity) can be used for the uniqueidentifier data type.
Use uniqueidentifier data
The uniqueidentifier data type stores 16-byte binary values, which are the same as the Globally Unique Identifier (guid. GUID is a unique binary number. No duplicate guid value is generated on any two computers in the world. GUID is used to assign a unique identifier to a network with multiple nodes and computers.
The guid value of the uniqueidentifier column is generally obtained in the following ways:
Call the newid function in a Transact-SQL statement, batch processing, or script.
In Application In the program code, call the application API function or method that returns the guid value.
The newid functions and application API functions and methods generate a new uniqueidentifier value from the identification numbers on their NICs and the unique number of the CPU clock. Each Nic has a unique ID. It is generated by the uniqueidentifier returned by newid using the NIC on the server. The uniqueidentifier returned by the application API functions and methods is generated using the NIC on the client.
Generally, uniqueidentifier is not defined as a constant, because it is difficult to ensure that the actually created uniqueidentifier is unique. There are two methods to specify a uniqueidentifier constant:
String format
'6f9619ff-8b86-d011-b42d-00c04fc964ff'
Binary format
0xff19966f868b11d0b42d00c04fc964ff
The uniqueidentifier data type does not automatically generate a new ID for the newly inserted row as the identity attribute. To obtain the new uniqueidentifier value, the table must have a default clause for the specified newid function or an insert statement using 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 contain the uniqueidentifier value that appears multiple times, unless the unique or primary key constraint is specified for this column. When multiple rows reference the same primary key in the source table, the foreign key column that references the uniqueidentifier primary key of other tables will contain the individual uniqueidentifier values that appear multiple times.
A table can have multiple uniqueidentifier columns. You can specify a uniqueidentifier column with the rowguidcol attribute in each table. The rowguidcol attribute indicates that the uniqueidentifier value of this column uniquely identifies rows in the table. However, this attribute does not perform this uniqueness. Uniqueness must be implemented through other mechanisms, such as specifying the primary key constraint for the column. The rowguidcol attribute is mainly used for SQL Server replication.
The main advantage of the uniqueidentifier data type is to ensure that the values generated by the transact-SQL newid function or the guid function of the application are globally unique.
The uniqueidentifier data type has the following Disadvantages:
The value is long and hard to understand. This makes it difficult for users to type them correctly, and it is more difficult to remember.
These values are random and they cannot accept any pattern that makes them more meaningful to users.
There is no way to determine the order in which uniqueidentifier values are generated. They are not applicable to existing applications that rely on incremental key values.
The uniqueidentifier data type has 16 bytes, which is relatively larger than other 4-byte integers. This means that the index created using the uniqueidentifier key may be slower than the index implemented using the int key.
If the global uniqueness is not required, or a continuously increasing key is required, you can use the identity attribute.

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.