Data cleansing Note: Generation of primary key class ID Fields

Source: Internet
Author: User

Data cleansing Note: Generation of primary key class ID Fields

 

Background]

After data is extracted from the source end and inserted to the target end, a primary key ID field needs to be created.

 

[Solution]

Method 1: Use the sys_guid () function to generate a 32-bit Note Number

For example, in the emp table, create a field and set the default value of the field to automatically generate a 32-bit random number with the sys_guid () function.

Alter table EMP add ID varchar2 (32) default sys_guid ();

 

Method 2: Use a trigger to insert an auto-incrementing Sequence

For example, before inserting a new piece of data into the emp1 table, insert an ID value. Specifically, seq_idadd is a sequence that has been created in advance. You can customize the starting value and difference value of the sequence.

Create or replace trigger tr_IDADD

Before insert on emp1

For each row

Begin

Select seq_idadd.nextval into: new. id from dual;

End;

 

Method 3: Splicing

For example, an ID Primary Key is formed by splicing the source business fields. For example:

Source ID|To_char (sysdate, 'yyyymmdd ')|Lpad (NEXTNUM. Nextval, 10, 0)

Assume that the source ID is 12 bits. In this way, a 12 + 8 + 10 = 30-bit primary key number is intercepted.

 

[Experiment]

Use sys_guid to obtain the ID value, as shown below:

With the trigger, ID increments by 1 for each new piece of data, as shown below:

 

Obtain the primary key ID by splicing, as shown below:

Small knowledge, easy to remember.

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.