logo

SQL에서 중복 행을 삭제하는 방법은 무엇입니까?

이 섹션에서는 중복 행을 삭제하는 다양한 방법을 알아봅니다. MySQL과 오라클 . 만약 SQL 테이블에 중복 행이 포함되어 있으면 중복 행을 제거해야 합니다.

샘플 데이터 준비

스크립트는 다음과 같은 테이블을 생성합니다. 콘택트 렌즈 .

 DROP TABLE IF EXISTS contacts; CREATE TABLE contacts ( id INT PRIMARY KEY AUTO_INCREMENT, first_name VARCHAR(30) NOT NULL, last_name VARCHAR(25) NOT NULL, email VARCHAR(210) NOT NULL, age VARCHAR(22) NOT NULL ); 

위의 표에는 다음과 같은 데이터가 삽입되어 있습니다.

자바의 인스턴스
 INSERT INTO contacts (first_name,last_name,email,age) VALUES ('Kavin','Peterson','[email protected]','21'), ('Nick','Jonas','[email protected]','18'), ('Peter','Heaven','[email protected]','23'), ('Michal','Jackson','[email protected]','22'), ('Sean','Bean','[email protected]','23'), ('Tom ','Baker','[email protected]','20'), ('Ben','Barnes','[email protected]','17'), ('Mischa ','Barton','[email protected]','18'), ('Sean','Bean','[email protected]','16'), ('Eliza','Bennett','[email protected]','25'), ('Michal','Krane','[email protected]','25'), ('Peter','Heaven','[email protected]','20'), ('Brian','Blessed','[email protected]','20'); ('Kavin','Peterson','[email protected]','30'), 

우리는 스크립트를 실행하여 테스트 데이터를 다시 생성합니다. 삭제 성명 .

쿼리는 연락처 테이블의 데이터를 반환합니다.

 SELECT * FROM contacts ORDER BY email; 

ID 이름 이메일 나이
7 반스 [이메일 보호됨] 이십 일
13 브라이언 축복 받은 [이메일 보호됨] 18
10 엘리자 베넷 [이메일 보호됨] 23
1 캐빈 피터슨 [이메일 보호됨] 22
14 캐빈 피터슨 [이메일 보호됨] 23
8 미샤 바튼 [이메일 보호됨] 이십
열하나 남자 이름 [이메일 보호됨] 17
4 남자 이름 잭슨 [이메일 보호됨] 18
2 건강 상태 요나스 [이메일 보호됨] 16
베드로 천국 [이메일 보호됨] 25
12 베드로 천국 [이메일 보호됨] 25
5 [이메일 보호됨] 이십
9 [이메일 보호됨] 이십
6 빵 굽는 사람 [이메일 보호됨] 30

다음 SQL 쿼리는 연락처 테이블에서 중복된 이메일을 반환합니다.

 SELECT email, COUNT(email) FROM contacts GROUP BY email HAVING COUNT (email) > 1; 

이메일 개수(이메일)
[이메일 보호됨] 2
[이메일 보호됨] 2
[이메일 보호됨] 2

우리는 세 개의 행을 가지고 있습니다 복제하다 이메일.

자바 트림 문자열

