Bu bölümde, yinelenen satırları silmenin farklı yollarını öğreniyoruz. MySQL ve Oracle . Eğer SQL tablo yinelenen satırlar içeriyorsa, yinelenen satırları kaldırmamız gerekir.
Örnek verilerin hazırlanması
Komut dosyası, adlı tabloyu oluşturur. kişiler .
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 );
Yukarıdaki tabloya aşağıdaki verileri ekledik.
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'),
Bir işlemi yürüttükten sonra test verilerini yeniden oluşturmak için betiği çalıştırıyoruz. SİLMEK ifade .
Sorgu, kişiler tablosundaki verileri döndürür:
SELECT * FROM contacts ORDER BY email;
İD | ilk adı | soy isim | E-posta | yaş |
7 | Ben | Barnes | [e-posta korumalı] | yirmi bir |
13 | Brian | Kutsanmış | [e-posta korumalı] | 18 |
10 | Eliza | Bennett | [e-posta korumalı] | 23 |
1 | Kavin | Peterson | [e-posta korumalı] | 22 |
14 | Kavin | Peterson | [e-posta korumalı] | 23 |
8 | Mişa | Barton | [e-posta korumalı] | yirmi |
on bir | Michael | Musluklar | [e-posta korumalı] | 17 |
4 | Michael | Jackson | [e-posta korumalı] | 18 |
2 | Nick | Jonas | [e-posta korumalı] | 16 |
3 | Peter | Cennet | [e-posta korumalı] | 25 |
12 | Peter | Cennet | [e-posta korumalı] | 25 |
5 | Sean | Fasulye | [e-posta korumalı] | yirmi |
9 | Sean | Fasulye | [e-posta korumalı] | yirmi |
6 | Tom | Fırıncı | [e-posta korumalı] | 30 |
Aşağıdaki SQL sorgusu, kişi tablosundaki yinelenen e-postaları döndürür:
SELECT email, COUNT(email) FROM contacts GROUP BY email HAVING COUNT (email) > 1;
e-posta | COUNT(e-posta) |
[e-posta korumalı] | 2 |
[e-posta korumalı] | 2 |
[e-posta korumalı] | 2 |
Üç satırımız var kopyalamak e-postalar.
(A) DELETE JOIN deyimiyle yinelenen satırları silin
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) > 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'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 > 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> 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('Potato','Brown'); INSERT INTO vegetables (VEGETABLE_NAME,color) VALUES('Potato','Brown'); INSERT INTO vegetables (VEGETABLE_NAME,color) VALUES('Onion','Red'); INSERT INTO vegetables (VEGETABLE_NAME,color) VALUES('Onion','Red'); INSERT INTO vegetables (VEGETABLE_NAME,color) VALUES('Onion','Red'); INSERT INTO vegetables (VEGETABLE_NAME,color) VALUES('Pumpkin','Green'); INSERT INTO vegetables (VEGETABLE_NAME,color) VALUES('Pumpkin','Yellow'); </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'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,'Potato','Brown'); INSERT INTO vegetables (VEGETABLE_ID,VEGETABLE_NAME,color) VALUES(1, 'Potato','Brown'); INSERT INTO vegetables (VEGETABLE_ID,VEGETABLE_NAME,color)VALUES(2,'Onion','Red'); INSERT INTO vegetables (VEGETABLE_ID,VEGETABLE_NAME,color)VALUES(2,'Onion','Red'); INSERT INTO vegetables (VEGETABLE_ID,VEGETABLE_NAME,color) VALUES(2,'Onion','Red'); INSERT INTO vegetables (VEGETABLE_ID,VEGETABLE_NAME,color) VALUES(3,'Pumpkin','Green'); INSERT INTO vegetables (VEGETABLE_ID,VEGETABLE_NAME,color) VALUES('4,Pumpkin','Yellow'); 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>
Üç satır silinmişti. Bulmak için aşağıda verilen sorguyu çalıştırıyoruz. yinelenen e-postalar masadan.
f-string pitonu
SELECT email, COUNT (email) FROM contacts GROUP BY email HAVING COUNT (email) > 1;
Sorgu boş kümeyi döndürür. Kişiler tablosundaki verileri doğrulamak için aşağıdaki SQL sorgusunu yürütün:
SELECT * FROM contacts;
İD | ilk adı | soy isim | E-posta | yaş |
7 | Ben | Barnes | [e-posta korumalı] | yirmi bir |
13 | Brian | Kutsanmış | [e-posta korumalı] | 18 |
10 | Eliza | Bennett | [e-posta korumalı] | 23 |
1 | Kavin | Peterson | [e-posta korumalı] | 22 |
8 | Mişa | Barton | [e-posta korumalı] | yirmi |
on bir | Michael | Musluklar | [e-posta korumalı] | 17 |
4 | Michael | Jackson | [e-posta korumalı] | 18 |
2 | Nick | Jonas | [e-posta korumalı] | 16 |
3 | Peter | Cennet | [e-posta korumalı] | 25 |
5 | Sean | Fasulye | [e-posta korumalı] | yirmi |
6 | Tom | Fırıncı | [e-posta korumalı] | 30 |
Satırlar kimlik 9, 12 ve 14 silindi. Yinelenen satırları silmek için aşağıdaki ifadeyi kullanırız:
Komut dosyasını yürütün yaratmak temas.
DELETE c1 FROM contacts c1 INNERJ OIN contacts c2 WHERE c1.id > c2.id AND c1.email = c2.email;
İD | ilk adı | soy isim | e-posta | yaş |
1 | Ben | Barnes | [e-posta korumalı] | yirmi bir |
2 | Kavin | Peterson | [e-posta korumalı] | 22 |
3 | Brian | Kutsanmış | [e-posta korumalı] | 18 |
4 | Nick | Jonas | [e-posta korumalı] | 16 |
5 | Michael | Musluklar | [e-posta korumalı] | 17 |
6 | Eliza | Bennett | [e-posta korumalı] | 23 |
7 | Michael | Jackson | [e-posta korumalı] | 18 |
8 | Sean | Fasulye | [e-posta korumalı] | yirmi |
9 | Mişa | Barton | [e-posta korumalı] | yirmi |
10 | Peter | Cennet | [e-posta korumalı] | 25 |
on bir | Tom | Fırıncı | [e-posta korumalı] | 30 |
(B) Bir ara tablo kullanarak yinelenen satırları silin
Ara tabloyu kullanarak yinelenen bir satırı silmek için aşağıdaki adımları izleyin:
Aşama 1 . Yeni bir tablo oluştur yapı , gerçek tabloyla aynı:
CREATE TABLE source_copy LIKE source;
Adım 2 . Veritabanının orijinal programından farklı satırları ekleyin:
INSERT INTO source_copy SELECT * FROM source GROUP BY col;
Aşama 3 . Orijinal tabloyu bırakın ve doğrudan tabloyu orijinal tabloyla yeniden adlandırın.
DROP TABLE source; ALTER TABLE source_copy RENAME TO source;
Örneğin aşağıdaki ifadeler şunları siler: satırlar ile kopyalamak kişiler tablosundaki e-postalar:
-- 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() İşlevini kullanarak yinelenen satırları silin
Not: ROW_NUMBER() işlevi MySQL sürüm 8.02'den beri desteklenmektedir, bu nedenle işlevi kullanmadan önce MySQL sürümümüzü kontrol etmeliyiz.
Aşağıdaki ifade şunu kullanır: SATIR NUMARASI () Her satıra sıralı bir tamsayı atamak için. E-posta yineleniyorsa satır birden yüksek olur.
SELECT id, email, ROW_NUMBER() OVER (PARTITION BY email ORDER BY email ) AS row_num FROM contacts;
Aşağıdaki SQL sorgusu döndürür kimlik listesi yinelenen satırlardan:
java rastgele matematik rastgele
SELECT id FROM (SELECT id, ROW_NUMBER() OVER ( PARTITION BY email ORDER BY email) AS row_num FROM contacts ) t WHERE row_num> 1;
Çıktı:
İD |
9 |
12 |
14 |
Oracle'da Yinelenen Kayıtları Silme
Tabloda yinelenen kayıtları bulduğumuzda verilerimizi temiz ve benzersiz tutmak için istenmeyen kopyaları silmek zorunda kaldık. Bir tabloda yinelenen satırlar varsa, bunu kullanarak silebiliriz. SİLMEK ifade.
Bu durumda, bir parçası olmayan bir sütunumuz var. grup eskiden değerlendirmek the kopyalamak tablodaki kayıtlar.
Aşağıda verilen tabloyu göz önünde bulundurun:
Java dizi listesi
SEBZE_ID | SEBZE_NAME | RENK |
01 | Patates | Kahverengi |
02 | Patates | Kahverengi |
03 | Soğan | Kırmızı |
04 | Soğan | Kırmızı |
05 | Soğan | Kırmızı |
06 | Kabak | Yeşil |
07 | Kabak | Sarı |
-- 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('Potato','Brown'); INSERT INTO vegetables (VEGETABLE_NAME,color) VALUES('Potato','Brown'); INSERT INTO vegetables (VEGETABLE_NAME,color) VALUES('Onion','Red'); INSERT INTO vegetables (VEGETABLE_NAME,color) VALUES('Onion','Red'); INSERT INTO vegetables (VEGETABLE_NAME,color) VALUES('Onion','Red'); INSERT INTO vegetables (VEGETABLE_NAME,color) VALUES('Pumpkin','Green'); INSERT INTO vegetables (VEGETABLE_NAME,color) VALUES('Pumpkin','Yellow');
-- query data from the vegetable table SELECT * FROM vegetables;
Diyelim ki en yüksek sırayı korumak istiyoruz SEBZE_ID ve diğer tüm kopyaları silin.
SELECT MAX (VEGETABLE_ID) FROM vegetables GROUP BY VEGETABLE_NAME, color ORDER BY MAX(VEGETABLE_ID);
MAX(VEGETABLE_ID) |
2 |
5 |
6 |
7 |
biz kullanıyoruz SİLMEK değerleri olan satırları silmek için kullanılan ifade SEBZE_ID SÜTUNU değil mi en yüksek .
DELETE FROM vegetables WHERE VEGETABLE_IDNOTIN ( SELECT MAX(VEGETABLE_ID) FROM vegetables GROUP BY VEGETABLE_NAME, color );
Üç satır silindi.
SELECT *FROM vegetables;
SEBZE_ID | SEBZE_NAME | RENK |
02 | Patates | Kahverengi |
05 | Soğan | Kırmızı |
06 | Kabak | Yeşil |
07 | Sarı | |
Satırı en düşük kimliğe sahip tutmak istiyorsak, MIN() işlevi yerine MAKS() işlev.
DELETE FROM vegetables WHERE VEGETABLE_IDNOTIN ( SELECT MIN(VEGETABLE_ID) FROM vegetables GROUP BY VEGETABLE_NAME, color );
Yukarıdaki yöntem, yinelenenleri değerlendirmek için grubun parçası olmayan bir sütunumuz varsa işe yarar. Sütunlardaki tüm değerlerin kopyaları varsa bu durumda SEBZE_ID kolon.
Bırakalım ve oluşturalım sebze yeni bir yapıya sahip tablo.
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,'Potato','Brown'); INSERT INTO vegetables (VEGETABLE_ID,VEGETABLE_NAME,color) VALUES(1, 'Potato','Brown'); INSERT INTO vegetables (VEGETABLE_ID,VEGETABLE_NAME,color)VALUES(2,'Onion','Red'); INSERT INTO vegetables (VEGETABLE_ID,VEGETABLE_NAME,color)VALUES(2,'Onion','Red'); INSERT INTO vegetables (VEGETABLE_ID,VEGETABLE_NAME,color) VALUES(2,'Onion','Red'); INSERT INTO vegetables (VEGETABLE_ID,VEGETABLE_NAME,color) VALUES(3,'Pumpkin','Green'); INSERT INTO vegetables (VEGETABLE_ID,VEGETABLE_NAME,color) VALUES('4,Pumpkin','Yellow'); SELECT * FROM vegetables;
SEBZE_ID | SEBZE_NAME | RENK |
01 | Patates | Kahverengi |
01 | Patates | Kahverengi |
02 | Soğan | Kırmızı |
02 | Soğan | Kırmızı |
02 | Soğan | Kırmızı |
03 | Kabak | Yeşil |
04 | Kabak | Sarı |
Sebze tablosunda tüm sütunlardaki değerler VEGETABLE_ID, VEGETABLE_NAME ve renk kopyalandı.
Şunu kullanabiliriz: sıralı Oracle'ın satırı nerede sakladığını belirten bir konum belirleyici. Çünkü sıralı benzersizdir, böylece yinelenen satırları kaldırmak için onu kullanabiliriz.
DELETE FROM Vegetables WHERE rowed NOT IN ( SELECT MIN(rowid) FROM vegetables GROUP BY VEGETABLE_ID, VEGETABLE_NAME, color );
Sorgu, silme işlemini doğrular:
SELECT * FROM vegetables;
SEBZE_ID | SEBZE_NAME | RENK |
01 | Patates | Kahverengi |
02 | Soğan | Kırmızı |
03 | Kabak | Yeşil |
04 | Kabak | Sarı |