Oracle uses triggers to implement auto-increment columns, and oracle uses triggers

Source: Internet
Author: User

Oracle uses triggers to implement auto-increment columns, and oracle uses triggers

Oracle uses triggers to implement auto-increment Columns

 

Oracle does not have the auto-increment function. mysql and sqlserver use auto_increment and identity () to implement auto-increment respectively. Oracle can only be implemented through sequences. During each insertion, it is inconvenient to display the sequence value to the auto-increment column. The trigger is used here, in this way, the three databases are syntactically consistent during insertion, facilitating DAO code porting. The following is an ORACLE implementation example. The steps are as follows:
                                                                                                                                      

1. Create a table

Create tabletest_user (

User_id number (10, 0) primary key,

User_name varchar2 (40)

);

 

2. Create a sequence

Create sequencetest_user_seq start with 1 maxvalue 9999999999 increment by 1;

 

3. Create a trigger

Create or replace

Triggertest_user_trigger

Before insert ontest_user

For each row

Begin

Selecttest_user_seq.nextval into: new. user_id from dual;

End;

4. insert data

Insert into test_user (user_name) values ('Tom ');

Insert into test_user (user_name) values ('jack ');

 

5. Test Results

 

Select * from test_user;

 

 

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.