Navicat: Set the Oracle database primary key to increase automatically

1. Create the following table

        Oracle database is different from Mysql and Sql Server databases. Oracle database primary key auto-increment cannot be set directly when creating a table, but needs to be set through sequences and triggers!

2. Create a sequence

create sequence SEQ_DEVICEDATAINFO  
start with 1                        
increment by 1                  
maxvalue  99999999               
nocycle       
cache 10;

In the above statement:

create sequence SEQ_DEVICEDATAINFO     --SEQ_DEVICEDATAINFO   is the sequence name
start with 1                                                        --indicates that it starts from 1
increment by 1                                                  --indicates that the step size is 1, if it is 2, add 2 each time
maxvalue 99999999                                         --indicates the maximum value, this line can Don’t write, go on indefinitely
nocycle                                                              -- means don’t cycle
cache 10;                                                          -- means cache

In addition to using statements, you can also create it through tools in the Navicate interface , as shown below:

 3. Create triggers

create or replace trigger SEQ_DEVICEDATAINFO  
before insert on "DeviceDataInfo" for each row 
begin 
	select SEQ_DEVICEDATAINFO.nextval into :new."id" from dual; 
end;
 

in:

SEQ_DEVICEDATAINFO is the sequence

"DeviceDataInfo" is the table name

"id" is an auto-incremented field in the table 

 As shown below:

 At this point, open the trigger in the design table and you can see that the following trigger has been successfully added:

         In this way, the primary key auto-increment in the Oracle database has been successfully set up. You can try to insert data and you will find that the primary key ID increases in steps of 1, as shown below:

Guess you like

Origin blog.csdn.net/sssxlxwbwz/article/details/125365784