An example of an SQL statement that automatically adds an ID to an Oracle database

Source: Internet
Author: User

The main content of this article includes: Implement Auto-incremental ID in Oracle and delete repeated records in the data table.

I. Auto-incrementing ID


1. First create sequence

Create sequence seqmax increment by 1

2. Get an ID

Select seqmax. nextval ID from dual

3. to delete a sequence

Drop sequence seqmax;


2. Delete duplicate records in a data table


1. Create a table first

Create TABLE "APPTEST "(

"ID" INTEGER primary key not null,

"MOBILE" nvarchar2 (50) NOT NULL

);

2. Assume that there are a large number of duplicate mobile phone numbers. To delete duplicate records, you can use either of the following methods:

(1) simple deletion using rowid

Delete from APPTEST a where rowid not in (select max (rowid) from APPTEST B where a. mobile = B. mobile );

It is said that this method is inefficient when the data volume is large.

(2) using analysis functions

Delete APPTEST where rowid in (

Select rid from

(Select rowid rid, row_number () over (partition by mobile order by id desc) rn from APPTEST)

Where rn> 1 );

(3) create a temp table

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.