(A) DELETE JOIN 문을 사용하여 중복 행을 삭제합니다.

 DELETE t1 FROM contacts t1 INNERJOIN contacts t2 WHERE t1.id <t2.id and t1.email="t2.email;" < pre> <p> <strong>Output:</strong> </p> <pre> Query OK, three rows affected (0.10 sec) </pre> <p>Three rows had been deleted. We execute the query, given below to finds the <strong>duplicate emails</strong> from the table.</p> <pre> SELECT email, COUNT (email) FROM contacts GROUP BY email HAVING COUNT (email) &gt; 1; </pre> <p>The query returns the empty set. To verify the data from the contacts table, execute the following SQL query:</p> <pre> SELECT * FROM contacts; </pre> <br> <table class="table"> <tr> <td>id</td> <td>first_name</td> <td>last_name</td> <td>Email</td> <td>age</td> </tr> <tr> <td>7</td> <td>Ben</td> <td>Barnes</td> <td> [email protected] </td> <td>21</td> </tr> <tr> <td>13</td> <td>Brian</td> <td>Blessed</td> <td> [email protected] </td> <td>18</td> </tr> <tr> <td>10</td> <td>Eliza</td> <td>Bennett</td> <td> [email protected] </td> <td>23</td> </tr> <tr> <td>1</td> <td>Kavin</td> <td>Peterson</td> <td> [email protected] </td> <td>22</td> </tr> <tr> <td>8</td> <td>Mischa</td> <td>Barton</td> <td> [email protected] </td> <td>20</td> </tr> <tr> <td>11</td> <td>Micha</td> <td>Krane</td> <td> [email protected] </td> <td>17</td> </tr> <tr> <td>4</td> <td>Michal</td> <td>Jackson</td> <td> [email protected] </td> <td>18</td> </tr> <tr> <td>2</td> <td>Nick</td> <td>Jonas</td> <td> [email protected] </td> <td>16</td> </tr> <tr> <td>3</td> <td>Peter</td> <td>Heaven</td> <td> [email protected] </td> <td>25</td> </tr> <tr> <td>5</td> <td>Sean</td> <td>Bean</td> <td> [email protected] </td> <td>20</td> </tr> <tr> <td>6</td> <td>Tom</td> <td>Baker</td> <td> [email protected] </td> <td>30</td> </tr> </table> <p>The rows <strong>id&apos;s 9, 12, and 14</strong> have been deleted. We use the below statement to delete the duplicate rows:</p> <p>Execute the script for <strong>creating</strong> the contact.</p> <pre> DELETE c1 FROM contacts c1 INNERJ OIN contacts c2 WHERE c1.id &gt; c2.id AND c1.email = c2.email; </pre> <br> <table class="table"> <tr> <td>id</td> <td>first_name</td> <td>last_name</td> <td>email</td> <td>age</td> </tr> <tr> <td>1</td> <td>Ben</td> <td>Barnes</td> <td> [email protected] </td> <td>21</td> </tr> <tr> <td>2</td> <td> <strong>Kavin</strong> </td> <td> <strong>Peterson</strong></td> <td> <strong> [email protected] </strong> </td> <td> <strong>22</strong> </td> </tr> <tr> <td>3</td> <td>Brian</td> <td>Blessed</td> <td> [email protected] </td> <td>18</td> </tr> <tr> <td>4</td> <td>Nick</td> <td>Jonas</td> <td> [email protected] </td> <td>16</td> </tr> <tr> <td>5</td> <td>Michal</td> <td>Krane</td> <td> [email protected] </td> <td>17</td> </tr> <tr> <td>6</td> <td>Eliza</td> <td>Bennett</td> <td> [email protected] </td> <td>23</td> </tr> <tr> <td>7</td> <td>Michal</td> <td>Jackson</td> <td> [email protected] </td> <td>18</td> </tr> <tr> <td>8</td> <td> <strong>Sean</strong> </td> <td> <strong>Bean</strong> </td> <td> <strong> [email protected] </strong> </td> <td> <strong>20</strong> </td> </tr> <tr> <td>9</td> <td>Mischa</td> <td>Barton</td> <td> [email protected] </td> <td>20</td> </tr> <tr> <td>10</td> <td> <strong>Peter</strong> </td> <td> <strong>Heaven</strong> </td> <td> <strong> [email protected] </strong> </td> <td> <strong>25</strong> </td> </tr> <tr> <td>11</td> <td>Tom</td> <td>Baker</td> <td> [email protected] </td> <td>30</td> </tr> </table> <h2>(B) Delete duplicate rows using an intermediate table</h2> <p>To delete a duplicate row by using the intermediate table, follow the steps given below:</p> <p> <strong>Step 1</strong> . Create a new table <strong>structure</strong> , same as the real table:</p> <pre> CREATE TABLE source_copy LIKE source; </pre> <p> <strong>Step 2</strong> . Insert the distinct rows from the original schedule of the database:</p> <pre> INSERT INTO source_copy SELECT * FROM source GROUP BY col; </pre> <p> <strong>Step 3</strong> . Drop the original table and rename the immediate table to the original one.</p> <pre> DROP TABLE source; ALTER TABLE source_copy RENAME TO source; </pre> <p>For example, the following statements delete the <strong>rows</strong> with <strong>duplicate</strong> emails from the contacts table:</p> <pre> -- step 1 CREATE TABLE contacts_temp LIKE contacts; -- step 2 INSERT INTO contacts_temp SELECT * FROM contacts GROUP BY email; -- step 3 DROP TABLE contacts; ALTER TABLE contacts_temp RENAME TO contacts; </pre> <h2>(C) Delete duplicate rows using the ROW_NUMBER() Function</h2> <h4>Note: The ROW_NUMBER() function has been supported since MySQL version 8.02, so we should check our MySQL version before using the function.</h4> <p>The following statement uses the <strong>ROW_NUMBER ()</strong> to assign a sequential integer to every row. If the email is duplicate, the row will higher than one.</p> <pre> SELECT id, email, ROW_NUMBER() OVER (PARTITION BY email ORDER BY email ) AS row_num FROM contacts; </pre> <p>The following SQL query returns <strong>id list</strong> of the duplicate rows:</p> <pre> SELECT id FROM (SELECT id, ROW_NUMBER() OVER ( PARTITION BY email ORDER BY email) AS row_num FROM contacts ) t WHERE row_num&gt; 1; </pre> <p> <strong>Output:</strong> </p> <table class="table"> <tr> <td>id</td> </tr> <tr> <td>9</td> </tr> <tr> <td>12</td> </tr> <tr> <td>14</td> </tr> </table> <h2>Delete Duplicate Records in Oracle</h2> <p>When we found the duplicate records in the table, we had to delete the unwanted copies to keep our data clean and unique. If a table has duplicate rows, we can delete it by using the <strong>DELETE</strong> statement.</p> <p>In the case, we have a column, which is not the part of <strong>group</strong> used to <strong>evaluate</strong> the <strong>duplicate</strong> records in the table.</p> <p>Consider the table given below:</p> <table class="table"> <tr> <td>VEGETABLE_ID</td> <td>VEGETABLE_NAME</td> <td>COLOR</td> </tr> <tr> <td>01</td> <td>Potato</td> <td>Brown</td> </tr> <tr> <td>02</td> <td>Potato</td> <td>Brown</td> </tr> <tr> <td>03</td> <td>Onion</td> <td>Red</td> </tr> <tr> <td>04</td> <td>Onion</td> <td>Red</td> </tr> <tr> <td>05</td> <td>Onion</td> <td>Red</td> </tr> <tr> <td>06</td> <td>Pumpkin</td> <td>Green</td> </tr> <tr> <td>07</td> <td>Pumpkin</td> <td>Yellow</td> </tr> </table> <br> <pre> -- create the vegetable table CREATE TABLE vegetables ( VEGETABLE_ID NUMBER generated BY DEFAULT AS ID ENTITY, VEGETABLE_NAME VARCHAR2(100), color VARCHAR2(20), PRIMARY KEY (VEGETABLE_ID) ); </pre> <br> <pre> -- insert sample rows INSERT INTO vegetables (VEGETABLE_NAME,color) VALUES(&apos;Potato&apos;,&apos;Brown&apos;); INSERT INTO vegetables (VEGETABLE_NAME,color) VALUES(&apos;Potato&apos;,&apos;Brown&apos;); INSERT INTO vegetables (VEGETABLE_NAME,color) VALUES(&apos;Onion&apos;,&apos;Red&apos;); INSERT INTO vegetables (VEGETABLE_NAME,color) VALUES(&apos;Onion&apos;,&apos;Red&apos;); INSERT INTO vegetables (VEGETABLE_NAME,color) VALUES(&apos;Onion&apos;,&apos;Red&apos;); INSERT INTO vegetables (VEGETABLE_NAME,color) VALUES(&apos;Pumpkin&apos;,&apos;Green&apos;); INSERT INTO vegetables (VEGETABLE_NAME,color) VALUES(&apos;Pumpkin&apos;,&apos;Yellow&apos;); </pre> <br> <pre> -- query data from the vegetable table SELECT * FROM vegetables; </pre> <p>Suppose, we want to keep the row with the highest <strong>VEGETABLE_ID</strong> and delete all other copies.</p> <pre> SELECT MAX (VEGETABLE_ID) FROM vegetables GROUP BY VEGETABLE_NAME, color ORDER BY MAX(VEGETABLE_ID); </pre> <br> <table class="table"> <tr> <td>MAX(VEGETABLE_ID)</td> </tr> <tr> <td>2</td> </tr> <tr> <td>5</td> </tr> <tr> <td>6</td> </tr> <tr> <td>7</td> </tr> </table> <p>We use the <strong>DELETE</strong> statement to delete the rows whose values in the <strong>VEGETABLE_ID COLUMN</strong> are not the <strong>highest</strong> .</p> <pre> DELETE FROM vegetables WHERE VEGETABLE_IDNOTIN ( SELECT MAX(VEGETABLE_ID) FROM vegetables GROUP BY VEGETABLE_NAME, color ); </pre> <p>Three rows have been deleted.</p> <pre> SELECT *FROM vegetables; </pre> <br> <table class="table"> <tr> <td>VEGETABLE_ID</td> <td>VEGETABLE_NAME</td> <td>COLOR</td> </tr> <tr> <td> <strong>02</strong> </td> <td>Potato</td> <td>Brown</td> </tr> <tr> <td> <strong>05</strong> </td> <td>Onion</td> <td>Red</td> </tr> <tr> <td> <strong>06</strong> </td> <td>Pumpkin</td> <td>Green</td> </tr> <tr> <td> <strong>07</strong> </td> <td><pumpkin td> <td>Yellow</td> </pumpkin></td></tr> </table> <p>If we want to keep the row with the lowest id, use the <strong>MIN()</strong> function instead of the <strong>MAX()</strong> function.</p> <pre> DELETE FROM vegetables WHERE VEGETABLE_IDNOTIN ( SELECT MIN(VEGETABLE_ID) FROM vegetables GROUP BY VEGETABLE_NAME, color ); </pre> <p>The above method works if we have a column that is not part of the group for evaluating duplicate. If all values in the columns have copies, then we cannot use the <strong>VEGETABLE_ID</strong> column.</p> <p>Let&apos;s drop and create the <strong>vegetable</strong> table with a new structure.</p> <pre> DROP TABLE vegetables; CREATE TABLE vegetables ( VEGETABLE_ID NUMBER, VEGETABLE_NAME VARCHAR2(100), Color VARCHAR2(20) ); </pre> <br> <pre> INSERT INTO vegetables (VEGETABLE_ID,VEGETABLE_NAME,color) VALUES(1,&apos;Potato&apos;,&apos;Brown&apos;); INSERT INTO vegetables (VEGETABLE_ID,VEGETABLE_NAME,color) VALUES(1, &apos;Potato&apos;,&apos;Brown&apos;); INSERT INTO vegetables (VEGETABLE_ID,VEGETABLE_NAME,color)VALUES(2,&apos;Onion&apos;,&apos;Red&apos;); INSERT INTO vegetables (VEGETABLE_ID,VEGETABLE_NAME,color)VALUES(2,&apos;Onion&apos;,&apos;Red&apos;); INSERT INTO vegetables (VEGETABLE_ID,VEGETABLE_NAME,color) VALUES(2,&apos;Onion&apos;,&apos;Red&apos;); INSERT INTO vegetables (VEGETABLE_ID,VEGETABLE_NAME,color) VALUES(3,&apos;Pumpkin&apos;,&apos;Green&apos;); INSERT INTO vegetables (VEGETABLE_ID,VEGETABLE_NAME,color) VALUES(&apos;4,Pumpkin&apos;,&apos;Yellow&apos;); SELECT * FROM vegetables; </pre> <br> <table class="table"> <tr> <td>VEGETABLE_ID</td> <td>VEGETABLE_NAME</td> <td>COLOR</td> </tr> <tr> <td>01</td> <td>Potato</td> <td>Brown</td> </tr> <tr> <td>01</td> <td>Potato</td> <td>Brown</td> </tr> <tr> <td>02</td> <td>Onion</td> <td>Red</td> </tr> <tr> <td>02</td> <td>Onion</td> <td>Red</td> </tr> <tr> <td>02</td> <td>Onion</td> <td>Red</td> </tr> <tr> <td>03</td> <td>Pumpkin</td> <td>Green</td> </tr> <tr> <td>04</td> <td>Pumpkin</td> <td>Yellow</td> </tr> </table> <p>In the vegetable table, the values in all columns <strong>VEGETABLE_ID, VEGETABLE_NAME</strong> , and color have been copied.</p> <p>We can use the <strong>rowid</strong> , a locator that specifies where Oracle stores the row. Because the <strong>rowid</strong> is unique so that we can use it to remove the duplicates rows.</p> <pre> DELETE FROM Vegetables WHERE rowed NOT IN ( SELECT MIN(rowid) FROM vegetables GROUP BY VEGETABLE_ID, VEGETABLE_NAME, color ); </pre> <p>The query verifies the deletion operation:</p> <pre> SELECT * FROM vegetables; </pre> <br> <table class="table"> <tr> <td>VEGETABLE_ID</td> <td>VEGETABLE_NAME</td> <td>COLOR</td> </tr> <tr> <td>01</td> <td>Potato</td> <td>Brown</td> </tr> <tr> <td>02</td> <td>Onion</td> <td>Red</td> </tr> <tr> <td>03</td> <td>Pumpkin</td> <td>Green</td> </tr> <tr> <td>04</td> <td>Pumpkin</td> <td>Yellow</td> </tr> </table> <hr></t2.id>

