Oracle database Creation table and primary key auto-increment

Source: Internet
Author: User

In an Oracle database, setting ID auto-increment is not as simple as MySQL, and a sequence needs to be set in Oracle.

Now give the way, specifically as follows:

First step: Create a table

CREATE TABLE Contestdb  (     TID number  () PRIMARY KEY,     teamnum varchar ()  null,      marknum varchar (+)    

Step two: Set ID self-increment

The first way: do not use triggers

1 CREATE SEQUENCE seq_tid;   2 INSERT into Contestdb VALUES (Seq_tid. Nextval, ' B20007009 ', ' B001 ');   3 INSERT into Contestdb VALUES (Seq_tid. Nextval, ' B20007010 ', ' B003 ');  

Second way: Using the trigger Trigger

1--Customizing a sequence2 Create sequence Contestdb_sequence3Increment by 1--every time I add a few, I'm adding 1 each time.4Start with 1--counting starting from 15Nomaxvalue--do not set the maximum value6Nocycle--keep accumulating, not looping7NoCache--do not build buffers8--Create a trigger9 CREATE TRIGGER Contestdb_triggerTen before INSERT on Contestdb One For each ROW AWhen (New. TID isNULL) --start the trigger to generate the TID number only when the TID is empty - begin -Select Contestdb_sequence.nextval into:New.  TID from Sys.dual;  the end;  -    -Insert into CONTESTDB (teamnum,marknum) VALUES (' A20007013 ', ' A002 ');  -    +Insert into CONTESTDB (teamnum,marknum) VALUES (' A20007014 ', ' A003 ');


Oracle database Creation table and primary key auto-increment

Related Article

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.