Oracle implements a method similar to adding a field in SQLServer

Source: Internet
Author: User
Since Oracle does not have auto-increment fields similar to SQL Server, if we want to set a unique column similar to ID, we need to create a sequence first with the help of Oracle sequence, then, each time data is inserted, the ID value is updated through the previous trigger, and the sequence number is added with 1.

Since Oracle does not have auto-increment fields similar to SQL Server, if we want to set a unique column similar to ID, we need to create a sequence first with the help of Oracle sequence, then, each time data is inserted, the ID value is updated through the previous trigger, and the sequence number is added with 1.

The related code is as follows:
1. Create sequence:
The Code is as follows:
Create sequence SEQU_DATA_DATAINFO increment by 1 start with 1 nocycle nocache noorder;

2. Create a trigger:
The Code is as follows:
Create or replace TRIGGER TRIG_TEST
Before insert on TABLE1
FOR EACH ROW
DECLARE
TmpVar NUMBER;
BEGIN
TmpVar: = 0;
Select SEQU_PROC_ASSOCIATEINFO.NextVal into tmpVar from dual;
: New. procid: = tmpVar;
EXCEPTION
WHEN OTHERS THEN
-- Consider logging the error and then re-raise
RAISE;
END;

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.