oracle数据库中由于没有做任何约束,如何删掉表中重复的记录
oracle数据库中由于没有设置任何约束,里面含有很多的重复项。现在的问题是如何删掉这些重复项,重复的记录只保留其中的一条。如下建表语句CREATETABLEmessage_s
oracle数据库中由于没有设置任何约束,里面含有很多的重复项。现在的问题是如何删掉这些重复项,重复的记录只保留其中的一条。如下建表语句
CREATE TABLE message_student3( stu_id INTEGER NOT NULL, --表中没有唯一性约束 stu_number varchar(30), stu_name VARCHAR(10) NOT NULL, stu_age NUMBER(2) NOT NULL ); INSERT INTO message_student3 VALUES(1,'100320036','张珊',18); INSERT INTO message_student3 VALUES(2,'100320037','刘备',19); INSERT INTO message_student3 VALUES(3,'100320038','张飞',19); INSERT INTO message_student3 VALUES(4,'100320039','吕布',19); INSERT INTO message_student3 VALUES(5,'100320040','周瑜',19); INSERT INTO message_student3 VALUES(6,'100320041','孙权',19); INSERT INTO message_student3 VALUES(7,'100320042','张辽',19); INSERT INTO message_student3 VALUES(1,'100320036','张珊',18); INSERT INTO message_student3 VALUES(2,'100320037','刘备',19); INSERT INTO message_student3 VALUES(3,'100320038','张飞',19); INSERT INTO message_student3 VALUES(4,'100320039','吕布',19); INSERT INTO message_student3 VALUES(5,'100320040','周瑜',19); INSERT INTO message_student3 VALUES(6,'100320041','孙权',19); INSERT INTO message_student3 VALUES(7,'100320042','张辽',19);分析:由于表没有做约束,所以重复记录的各个字段必然是相等的,然而对于删除操作要么是全部删除要么是根据一个条件删除,前者肯定是不行,后者呢,一删除就重复的都删除了。所以要删掉其中重复项的切入点必须要有一个可以区别不同记录的量。想想在做oracle的分页的时候,用到了rownum,于是想用rownum来试一下。但是每一个新的select语句都有一个新的rownum,用别名就是无法之前的,这么写可能有些含糊,于是在网上找了一些有关这方面的资料,发现其实我应该要用的是rowid。rownum和rowid有什么不同呢?下面是摘抄的内容:原地址是:
rownum和rowid都是伪列,但是两者的根本是不同的,rownum是根据sql查询出的结果给每行分配一个逻辑编号,所以你的sql不同也就会导致最终rownum不同,
但是rowid是物理结构上的,在每条记录insert到数据库中时,都会有一个唯一的物理记录 (不会变),,
例如 AAAMgzAAEAAAAAgAAB 7499 ALLEN SALESMAN 7698 1981/2/20 1600.00 300.00 30
这里的AAAMgzAAEAAAAAgAAB物理位置对应了这条记录,这个记录是不会随着sql的改变而改变。
因此,这就导致了他们的使用场景不同了,通常在sql分页时或是查找某一范围内的记录时,我们会使用rownum。
1、rownum
例如:
查找2到10范围内的记录(这里包括2和10的记录)
查找前三名的记录
select * from emp a where rownum 例如:
2、rowid
我们在处理一张表中重复记录时经常用到他
因此我们可以根据rowid来处理,rowid具有唯一性,查询时效率是很高的,
回到最开始的问题,要删掉其中重复的使用下面的语句就可了
这样就可以了。
当然你也可以用一个很原始的方法,就是将有重复记录的表中的数据导到另外一张表中,最后再倒回去。
create table stu_tmp as select distinct* from stu; truncate table sut; //清空表记录 insert into stu select * from stu_tmp; //将临时表中的数据添加回原表但是要是stu的表这种只适合数量级不大的情况下。
本文出自 “菜鸟也想飞” 博客,请务必保留此出处

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



MySQL is an open source relational database management system, mainly used to store and retrieve data quickly and reliably. Its working principle includes client requests, query resolution, execution of queries and return results. Examples of usage include creating tables, inserting and querying data, and advanced features such as JOIN operations. Common errors involve SQL syntax, data types, and permissions, and optimization suggestions include the use of indexes, optimized queries, and partitioning of tables.

MySQL is chosen for its performance, reliability, ease of use, and community support. 1.MySQL provides efficient data storage and retrieval functions, supporting multiple data types and advanced query operations. 2. Adopt client-server architecture and multiple storage engines to support transaction and query optimization. 3. Easy to use, supports a variety of operating systems and programming languages. 4. Have strong community support and provide rich resources and solutions.

In Oracle, the FOR LOOP loop can create cursors dynamically. The steps are: 1. Define the cursor type; 2. Create the loop; 3. Create the cursor dynamically; 4. Execute the cursor; 5. Close the cursor. Example: A cursor can be created cycle-by-circuit to display the names and salaries of the top 10 employees.

SQL statements can be created and executed based on runtime input by using Oracle's dynamic SQL. The steps include: preparing an empty string variable to store dynamically generated SQL statements. Use the EXECUTE IMMEDIATE or PREPARE statement to compile and execute dynamic SQL statements. Use bind variable to pass user input or other dynamic values to dynamic SQL. Use EXECUTE IMMEDIATE or EXECUTE to execute dynamic SQL statements.

An AWR report is a report that displays database performance and activity snapshots. The interpretation steps include: identifying the date and time of the activity snapshot. View an overview of activities and resource consumption. Analyze session activities to find session types, resource consumption, and waiting events. Find potential performance bottlenecks such as slow SQL statements, resource contention, and I/O issues. View waiting events, identify and resolve them for performance. Analyze latch and memory usage patterns to identify memory issues that are causing performance issues.

The method to solve the Oracle cursor closure problem includes: explicitly closing the cursor using the CLOSE statement. Declare the cursor in the FOR UPDATE clause so that it automatically closes after the scope is ended. Declare the cursor in the USING clause so that it automatically closes when the associated PL/SQL variable is closed. Use exception handling to ensure that the cursor is closed in any exception situation. Use the connection pool to automatically close the cursor. Disable automatic submission and delay cursor closing.

Deleting all data in Oracle requires the following steps: 1. Establish a connection; 2. Disable foreign key constraints; 3. Delete table data; 4. Submit transactions; 5. Enable foreign key constraints (optional). Be sure to back up the database before execution to prevent data loss.

The steps to open an Oracle database are as follows: Open the Oracle database client and connect to the database server: connect username/password@servername Use the SQLPLUS command to open the database: SQLPLUS
