Method for realizing self growth of ID number in oracle

  • 2021-11-29 16:51:40
  • OfStack

Generate primary key values using sequences.

Sequence (Sequence) is a database object that can be used by multiple users to generate a series of only one numbers. Sequence definitions are stored in a data dictionary. By providing a sequence table with only one value to simplify programming, sequences can be used to automatically generate key values of primary keys. When a sequence is called by a query for the first time, it will return a predetermined value. In each subsequent query, the sequence will produce a value that grows in the specified increment. The sequence can be cyclic or continuously increased until the specified maximum value.
 
-- Create sequence 
create sequence seq_on_test 
increment by 1 
start with 1 
nomaxvalue 
nocycle 
nocache; 

-- Table building  
drop table test; 
create table test( 
ID integer 
,stu_name nvarchar2(4) 
,stu_age number 
); 

-- Insert data  
insert into test values(seq_on_test.nextval,'Mary',15); 
insert into test values(seq_on_test.nextval,'Tom',16); 

select * from test; 

-- Results  
/* 
1 Mary 15 
2 Tom 16 
*/ 

--seq Two methods of  
select seq_on_test.currval from dual; 
select seq_on_test.nextval from dual; 

-- Results  
/* 
2 
3 
*/ 

Related articles: