Example tutorial for creating a self-added primary key in an Oracle database _oracle

Source: Internet
Author: User

When designing a database table, it was found that Oracle did not have a set of self-adding primary keys, and Google learned that Oracle itself does not support the Sequence key, which needs to be implemented through sequences (Trigger) and triggers.
CREATE TABLE Student

Create Table Student ( 
 ID number () primary key, the 
 name Varchar2 ( 
 3) 
 that implements the ID by sequence and trigger, Sex number (1) 
) 

Create a sequence sequence

Create Sequence seq_student 
minvalue 1 
maxvalue 99999999999999999999 start with 
1   --Starting 
from 1 Increment by 1--increment to 1 
cache 0 Order 
; 

Creating triggers Trigger

Create or Replace Trigger student_autoincrement 
before Insert on STUDENT for each 
Row 
, New.id is NULL 
Begin 
Select seq_student. Nextval INTO:NEW.ID from DUAL; 
End; 

Note the point:

1: A sequence can be shared by multiple tables.

2: The number of sequence generated by multiple tables is always continuous and will not start again.

3: Remove sequence If no longer in use.

SELECT * from DAYSBFJ. DAYS_CARD_UPDATE3 ORDER BY ID ASC
--alter table DAYSBFJ. Days_card_update3 Add Source_flag varchar2 (2);
--create sequence days_card_update2_seq_id minvalue 1 maxvalue 999999999 start with 1;
--update DAYSBFJ. Days_card_update2 Set id = days_card_update2_seq_id.nextval;
--update DAYSBFJ. Days_card_update3 Set source_flag = ' 2 '

Another example:

Create a new table with missing primary key

CREATE TABLE Test1 (name1 varchar2 (), City Varchar2 (40));

--Inserting data

INSERT into test1 values (' name1 ', ' Nanjing ');
INSERT into test1 values (' name1 ', ' Nanjing ');
INSERT into test1 values (' name2 ', ' nanjing1 ');
INSERT into test1 values (' Name3 ', ' nanjing2 ');
INSERT into test1 values (' name4 ', ' nanjing3 ');
INSERT into test1 values (' name5 ', ' Nanjing4 ');
INSERT into test1 values (' Name6 ', ' nanjing5 ');
INSERT into test1 values (' Name7 ', ' Nanjing6 ');
INSERT into test1 values (' Name8 ', ' Nanjing7 ');
INSERT into test1 values (' name9 ', ' Nanjing8 ');
INSERT into test1 values (' Name10 ', ' nanjing9 ');
INSERT into test1 values (' Name10 ', ' nanjing9 ');
INSERT into test1 values (' name12 ', ' nanjing11 ');
INSERT into test1 values (' name13 ', ' nanjing12 ');
INSERT into test1 values (' name14 ', ' nanjing13 ');
Commit

--Increase primary Key ID

ALTER TABLE TEST1 Add ID number (10);

--set sequence to make ID self-increasing

Create sequence seq_id
 minvalue 1
 maxvalue 999999999
 start with 1;

--Set the value of the ID to sequence

Update test1 set id=seq_id.nextval;
Commit

--Set ID as primary key

ALTER TABLE TEST1
 add constraint Pk_test1 primary key (ID);
 
Select Id,name1,city from TEST1;

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.