Quick Search:
CODE
Oracle PL/SQL Code Library
JOBS
Find Or Post Oracle Jobs
FORUM
Oracle Discussion & Chat
 Oracle PL/SQL: SEQUENCE: Demo code and Examples Jump to:  
Category: >> Oracle PL/SQL >> SEQUENCE: Demo code and Examples Bookmark and Share

<< lastnext >>

Snippet Name: SEQUENCE: Demo code and Examples

Description: In Oracle, you can create an autonumber field by using sequences. A sequence is an object in Oracle that is used to generate a number sequence. This can be useful when you need to create a unique number to act as a primary key.

Once you've created a sequence object, to retrieve the next value in the sequence order you need to use NEXTVAL.

Also see:
» SEQUENCE: Find sequence MAX value
» SEQUENCE: Reset a sequence to a predef...
» SEQUENCE: get sequence value into vari...
» SEQUENCE: Autonumber into two tables
» SEQUENCE: Create sequence demo

Comment: (none)

Language: PL/SQL
Highlight Mode: PLSQL
Last Modified: March 14th, 2009

CREATE SEQUENCE <sequence_name>
INCREMENT BY <integer>
START WITH <integer>
MAXVALUE <integer> / NOMAXVALUE
MINVALUE <integer> / NOMINVALUE
CYCLE / NOCYCLE
CACHE <#> / NOCACHE
ORDER / NOORDER; 
 
 
-- basic autonumber with sequence:
INSERT INTO <table_name>
(<column_name>)
VALUES
(<sequence_name>.NEXTVAL);
 
-- basic autonumber with sequence demo.
-- these insert statements will insert 2 new records into 
-- the 'test_bed' table. The user_id field would be assigned 
-- the next number from the seq__user_id sequence. 
 
CREATE SEQUENCE seq_user_id;
 
SELECT seq_user_id.NEXTVAL FROM dual;
 
INSERT INTO test_bed
(user_id, class_type, room_location)
VALUES
(seq_user_id.NEXTVAL, 'Underwater Basketweaving', 'RM1205');
 
SELECT *
FROM test_bed;
 
INSERT INTO test_bed
(user_id, class_type, room_location)
VALUES
(seq_user_id.NEXTVAL, 'Creative Anarchy', 'RM3111');
 
SELECT * FROM test_bed; 
 
 
 


Free
Oracle Magazine
Subscriptions
and Oracle White Papers


SQL University.net courses meet the most demanding needs of the business world for advanced education in a cost-effective manner. SQL University.net courses are available immediately for IT professionals and can be taken without disruption of your workplace schedule or processes.

Compared to traditional travel-based training, SQL University.net saves time and valuable corporate resources, allowing companies to do more with less. That's our mission, and that's what we deliver.

Click here to find out more
 
Home      :      Code Library      :      Sponsors      :      Privacy      :      Terms of Use      :      Contact Us 205 users online    © 2009 psoug.org

PSOUG LOGIN
Username: 
Password: 
Forgot your password?