Oracle CREATE TABLE-related

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.