Database primary key Auto Insert display value __ Database

Source: Internet
Author: User
SQL Server 2008 Database primary key self Insert display value

A few days ago, when I was at work, I was in the process of deleting data from tables in the database, after the deletion, the data that was added did not get the ID value of the original data (the ID in the table is the primary key and is set to self increasing), using SQL Server 2008, now resolved, and share with you.

Specific situation:

1. Set up the table T_test, set the primary key to increase, the following figure


2. Inserting data into the table

Because the primary key field ID in the table is self increasing, you do not need to specify a display insert when inserting, so the SQL statement is

<span style= "FONT-SIZE:24PX;" >insert into  [xxx].[ DBO]. [T_test]  VALUES (' xiaoming ')
insert INTO  [xxx].[ DBO]. [T_test]  VALUES (' Hanmei ')
insert INTO  [xxx].[ DBO]. [T_test]  VALUES (' Lilei ')
insert INTO  [xxx].[ DBO]. [T_test]  VALUES (' Ligang ')
insert INTO  [xxx].[ DBO]. [T_test]  VALUES (' Xiaozhi ') </span>

When you insert it without specifying a display value ID field, the database automatically increases the primary key ID value, and the data in the database is:

3. Delete the data and add the data with the displayed value again

<span style= "FONT-SIZE:24PX;" >delete [xxx]. [dbo]. [t_test] WHERE id = 1</span>
To insert data with a display value:

<span style= "FONT-SIZE:24PX;" >insert into  [guagua_new_event_system_test].[ DBO]. [T_test] VALUES (1, ' xiaoming ') </span>
Database hints:

Message 8101, Level 16, State 1, line 1th
You can specify an explicit value for an identity column in table ' Xxx.dbo.t_test ' only if the column list is used and Identity_insert is on.

When you reset Indentity_insert to ON, re-add, and the SQL statement is:

<span style= "FONT-SIZE:18PX;" > SET  identity_insert [xxx].[ DBO]. [T_test]  On </span>
<span style= "FONT-SIZE:18PX;" > INSERT INTO  [xxx].[ DBO]. [T_test]  VALUES (1, ' xiaoming ') </span>
Database hints:

Message 8101, Level 16, State 1, line 2nd
You can specify an explicit value for an identity column in table ' Xxx.dbo.t_test ' only if the column list is used and Identity_insert is on.
Obviously already set the Indentity_insert to on, but why did not add in, read the SQL Server 2008 Help document, just understand need to develop one by one corresponding column name in the display insert.

So, the correct SQL statement is:

<span style= "FONT-SIZE:18PX;" >set  Identity_insert [xxx].[ DBO]. [T_test]  On 
insert INTO  [xxx].[ DBO]. [T_test] (ID, name)  VALUES (1, ' xiaoming ')
SET identity_insert [xxx].[ DBO]. [T_test] Off</span>

It is only possible to insert a column name when the insertion value is displayed, while opening the Indentity_insert that allows the insertion to be displayed.

------long way to repair, I will be up and down and searching

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.