세 개의 행이 삭제되었습니다. 우리는 아래에 주어진 쿼리를 실행하여 중복된 이메일 테이블에서.

 SELECT email, COUNT (email) FROM contacts GROUP BY email HAVING COUNT (email) &gt; 1; 

쿼리는 빈 집합을 반환합니다. 연락처 테이블의 데이터를 확인하려면 다음 SQL 쿼리를 실행합니다.

 SELECT * FROM contacts; 

ID 이름 이메일 나이
7 반스 [이메일 보호됨] 이십 일
13 브라이언 축복 받은 [이메일 보호됨] 18
10 엘리자 베넷 [이메일 보호됨] 23
1 캐빈 피터슨 [이메일 보호됨] 22
8 미샤 바튼 [이메일 보호됨] 이십
열하나 남자 이름 [이메일 보호됨] 17
4 남자 이름 잭슨 [이메일 보호됨] 18
2 건강 상태 요나스 [이메일 보호됨] 16
베드로 천국 [이메일 보호됨] 25
5 [이메일 보호됨] 이십
6 빵 굽는 사람 [이메일 보호됨] 30

id는 9, 12, 14입니다. 삭제되었습니다. 아래 명령문을 사용하여 중복 행을 삭제합니다.

다음에 대한 스크립트를 실행합니다. 창조 연락처.

 DELETE c1 FROM contacts c1 INNERJ OIN contacts c2 WHERE c1.id &gt; c2.id AND c1.email = c2.email; 

