Oracle self-growth field implementation

Source: Internet
Author: User

First, you must have a table!
Create Table example (
ID number (4) not null primary key,
Name varchar (25 ),
Phone varchar (10 ),
Address varchar (50)

);

Then, you need a custom Sequence
Create sequence emp_sequence
Increment by 1 -- add several
Start with 1 -- count from 1
Nomaxvalue -- do not set the maximum value
Nocycle -- always accumulate without repeating
Nocache -- do not create a buffer
The above code completes the establishment of a sequence, named emp_sequence. The range is from 1 to infinitely large (the degree of infinity is determined by your machine ), nocycle decides not to loop. If you set the maximum value, you can use cycle to make seq reach the maximum and then loop. for nocache, by the way, if you provide the cache value, the system will automatically read the seq of your cache value.
In this way, the operation speed will be accelerated during repeated operations. However, if an unexpected situation occurs, such as when the machine or Oracle is dead, the seq value retrieved next time will be inconsistent with the previous one. (If the connection is not coherent, we recommend using cache because time is money! Run the question !)
You only have tables and sequences. You also need a trigger to execute them! The Code is as follows:
Create trigger "trigger name" before
Insert on example for each row when (New. ID is null)
Begin
Select emp_sequence.nextval into: New. ID from dual;
End;
Close the work! Next, try to insert data!
Insert into example (name, phone, address) values ('cao ', '000000', 'heibei ');
 

========================================================== ==================================

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.