1 --Create a table2 Create TablePerson (3Id Number Primary Key,4Namevarchar2( +),5 Birth date6 );7 --Create a sequence8 Createsequence Person_id_seq9Increment by 1TenStart with 1 OneNomaxvalue--do not set the maximum value ANocycle--keep accumulating, not looping -CacheTen; - --Create a trigger the Create or Replace TriggerPerson_id_tri beforeInsert on Person - forEach row - Declare -V_newval Number( A) := 0; +V_incval Number( A) := 0; - BEGIN + IFINSERTING and: new.id is NULL Then A SELECTPerson_id_seq. Nextval intoV_newval fromDUAL; at --If This is the first time this table has been inserted into (sequence = = 1) - IFV_newval= 1 Then - --get the max indentity value from the table - SELECTNVL (Max(ID),0) intoV_newval fromPerson ; -V_newval:=V_newval+ 1; - --set the sequence to that value in LOOP - EXIT whenV_incval>=V_newval; to SELECTPerson_id_seq.nextval intoV_incval fromdual; + ENDLOOP; - END IF; the --used to emulate last_insert_id () * --mysql_utilities.identity: = V_newval; $ --assign the value from the sequence to emulate the identity columnPanax Notoginseng: new.id:=V_newval; - END IF; the END; + A --simple trigger, there is a problem with the trigger, the sequence is skipped the Create or Replace TriggerPerson_id_tri beforeInsert on Person + forEach row when(new.id is NULL) - BEGIN $ SelectPerson_id_seq.nextval into: new.id fromdual; $ End; - - --Insert an instance the Insert intoPerson (name, birth)Values('Ceshi', sysdate); - --the wrong time formatWuyi Insert intoPerson (Name,birth)Values('hehe','2015-06-02 00:00:00'); the --the correct insertion date - Insert intoPerson (Name,birth)Values('hehe', To_date ('2005-01-01 13:14:20','YYYY-MM-DD HH24:mi:ss')); Wu Insert intoPerson (Name,birth)Values('hehe', To_date ('2005-01-01','YYYY-MM-DD')); - About --formatting dates in Oracle $ SelectTo_date ('2005-01-01 13:14:20','YYYY-MM-DD HH24:mi:ss') fromdual; - - --query Table - Select * fromPerson ; A + --TRUNCATE TABLE the truncate TablePerson
Oracle CREATE TABLE-related