ID 이름 이메일 나이
1 반스 [이메일 보호됨] 이십 일
2 캐빈 피터슨 [이메일 보호됨] 22
브라이언 축복 받은 [이메일 보호됨] 18
4 건강 상태 요나스 [이메일 보호됨] 16
5 남자 이름 [이메일 보호됨] 17
6 엘리자 베넷 [이메일 보호됨] 23
7 남자 이름 잭슨 [이메일 보호됨] 18
8 [이메일 보호됨] 이십
9 미샤 바튼 [이메일 보호됨] 이십
10 베드로 천국 [이메일 보호됨] 25
열하나 빵 굽는 사람 [이메일 보호됨] 30

(B) 중간 테이블을 사용하여 중복 행 삭제

중간 테이블을 사용하여 중복 행을 삭제하려면 아래 단계를 따르십시오.

1 단계 . 새 테이블 만들기 구조 , 실제 테이블과 동일:

 CREATE TABLE source_copy LIKE source; 

2 단계 . 데이터베이스의 원래 일정에서 고유한 행을 삽입합니다.

 INSERT INTO source_copy SELECT * FROM source GROUP BY col; 

3단계 . 원본 테이블을 삭제하고 직접 테이블의 이름을 원본 테이블로 바꿉니다.

 DROP TABLE source; ALTER TABLE source_copy RENAME TO source; 

