Oracle Auto-incrementing Sequence

Source: Internet
Author: User

-- 1. Create a dedicated user under PLSQL
-- Create the user
Create user user1
Identified by \ "user1 \"
Default tablespace users
Temporary tablespace temp
Profile default;
-- Grant/Revoke role privileges
Grant DBA to user1;
-- Grant/Revoke SYSTEM privileges
Grant unlimited tablespace to user1;
------------------------------------

------------------------------------
-- Create a table
Create Table AAA (
Oid number not null,
Type Number (3)
);

 

The following sections handle auto-incrementing sequence Problems

-- Auto-incrementing Sequence
-- Step 1: Create a queue
Create SequenceAaa_oid_ 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;

-- Step 2: Create a trigger
Create or replace triggerAaa_oid_trigger
Before insert onAaaFor each row
Declare
Next_id number;
Begin
-- Get the next ID number from the sequence
SelectAaa_oid_ Sequence. Nextval into next_id from dual;
-- Use the sequence number as the primarykey
-- For there cord being inserted.
: New.Oid: = Next_id;
End;

 

Test:

Insert into AAA (type) values (2 );

Select * from AAA;

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.