Simple Method for setting ID field auto-increment when creating a table on SQL server, SQL Field
- Open the database table to be set and click the field to be set, such as id. The column attribute table of id appears below.
- In column attributes, you can set the field auto-increment by setting the "Identity specification" attribute. From the above, whether the "yes" value indicates that the id is not an auto-increment Field
- The fields that can set the ID field auto-increment must be auto-incrementing, such as the int and bigint types, but the varchar type cannot auto-increment. For example, when viewing the column attribute of name, you can see whether "yes" cannot be changed.
- Click "id specifications". Click "yes" to display the drop-down list. You can double-click Settings or select from the drop-down list.
- After the "yes" value is set to "yes", you can see that the following attributes are written with values by default.
- You can set the ID increment and ID seed. Id increment is the value automatically added to the field each time. For example, 1 is added to the field each time. The ID seed is the initial value of the field. For example, 1, the value of this field in the first record is 1.
- After setting, do not forget to click "save"
END
Notes
When setting an auto-increment field, pay attention to the field type. Not all field types can be set to auto-increment.
For tables with auto-incrementing primary keys in SQLServer, ID values cannot be specified directly. You can use the following method to insert data.
1. SQL Server auto-incrementing primary key creation Syntax:
identity(seed, increment)
Where
Start value of seed
Increment
Example:
create table student( id int identity(1,1), name varchar(100))
2. Specify the auto-increment primary key column value to insert data (SQL Server 2000)
First, execute the following statement:
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,1), name varchar(100))
Insert data
set IDENTITY_INSERT student ONinsert into student(id,name)values(1,'student1');insert into student(id,name)values(2,'student2');set IDENTITY_INSERT student OFF
Summary
The above is a simple method for setting the ID field auto-increment when creating a table on SQL server. I hope it will be helpful to you. If you have any questions, please leave a message for me, the editor will reply to you in a timely manner. Thank you very much for your support for the help House website!