Simple Method for setting ID field auto-increment when creating a table on SQL server, SQL Field

Source: Internet
Author: User

Simple Method for setting ID field auto-increment when creating a table on SQL server, SQL Field

  1. 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.
  2. 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
  3. 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.
  4. Click "id specifications". Click "yes" to display the drop-down list. You can double-click Settings or select from the drop-down list.
  5. After the "yes" value is set to "yes", you can see that the following attributes are written with values by default.
  6. 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.
  7. 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!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.