The primary key ID for an existing table in the Oracle database is set to self-increment

Source: Internet
Author: User
  • Create a table
  • CREATE TABLE T_ (
    USER_ID number NOT NULL primary key,
    User_name VARCHAR2 (30),
    Credits number,
    User_password VARCHAR2 (32),
    Last_visit date,
    Last_ip VARCHAR2 (23)
    )

    1. Create a sequence for this table

    Create sequence T_seq
    MinValue 1
    MaxValue 999999999999999999999999999
    Start with 21
    Increment by 1
    Cache 20;

    1. Create a Trigger

    Create or replace trigger TTR
    Before insert on t

    For each row
    Begin
    Select T_seq.nextval into:new.id from dual;
    End T_tr;

    1. Test

    INSERT into t_user values (null, ' xiaoming ', 123, ' 111 ', sysdate, ' 192.168.37.132 ');
    INSERT into T_user values (6, ' xiaoming ', 124, ' 111 ', sysdate, ' 192.168.37.132 ');
    SELECT * FROM T_user

    The primary key ID for an existing table in the Oracle database is set to self-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.