SQL Server 2005 and growth since the primary key identity to say goodbye --NEWSEQUENTIALID () (reprint)

In SQL Server 2005 environment, the table's primary key should be how to design.
Primary key program is currently mainly used in a total of three kinds:

  • Automatic primary key growth
  • Manual primary key growth
  • UNIQUEIDENTIFIER主键

1, start with the automatic growth of the primary key, it has the advantage of simplicity, the type of support bigint but it has a fatal weakness:

When we need to replicate data between multiple databases (SQL Server data distribution, subscription mechanism allows us to copy data between database operations), auto-growth fields may cause the primary key conflicts when data consolidation. Imagine a database when the Order table copy database to another library in the Order table, OrderID in the end that should not automatically increase it?

 

2, again manually increase the primary key, it has the advantage of customizing their own primary key column, the data type of the primary key column and the data sample can be controlled, can be obtained stably target key, will not be repeated, but it maintains the costs are out, first of all generated keys values ​​need to write your own stored procedures to produce, large network overhead, also taking into account concurrency conflicts so on running.

 

3, the last is UNIQUEIDENTIFIER primary key, use it as a GUID key, you can call directly newid () to obtain a globally unique identifier, even if there will not be consolidated data tables duplication GUID but has two weaknesses: First, and type int comparison, the GUID is the length of 4 times the former. Second, the GUID with NEWID () obtained in erratic, because the column as a primary key, there must be clustered index, then when inserting new data, will be a very time-consuming operation. in this case UNIQUEIDENTIFIER as the primary key will be greatly diminishing efficiency.

So the next SQL Server 2000 DBA environment tend to write a stored procedure to generate time-related GUID, that is, with the generation of time in front of the GUID. This ensures that the primary key is generated out of the global unique and time increments. But it went back to the second kind of primary key programs, inconvenient maintenance.

 

4, SQL Server 2005 has solved this problem by using a NEWSEQUENTIALID ()

GUID generated by this function is increasing, the following look at its use

- Create Table Experiment 
- 1 Create id column type the UNIQUEIDENTIFIER 
- 2ROWGUIDCOL just an alias for the column, a table can have only one 
- 3PRIMARY id primary key KEY OK 
- 4 Use DEFAULT constraint for the column automatically Add the GUID 
Create  Table Jobs 
( 
ID the UNIQUEIDENTIFIER  the ROWGUIDCOL  a PRIMARY  KEY   the NOT  nULL 
CONSTRAINT  [ DF_jobs_id ]  the DEFAULT (the NEWSEQUENTIALID ()), 
Account VARCHAR ( 64 ) Not  null , 
password VARCHAR ( 64 ) Not  null 
)
go
 
select * from jobs
--添加实验数据
insert jobs (account,password) values ('tudou','123')
insert jobs (account,password) values ('ntudou','123')
insert jobs (account,password) values ('atudou','123')
insert jobs (account,password) values ('btudou','123')
insert jobs (account,password) values ('ctudou','123')
 
select * from jobs

result:

 

- the use of identity is that we can get to the newly added by id @@ IDENTITY the Select 
- Use UNIQUEIDENTIFIER how to do it? 
- to take manual growth method select NEWSEQUENTIALID () to remove the id to add 
- no, the syntax is not supported 
- can be taken by the following method to add new data id 
- use in ADO.NET and Select @@ IDENTITY as 
the DECLARE  @outputTable  TABLE (ID UNIQUEIDENTIFIER )
 the INSERT  the iNTO Jobs (Account, password) 
the OUTPUT INSERTED.ID the iNTO  @outputTable 
the VALUES ( ' dtudou ' , ' 123 ' ) 
 
the SELECT ID the FROM  @outputTable 
 
-Compare the data 
SELECT  *  from Jobs

result:

 

- the ROWGUIDCOL column is the primary key alias, can be directly used as the column name 
- so to ignore the name of the primary key column 
INSERT Jobs (Account, password) values ( ' etudou ' , ' 123 ' )
 SELECT  the ROWGUIDCOL  from Jobs

result:

 

Description link

 

Guess you like

Origin www.cnblogs.com/OpenCoder/p/12090674.html