Resolving conflicts with old data IDs when Oracle inserts data

Source: Internet
Author: User

We need to make him self-increment when using Oracle to create a primary key, but unlike MySQL, you need to create a sequence that looks like this:

You can delete the previously created sequence, and we re-create one:

DROP SEQUENCE sjgxqk_sequence; --Delete

Create sequence Sjgxqk_sequence--(Sjgxqk_sequence, this represents the name of your sequence)
INCREMENT by 1--add a few each time
Start with 1-counting starting from 1
Nomaxvalue--Do not set the maximum value
Nocycle--keep accumulating, not looping
CACHE 10; --cache can be used to improve the acquisition speed of sequential values, but it is possible to waste some serial numbers, and the cache size should be set according to the actual situation. This specific I do not know how to use, refer to others to share, haha

Then query under:

Select Sjgxqk_sequence.nextval from dual; Look at him. What is the ID of the next auto-insert?

It is possible that the result value is Nextval = 1; This means that he will automatically insert the ID from "1".

Before this we may have old data, this ID does not necessarily start from 1, it may have been sorted to 10, 20.

At this point we need to change the starting value of his:

alter sequence sjgxqk_sequence increment by; (from 14 onwards, this 14 is based on your needs)

alter sequence sjgxqk_sequence increment by 1; (Increment one at a time)

So after that they won't be prompting us: violating the unique constraints haha haha ~

Welcome reprint, Mutual encouragement.

End

Resolving conflicts with old data IDs when Oracle inserts data

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.