Auto-increment of Oracle primary keys

Source: Internet
Author: User
Oracle primary key auto-increment 1. Compared with the local primary key number type, the ID is the max (ID) to obtain the table each time the data is stored, followed by + 1, the time can be used as the primary key and is unique. 2. The official version uses the sequence mode to increase the primary key. The following describes the procedure. Create test table t [SQL] SQL> Create Table T (2 ID number (10) primary key, 3 name varchar2 (20) not null ); www.2cto.com table created create sequence t_id [SQL] SQL> Create sequence t_id 2 start with 2 -- start with 2 3 increment by 2; -- increase by 2: 1, 3, 5, and 7... sequence created uses sequence [SQL] SQL> insert into T values (t_id.nextval, 'person 1'); 1 row inserted SQL> insert into T values (t_id.nextval, 'person 1 '); 1 row inserted www.2cto.com SQL> INS ERT into T values (t_id.nextval, 'person 1'); 1 row inserted SQL> insert into T values (t_id.nextval, 'person 1 '); 1 row inserted query table t [SQL] SQL> select * from T; ID name ----------- ------------------ 2 persons 1 4 persons 1 6 persons 1 8 persons 1 Delete table data Delete table [SQL] SQL> truncate table t; www.2cto.com table truncated SQL> drop table t; table dropped restore and delete a table [SQL] flashback table t to before drop; Sequence details [SQL] -- sequence/** requirement: when inserting a record, the primary key value, it must be unique and unique, * Manual generation of its values is not applicable, so we need something that can generate values. * This is a sequence, and a sequence is a database object. Syntax for generating sequences: */create sequence [user.] sequence_name [start with N] 1 [increment by N]/* increases by 1, 3 with n = 2, 5 */[maxvalue n | nomaxvalue] [minvalue n | nominvalue] [noorder | order]/* multithreading, single thread */[nocycle] www.2cto.com [cache N]; /* cache */-- delete sequence drop sequence sequence_name; -- change sequence 1 3 5 alter sequence sequence_name increment by 2 maxvalue 80 minvalue 1 order nocycle cache 2;

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.