Daily usage of SQLServer self-increasing id

To set an auto-increment field in a certain table in the SQL Server database, the field type must be a numeric type, such as decimal, int, numeric, smallint, bigint, tinyint, etc. It should be noted that if the decimal and numeric types are set to auto-increment columns , The number of decimal places must be 0, otherwise the setting is unsuccessful.

The keyword syntax for setting auto-increment is: create table table name (field name field type identity(m,n)), the field type must be numeric and integer. m is the starting value of the auto-increment id, n is the step size, which means starting from m and increasing by n each time.

If neither value is specified, the default is (1,1), and the two values ​​can be specified at the same time
by default, and only one parameter is not supported.

I don’t know if you have noticed: both m and n can be negative. If n is a negative number, id is decremented by 1 each time. Has anyone used the auto-decrement function of auto-increment columns?

The two most commonly used functions are auto-incremented:

The first one turns off auto-increment, manually inserts an id value, and then turns on auto-increment.

The syntax is as follows:

set identity_insert table name on;
insert into table name (column name 1, column name 2, column name 3, column name 4) values ​​(data 1, data 2, data 3, data 4);
set identity_insert table name off;

The column name 1 is an auto-increasing column.

The second common function is to set the next value of the auto-increment column to the value you want

The syntax is as follows:

DBCC CHECKIDENT ('[data table name]', RESEED, [required value -1])

For example, if you want to set the value of the next auto-increment id to 10000, then

DBCC CHECKIDENT (table_name,RESEED, 9999).

When inserting data again, you will see that the value of the auto-increment id column is 10000.

Well, I will study here today. Through this article, have you learned a trick?

Guess you like

Origin blog.51cto.com/15057852/2649862