예를 들어, 다음 명령문은 ~와 함께 복제하다 연락처 테이블의 이메일:

 -- step 1 CREATE TABLE contacts_temp LIKE contacts; -- step 2 INSERT INTO contacts_temp SELECT * FROM contacts GROUP BY email; -- step 3 DROP TABLE contacts; ALTER TABLE contacts_temp RENAME TO contacts; 

(C) ROW_NUMBER() 함수를 사용하여 중복 행 삭제

참고: ROW_NUMBER() 함수는 MySQL 버전 8.02부터 지원되므로 해당 함수를 사용하기 전에 MySQL 버전을 확인해야 합니다.

다음 문에서는 ROW_NUMBER() 모든 행에 순차적 정수를 할당합니다. 이메일이 중복된 경우 행은 1보다 높습니다.

문자열을 배열로
 SELECT id, email, ROW_NUMBER() OVER (PARTITION BY email ORDER BY email ) AS row_num FROM contacts; 

다음 SQL 쿼리는 다음을 반환합니다. 아이디 목록 중복 행 중:

 SELECT id FROM (SELECT id, ROW_NUMBER() OVER ( PARTITION BY email ORDER BY email) AS row_num FROM contacts ) t WHERE row_num&gt; 1; 

산출:

최대 절전 모드란 무엇인가요?
ID
9
12
14

