To create a self-increasing ID field in an Oracle database _oracle

Source: Internet
Author: User

The steps to use the self-increasing ID field in Oracle are cumbersome, as summarized below:

--Create a table

CREATE TABLE ADVICE
(
ID INT not NULL,
ACTIVE INT DEFAULT 1 is not NULL,
TYPE int. NOT NULL, 
MSG VARCHAR2 ( ) not NULL,
ADVICE VARCHAR2 (4000) is not NULL,
PRIMARY KEY (ID),
CONSTRAINT Advice_uni UNIQUE (type,msg)
) Tablespace MYDB;

--Create your own ID, Name: Table name _ Field name _seq
CREATE SEQUENCE advice_id_seq minvalue 1 nomaxvalue INCREMENT by 1 START with 1 nocache;
  
   --creates a trigger for the INSERT operation without writing a nextval in the SQL statement, named table name _INS_TRG
Create OR REPLACE TRIGGER ADVICE_INS_TRG before INSERT on ADVICE for each ROW, New.id is NULL,
BEGIN
SELECT advice_id_seq. Nextval INTO:NEW.ID from DUAL;
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.