How to specify table field self-increment in Oracle

Source: Internet
Author: User

Background introduction:

SQL Server can add an identity (1) After a field of type int, and the field will be increased by +1, and this field will be set as the primary key, enabling us to insert the data. You can use "auto_increment" in MySQL. But Oracle is a bit cumbersome and needs to use sequences and triggers to achieve its purpose.

--School Table

CREATE TABLE School (SID number (4), sname varchar2 (), check_status number (1) Default 0 check (Check_status in (0,1)), Accountant VARCHAR2 (+ char), pwd varchar2 (+ char), email varchar2 (+ char), Photo_path varchar2 (+ char),
Constraint Pk_t_school primary KEY (SID));


--Sequence (sequence and trigger to implement automatic growth of Sid field in school table)
Create sequence Shool_sid_autoinc
MinValue 1
MaxValue 9999999999999999999999999999
Start with 1
Increment by 1
NoCache


--Triggers (sequence and trigger implementations school the SID field in the table)
Create or Replace Trigger Insert_shool_sid_autoinc
Before insert on school
For each row
Begin
Select Shool_sid_autoinc.nextval Into:new.sid from dual;

End

/

--Test results

Sql> INSERT into school values (+, ' a ', 0,001,001, ' [email protected] ', 66666);


1 row created.


Sql> INSERT into school values (+, ' aaaaaaaaaaaaaaaaaaaaa ', 0,001,001, ' [email protected] ', 66666);
1 row created.


Sql> select Sid from School;


Sid
----------
1
2

Ok, end. Reprint please indicate the source

How to specify table field self-increment in Oracle

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.