Oracle에서 중복 레코드 삭제

테이블에서 중복 레코드를 발견하면 데이터를 깨끗하고 고유하게 유지하기 위해 원하지 않는 복사본을 삭제해야 했습니다. 테이블에 중복된 행이 있는 경우 다음을 사용하여 삭제할 수 있습니다. 삭제 성명.

이 경우에는 다음의 일부가 아닌 열이 있습니다. 그룹 사용 평가하다 그만큼 복제하다 테이블에 기록이 있습니다.

아래 표를 고려하십시오.

VEGETABLE_ID VEGETABLE_NAME 색상
01 감자 갈색
02 감자 갈색
03 양파 빨간색
04 양파 빨간색
05 양파 빨간색
06 호박 녹색
07 호박 노란색

 -- create the vegetable table CREATE TABLE vegetables ( VEGETABLE_ID NUMBER generated BY DEFAULT AS ID ENTITY, VEGETABLE_NAME VARCHAR2(100), color VARCHAR2(20), PRIMARY KEY (VEGETABLE_ID) ); 

 -- insert sample rows INSERT INTO vegetables (VEGETABLE_NAME,color) VALUES(&apos;Potato&apos;,&apos;Brown&apos;); INSERT INTO vegetables (VEGETABLE_NAME,color) VALUES(&apos;Potato&apos;,&apos;Brown&apos;); INSERT INTO vegetables (VEGETABLE_NAME,color) VALUES(&apos;Onion&apos;,&apos;Red&apos;); INSERT INTO vegetables (VEGETABLE_NAME,color) VALUES(&apos;Onion&apos;,&apos;Red&apos;); INSERT INTO vegetables (VEGETABLE_NAME,color) VALUES(&apos;Onion&apos;,&apos;Red&apos;); INSERT INTO vegetables (VEGETABLE_NAME,color) VALUES(&apos;Pumpkin&apos;,&apos;Green&apos;); INSERT INTO vegetables (VEGETABLE_NAME,color) VALUES(&apos;Pumpkin&apos;,&apos;Yellow&apos;); 

 -- query data from the vegetable table SELECT * FROM vegetables; 

