Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to set self-increment of ID field in sql server

2025-01-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)05/31 Report--

Sql server how to set ID field self-increment, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain in detail for you, people with this need can come to learn, I hope you can gain something.

Open the database table to be set, and click the field to be set, such as id, and the column properties of id will appear below. You can set the field self-increment by setting the properties of "identity specification". From the following figure, whether the value of "yes identification" is not self-increment indicates that id is not a self-increment field that can set the fields to be self-incremented, such as the int,bigint type, while the varchar type cannot be self-incremented. For example, when you look at the column properties of name, you can see whether the "Yes identity" is unchangeable. Click "identity Specification", after expansion, click "Yes identity" and the drop-down list button will appear. You can double-click the settings or select from the drop-down list. After setting the value of "Yes identity" to "Yes", you can see that the following properties write the value by default to set the identity increment and identity seed. Identification increment is the value that the field automatically increases each time, such as 1, and the field increases by 1 each time. If the identification seed is the initial value of the field, such as 1, then the field value of the first record is 1. After the setting is completed, don't forget to click "Save"

END

Matters needing attention

Pay attention to the type of field when setting self-increment field. Not all field types can set self-increment field.

Tables with self-increasing primary keys in SQLServer usually cannot be inserted with ID values directly, but can be inserted in the following ways.

1. SQLServer self-adding primary key creation syntax:

Identity (seed, increment)

Among them

Seed starting value

Increment increment

Example:

Create table student (id int identity (1), name varchar (100)

two。 Specifies that data is inserted from the increment of the primary key column value (SQL Server 2000)

Execute the following statement first

SET IDENTITY_INSERT [database. [owner. ]] {table} ON

Then execute the insert statement

Finally, execute the following statement

SET IDENTITY_INSERT [database. [owner. ]] {table} OFF

Example:

The table is defined as follows

Create table student (id int identity (1), name varchar (100)

Insert data

Set IDENTITY_INSERT student ONinsert into student (id,name) values (1); insert into student (id,name) values (2); does set IDENTITY_INSERT student OFF help you after reading the above? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 252

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report