SQL Server identity column

[turn]

First, the definition of identity columns and features
in SQL Server identity column, also known identifier column, customarily known as auto-increment.
The column has the following three types of characteristics:
1, the column data type is the type without a decimal value
2, during the insertion (the Insert) operation, the value of the column is generated by the system according to certain rules, does not allow nulls
3 column value will not be repeated, has the effect of identifying each row in the table, each table can have only one identity column.
Because of these characteristics, so that the identity column widely used in the design database.

Second, the composition of the identity column
to create an identity column, usually specify three elements:
1, type (type)
in SQL Server 2000, the identity column type must be numeric type, as follows:
decimal, int, numeric, smallint, bigint, tinyint
One should note that, when selected and decimal numeric, decimal digits must be zero
Also note that for all numerical ranges for each type of data represented by

2, the seed (sEED)
is assigned to the value of the first row of the table, the default is a

3 increment (iNCREMENT)
adjacent increment between two identification values, the default is one.

Third, create and modify an identity column
to create and modify an identity column, usually in the Enterprise Manager and can be realized with Transact-SQL statement, using the Enterprise Manager management is relatively simple, please refer to the online help for SQL Server, which
discuss only using Transact-SQL method

1, specify the identity column when you create the table
Identity column can be used to establish the IDENTITY property, and therefore in SQL Server, also known as the identity column or IDENTITY column with the IDENTITY property.
The following example creates a named ID contains, int, and seed 1, is incremented by 1 column identifies
the CREATE TABLE T_test
(ID int the IDENTITY (1,1),
the Name VARCHAR (50)
)

2, in the conventional table adding identity column
examples below was added to a table named T_test ID, int, with a seed, the identity column is incremented by 1
- create a table
the CREATE tABLE T_test
(the name VARCHAR (50)
)

- insert data
INSERT T_test (Name) VALUES ( 'John Doe')

- an identity column increases
the ALTER tABLE T_test
the ADD ID int the iDENTITY (1,1)

. 3, judge whether the segment table having a column identity
can be determined using a table function OBJECTPROPERTY whether the iDENTITY ( identification) column usage:
the Select the OBJECTPROPERTY (the OBJECT_ID ( 'table'), 'TableHasIdentity')
if so, returns 1, otherwise 0

4 determines a whether the column is an identity column
may be used COLUMNPROPERTY function to determine a column whether iDENTITY property usage
SELECT COLUMNPROPERTY (OBJECT_ID ( 'table name') 'column name', 'IsIdentity')
If the column is an identity column, it returns 1, otherwise 0

4 query a table column names Identity column
in SQL Server is not ready function to achieve this functionality, the following SQL statement to achieve
the SELECT COLUMN_NAME the FROM INFORMATION_SCHEMA.COLUMNS
the WHERE TABLE_NAME = 'table' the COLUMNPROPERTY the AND (
the OBJECT_ID ( 'table'), COLUMN_NAME, 'IsIdentity') =. 1

. 5, the reference column identifies
if SQL statement identification reference column can be used in place of the keyword IDENTITYCOL
example, to query the ID is equal to the line in example 1,
the following two are equivalent query
the SELECT * = 1 the FROM T_test the WHERE IDENTITYCOL
the SELECT * the WHERE ID = T_test the FROM . 1

6 obtain identification columns seed value
using a function IDENT_SEED, usage:
the SELECT IDENT_SEED ( 'table')

7, acquiring an incremental amount identity columns
can use the function IDENT_INCR, usage:
the SELECT IDENT_INCR ( 'table')

8 acquires specified table identity value last generated
using functions IDENT_CURRENT, use:
SELECT IDENT_CURRENT('表名')
Note: When a table contains an identity column just created, is going through any insert operation, using the seed value is the value of the identity column IDENT_CURRENT function obtained, it should be noted in particular in the development of database applications.

9. [SQL Server] on the issue of identity column 1 starts counting
the time in SQL Server, we sometimes need to clear the data in the table after re-add records, identity column from 1 to re-start counting.
We just need to insert before recording, execute the following command:
the DBCC CHECKIDENT (table name, RESEED, 0)
a TRUNCATE TABLE can do, but also because of high efficiency:
with the each operation will delete logged, so inefficient, truncate table is a one-off, much faster efficiency.
However, Truncate Table limited, for example, the identity column is a foreign key to another table, and identifies the column of the current value is 1, starting from row 2 is inserted,
the DBCC CHECKIDENT (table name, RESEED, 0) can be used even with a case where the foreign key.

Reproduced in: https: //www.cnblogs.com/jenneyblog/archive/2012/09/11/IDEntity.html

Guess you like

Origin blog.csdn.net/weixin_33841503/article/details/93370512