가장 높은 행을 유지하고 싶다고 가정합니다. VEGETABLE_ID 다른 모든 복사본을 삭제합니다.

 SELECT MAX (VEGETABLE_ID) FROM vegetables GROUP BY VEGETABLE_NAME, color ORDER BY MAX(VEGETABLE_ID); 

최대(VEGETABLE_ID)
2
5
6
7

우리는 삭제 값이 있는 행을 삭제하는 명령문 VEGETABLE_ID 열 아니다 제일 높은 .

 DELETE FROM vegetables WHERE VEGETABLE_IDNOTIN ( SELECT MAX(VEGETABLE_ID) FROM vegetables GROUP BY VEGETABLE_NAME, color ); 

세 개의 행이 삭제되었습니다.

이진 검색 트리와 이진 트리
 SELECT *FROM vegetables; 

VEGETABLE_ID VEGETABLE_NAME 색상
02 감자 갈색
05 양파 빨간색
06 호박 녹색
07 노란색

ID가 가장 낮은 행을 유지하려면 다음을 사용하십시오. 최소() 대신 기능 최대() 기능.

 DELETE FROM vegetables WHERE VEGETABLE_IDNOTIN ( SELECT MIN(VEGETABLE_ID) FROM vegetables GROUP BY VEGETABLE_NAME, color ); 

위 방법은 중복 평가를 위한 그룹의 일부가 아닌 열이 있는 경우에 작동합니다. 열의 모든 값에 복사본이 있으면 다음을 사용할 수 없습니다. VEGETABLE_ID 열.

드롭하고 생성해 보겠습니다. 채소 새로운 구조의 테이블.

 DROP TABLE vegetables; CREATE TABLE vegetables ( VEGETABLE_ID NUMBER, VEGETABLE_NAME VARCHAR2(100), Color VARCHAR2(20) ); 

 INSERT INTO vegetables (VEGETABLE_ID,VEGETABLE_NAME,color) VALUES(1,&apos;Potato&apos;,&apos;Brown&apos;); INSERT INTO vegetables (VEGETABLE_ID,VEGETABLE_NAME,color) VALUES(1, &apos;Potato&apos;,&apos;Brown&apos;); INSERT INTO vegetables (VEGETABLE_ID,VEGETABLE_NAME,color)VALUES(2,&apos;Onion&apos;,&apos;Red&apos;); INSERT INTO vegetables (VEGETABLE_ID,VEGETABLE_NAME,color)VALUES(2,&apos;Onion&apos;,&apos;Red&apos;); INSERT INTO vegetables (VEGETABLE_ID,VEGETABLE_NAME,color) VALUES(2,&apos;Onion&apos;,&apos;Red&apos;); INSERT INTO vegetables (VEGETABLE_ID,VEGETABLE_NAME,color) VALUES(3,&apos;Pumpkin&apos;,&apos;Green&apos;); INSERT INTO vegetables (VEGETABLE_ID,VEGETABLE_NAME,color) VALUES(&apos;4,Pumpkin&apos;,&apos;Yellow&apos;); SELECT * FROM vegetables; 

VEGETABLE_ID VEGETABLE_NAME 색상
01 감자 갈색
01 감자 갈색
02 양파 빨간색
02 양파 빨간색
02 양파 빨간색
03 호박 녹색
04 호박 노란색

야채 테이블에서는 모든 열의 값이 VEGETABLE_ID, VEGETABLE_NAME , 색상이 복사되었습니다.

우리는 로위드 , Oracle이 행을 저장하는 위치를 지정하는 위치자입니다. 왜냐하면 로위드 중복 행을 제거하는 데 사용할 수 있도록 고유합니다.

 DELETE FROM Vegetables WHERE rowed NOT IN ( SELECT MIN(rowid) FROM vegetables GROUP BY VEGETABLE_ID, VEGETABLE_NAME, color ); 

쿼리는 삭제 작업을 확인합니다.

 SELECT * FROM vegetables; 

VEGETABLE_ID VEGETABLE_NAME 색상
01 감자 갈색
02 양파 빨간색
03 호박 녹색
04 호박 노란색