Set automatic table numbers in SQL Server-specific operations

Source: Internet
Author: User

We all know that many database software provide the automatic numbering function for tables, which is very useful for some table functions and can reduce unnecessary work, I recently used SQL Server, So I encountered some problems in this process, but after searching for information and exploration, I have finally solved them. So I will write down my own experiences, we hope you can solve the same problem quickly.
There are two ways to automate SQL Server:
1. Create and open the window in the form of window through "Enterprise Manager". Here we take the ID of the test table created in the test database as the automatic number field as an example:


First, use the "Database" pop-up menu "New Database" to create a database test;
Then, select the created test database, select "table" in the window, right-click the table, and select "new table" from the pop-up menu. The table designer is used to design the table. is the set effect. after the design is complete, click the close button to save and name it "test"

2. create a query analyzer using SQL. Open the query analyzer, in the query window, enter the SQL statement
Create Database test -- create test database
Create Table Test (ID int identity () not null, test text) -- the identity (the initial value of the automatic number, the increment of the automatic number) sets the automatic number.
3 Note:
(1) only one column in a table can be defined as the automatic numbering attribute.
(2) the following data types can be automatically numbered in SQL Server: decimal, Int, numeric, smallint, bigint, or tinyint.
(3) when using SQL statements to insert data into a table with automatic numbers, do not include columns with automatic numbers. If you want to insert a row of data into the test table, the insert statement should be: insert into test (test) values ('this is a test table') or insert into test values ('this is a test table '), you cannot use insert into test (ID, test) values ('', 'This is a test table') or insert into test values ('', 'This is a test table '). otherwise, "When identity_insert is set to off, explicit values cannot be inserted into the ID column of the 'test' table. "Error prompt. if you must insert a specific row in the auto-numbered column, for example, to execute insert into test values (1, 'This is not a test table '), you can run the set identity_insert test (Table Name) on statement first.

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.