oracle achieve increment primary key id

The company now projects database using oracle, oracle table primary key increment to achieve than mysql trouble

mysql auto_increment primary key table to tick. oracle properties did not change, it is relatively cumbersome. Hereby record what increment method

Test case as follows

The first step to create a test table table1

sql statement

create table table1
id int not null,
name varchar2(20),
sex varchar2(4)

Add Notes to Table, Field Comment

comment on table table1 is 'test table will be deleted later'
the Comment column IS ON 'name'
the Comment column IS ON 'gender'

Step 2: Create a sequence

Sequence table1_id the Create
MINVALUE 1 // increment field minimum
nomaxvalue // maximum is not even NOMAXVALUE
INCREMENT each value by 1 // 1
Start with 1 // start value
nocache; // Do not cache

Step 3: Create trigger

create or replace trigger table1_tg_insertId
before insert on table1 for each row
select table1_id.nextval from dual;

Step four: two insert test data starts

insert into table1(name,sex) values ('zhangsan','nan');
insert into table1(name,sex) values ('lisi','nan');


Query data


Guess you like