Auto-increment of fields created in oracle -- two implementation methods are summarized: oracle Fields
Mysql and other databases have the function of Automatically increasing table IDs with the insertion of records, while oracle does not. We have the followingTwo TypesThis method can solve the field auto-increment function.
Because both methods need to be implemented by creating sequences, we will first provideSequence Creation Method.
Create sequence name [increment by n] [start with n] [{MAXVALUE/MINVALUE n | NOMAXVALUE}] [{CYCLE | NOCYCLE}] [{CACHE n | NOCACHE}];
Analysis:
1) increment by is used to define the sequence step. If omitted, the default value is 1. If a negative value is displayed, the value of the Oracle sequence decreases according to this step.
2) start with defines the initial value of the sequence (that is, the first value generated). The default value is 1.
3) MAXVALUE defines the maximum value that can be generated by the sequence generator. The "NOMAXVALUE" option is the default option, indicating that no maximum value is defined. In this case, the maximum value generated by the system for an incremental Oracle sequence is the 27 power of 10. For a descending sequence, the maximum value is-1.
4) MINVALUE defines the minimum value generated by the sequence generator. The "NOMAXVALUE" option is the default option, indicating that there is no minimum value defined. What is the minimum value that the system can produce for the descending sequence? 10 to the power of 26; for incremental sequence, the minimum value is 1.
5) CYCLE and NOCYCLE indicate whether to CYCLE when the value of the sequence generator reaches the limit value. CYCLE indicates loop, and NOCYCLE indicates no loop. If there is a loop, when the ascending sequence reaches the maximum value, it loops to the minimum value. When the descending sequence reaches the minimum value, it loops to the maximum value. If there is no loop, an error occurs when a new value is generated after the limit value is reached.
6) CACHE (buffer) defines the size of the memory block for storing the sequence. The default value is 20. NOCACHE indicates no memory buffer for the sequence. Buffer the sequence memory to improve the sequence performance.
Solution 1: sequence + trigger
The specific implementation method is as follows:
Step 1: Create a sequence
-- Create sequencecreate sequence SEQ_T_RECVminvalue 1maxvalue 9999999start with 1increment by 1cache 50;
Step 2: Create a table
-- Create tablecreate table RECV_MSG( id NUMBER, messageid VARCHAR2(32), contents VARCHAR2(2000), app_flg VARCHAR2(100), phonenumber VARCHAR2(2000), updatetime DATE default sysdate);
Step 3: Create a trigger
CREATE OR REPLACE TRIGGER "recv_trig" BEFORE INSERT ON recv_msg REFERENCING OLD AS OLD NEW AS NEW FOR EACH ROWDECLAREBEGIN SELECT SEQ_T_RECV.NEXTVAL INTO :NEW.ID FROM DUAL;END recv_trig;
Step 4: Test and confirm
Insert a data entry to the table to check whether the id field is automatically increased.
Solution 2: sequence + display Call Sequence
1. Create sequence
create sequence seq_on_test increment by 1 start with 1 nomaxvalue nocycle nocache;
Ii. Create a table
-- Create a table drop table test; create table test (ID integer, stu_name nvarchar2 (4), stu_age number );
3. insert data (the next value of the call sequence is displayed)
-- Insert into test values (seq_on_test.nextval, 'Mary ', 15); insert into test values (seq_on_test.nextval, 'Tom', 16 );
Iv. View
Select * from test; -- result/* 1 Mary 15 2 Tom 16 */
Note: view the current and next values of a sequence
-- Two methods of seq: select seq_on_test.currval from dual; select seq_on_test.nextval from dual; -- result/* 2 3 */
Summary
It is easier to add a trigger directly than to display the call. We do not need to use any field to obtain the next value through which sequence, but use the trigger to add the execution.