How to create an auto-increment field in a table in Oracle

Source: Internet
Author: User

How to create an auto-increment field in a table in Oracle
This method uses SEQUENCE (SEQUENCE). For example, the structure of table temp_test is: create table TEMP_TEST.
(
Id number,
Nm varchar (10 ),
Primary key (id) // id is the primary key. The following method sets it as an auto-increment Field
) 1. CREATE an auto-incrementing SEQUENCE TEMP_TEST_IDADD. The name is arbitrary. START from 1 and Add 1 create sequence TEMP_TEST_IDADD increment by 1 start with 1 each time; 2. CREATE a TRIGGER in the table temp_test as follows: create or replace trigger T_TEMP_TEST_IDADD
Before insert on TEMP_TEST
FOR EACH ROW
BEGIN
SELECT TEMP_TEST_IDADD.NEXTVAL INTO: NEW. id from dual;
END T_TEMP_TEST_IDADD; Test Result: add data to the temp_test table and execute the following statement to insert into TEMP_TEST (nm) twice)
Values ('ss'); the result is as follows:

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.