[Oracle11g auto-increment primary key], oracle11g primary key

Source: Internet
Author: User

[Oracle11g auto-increment primary key], oracle11g primary key

Oracle Auto-incrementing primary keys must be implemented using sequences and triggers. First, we create a table where the primary key is int to implement auto-incrementing.
CreatetableTEST
(
ID intPRIMARYKEY,
Test1 varchar2 (20 ),
Test2 varchar2 (20 ),
Test3 varchar2 (20 ))

Create a sequence:
Create sequence SEQ_TEST
Minvalue 1 -- Minimum value
Maxvalue 999999999999999999999999999 -- maximum value
Start with 1 --- start from 1
Increment by 1 --- add 1 each time
Nocycle -- always accumulate without repeating nocache;

Create a trigger to implement auto-increment of the primary key when inserting data:

Create or replace trigger tg_test
// Test table name, id primary key name, seq_test.nextval Sequence
Before insert on testFOR each row when (new. id is null)
Beginselect seq_test.nextval into: new. id from dual;
End;
When the primary key of the inserted data is null, the sequence is used to generate the auto-incrementing primary key.
// ------------------ Trigger Style
Create or replace trigger TRI_SCORE
BEFORE INSERT OR UPDATE ON XUANKE
FOR EACH ROW
BEGIN
// If score is empty, score = 0
IF: new. score is null then: new. SCORE: = 0;
End if;
END;

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.