Removing Duplicates by Adding an Index : Index « Table Index « SQL / MySQL






Removing Duplicates by Adding an Index

    
mysql>
mysql>
mysql> CREATE TABLE cat_mailing
    -> (
    ->  last_name       CHAR(40) NOT NULL,
    ->  first_name      CHAR(40) NOT NULL,
    ->  street          CHAR(40) NOT NULL
    -> );
Query OK, 0 rows affected (0.01 sec)

mysql>
mysql> INSERT INTO cat_mailing (first_name, last_name, street)
    ->  VALUES
    ->          ('Jim','Isaacson','515 Fordam St., Apt. 917'),
    ->          ('Wallace','Baxter','57 3rd Ave.'),
    ->          ('Taylor','McTavish','432 River Run'),
    ->          ('Marlene','Pinter','9 Sunset Trail'),
    ->          ('WALLACE','BAXTER','57 3rd Ave.'),
    ->          ('Bartholomew','Brown','432 River Run'),
    ->          ('Marlene','Pinter','9 Sunset Trail'),
    ->          ('Wallace','Baxter','57 3rd Ave., Apt 102')
    -> ;
Query OK, 8 rows affected (0.00 sec)
Records: 8  Duplicates: 0  Warnings: 0

mysql>
mysql> SELECT * FROM cat_mailing;
+-----------+-------------+--------------------------+
| last_name | first_name  | street                   |
+-----------+-------------+--------------------------+
| Isaacson  | Jim         | 515 Fordam St., Apt. 917 |
| Baxter    | Wallace     | 57 3rd Ave.              |
| McTavish  | Taylor      | 432 River Run            |
| Pinter    | Marlene     | 9 Sunset Trail           |
| BAXTER    | WALLACE     | 57 3rd Ave.              |
| Brown     | Bartholomew | 432 River Run            |
| Pinter    | Marlene     | 9 Sunset Trail           |
| Baxter    | Wallace     | 57 3rd Ave., Apt 102     |
+-----------+-------------+--------------------------+
8 rows in set (0.00 sec)

mysql> SELECT * FROM cat_mailing ORDER BY last_name, first_name;
+-----------+-------------+--------------------------+
| last_name | first_name  | street                   |
+-----------+-------------+--------------------------+
| Baxter    | Wallace     | 57 3rd Ave.              |
| BAXTER    | WALLACE     | 57 3rd Ave.              |
| Baxter    | Wallace     | 57 3rd Ave., Apt 102     |
| Brown     | Bartholomew | 432 River Run            |
| Isaacson  | Jim         | 515 Fordam St., Apt. 917 |
| McTavish  | Taylor      | 432 River Run            |
| Pinter    | Marlene     | 9 Sunset Trail           |
| Pinter    | Marlene     | 9 Sunset Trail           |
+-----------+-------------+--------------------------+
8 rows in set (0.00 sec)

mysql>
mysql> ALTER IGNORE TABLE cat_mailing
    -> ADD PRIMARY KEY (last_name, first_name);
Query OK, 8 rows affected (0.02 sec)
Records: 8  Duplicates: 3  Warnings: 0

mysql>
mysql> SELECT * FROM cat_mailing ORDER BY last_name, first_name;
+-----------+-------------+--------------------------+
| last_name | first_name  | street                   |
+-----------+-------------+--------------------------+
| Baxter    | Wallace     | 57 3rd Ave.              |
| Brown     | Bartholomew | 432 River Run            |
| Isaacson  | Jim         | 515 Fordam St., Apt. 917 |
| McTavish  | Taylor      | 432 River Run            |
| Pinter    | Marlene     | 9 Sunset Trail           |
+-----------+-------------+--------------------------+
5 rows in set (0.00 sec)

mysql>
mysql> drop table cat_mailing;
Query OK, 0 rows affected (0.00 sec)

   
    
    
    
  








Related examples in the same category

1.Creating a Table with an Index
2.Create table: alter table to drop index
3.Create index on a column
4.Create table: Make index
5.Create index on table column with order
6.Create index on one column
7.Create index on two columns
8.Create unique index on table columns
9.Create index using btree
10.Handling Duplicate Index Values
11.Eliminating Duplicates by Adding an Index
12.How to include a unique index in the table definition.
13.How to include a regular index in a table definition:
14.Get the descriptive data of the indexes defined on the PENALTIES table.
15.Creating Indexes (CREATE INDEX)
16.Create an index on the AMOUNT column of the PENALTIES table.
17.Get the names of the indexes defined on the PENALTIES table.
18.Defining Indexes at Table-Creation Time
19.The syntax for CREATE INDEX is as follows
20.Removing Indexes
21.Create index of decimal type column
22.Create view for indexes
23.Drop an index by name
24.Add index to int type column
25.To create a unique-valued index, use the UNIQUE keyword instead of INDEX.
26.PRIMARY KEY INDEX_PRIM
27.Create an index and check it
28.Adding or Dropping Indexes
29.There are four types of statements for adding indexes to a table:
30.Hash index
31.Show index for a table
32.Using index in order by clause to indecate the result from subquery
33.Create a table with a nonunique index on the date-valued column Inauguration
34.To include multiple columns in an index
35.Provide index definitions in addition to the column definitions.
36.Modifying Indexes of Existing Tables
37.A table can have multiple indexes.
38.Create a single-column index on c, and the second creates a multiple-column index that includes both c and i