Insert a specific value into the Oracle ID column

Source: Internet
Author: User

You have some knowledge about the Oracle ID column. The following describes how to insert a specific value in the Oracle ID column. If you are interested in this, take a look.

Although you can use the value of the Oracle identity column and any value of the column for thousands of reasons, however, some people who work with you will insist on using continuous primary key words in the given table ). Then, when the invoice number is lost, they will be panic, afraid of being sued, cover up errors, or even worse.

To solve this problem, you can create a table with the Oracle ID column and fill it with some data rows:

 
 
  1. -- Create a test table.  
  2. CREATE TABLE TestIdentityGaps  
  3. (  
  4. ID int IDENTITY PRIMARY KEY,  
  5. Description varchar(20)  
  6. )  
  7. GO  
  8. -- Insert some values. The word INTO is optional:  
  9. INSERT [INTO] TestIdentityGaps (Description) VALUES ('One')  
  10. INSERT [INTO] TestIdentityGaps (Description) VALUES ('Two')  
  11. INSERT [INTO] TestIdentityGaps (Description) VALUES ('Three')  
  12. INSERT [INTO] TestIdentityGaps (Description) VALUES ('Four')  
  13. INSERT [INTO] TestIdentityGaps (Description) VALUES ('Five')  
  14. INSERT [INTO] TestIdentityGaps (Description) VALUES ('Six')  
  15. GO  

Delete several data rows:

 
 
  1. DELETE TestIdentityGaps  
  2. WHERE Description IN('Two', 'Five')  

When we write code, we know that the values "Two)" and "Five)" are lost. We want to insert two data rows to fill these gaps. Two simple INSERT statements cannot meet the requirements. However, they create primary keywords at the end of the sequence.

 
 
  1. INSERT [INTO] TestIdentityGaps (Description) VALUES ('Two Point One')  
  2. INSERT [INTO] TestIdentityGaps (Description) VALUES ('Five Point One')  
  3. GO  
  4. SELECT * FROM TestIdentityGaps  

You cannot explicitly set the value of the ID column either:

 
 
  1. -- Try inserting an explicit ID value of 2. Returns a warning.  
  2. INSERT INTO TestIdentityGaps (id, Description) VALUES(2, 'Two Point One')  
  3. GO  

To solve this problem, SQL Server 2000 uses IDENTITY_INSERT for settings. To forcibly insert a data line with a specific value, you need to issue a command to connect the specific inserted content to the end:

 
 
  1. SET TestIdentityGapsON  
  2. INSERT INTO TestIdentityGaps (id, Description) VALUES(2, 'Two Point One')  
  3. INSERT INTO TestIdentityGaps (id, Description) VALUES(5, 'Five Point One')  
  4. GO  
  5. SELECT * FROM TestIdentityGaps  

Now you can see that the new data row has been inserted with the specified primary key value.

Note: the setting of IDENTITY_INSERT can be used in a table in the database at any specific time. To fill the gaps in one or more tables, you must use specific commands to specify each table.

You can insert a specific value in a table with an ID column. To do this, you must first set the value of IDENTITY_INSERT to ON. If you do not have one, you will see an error message. Even if you set the value of IDENTITY_INSERT to ON, if you insert another existing value, you will still see the error message.

Oracle command line custom editor vi

Implementation of oracle command line Logon

Syntax for creating views in Oracle

Create tables and indexes in Oracle

This section describes Oracle partition indexes in detail.

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.