0

I have a database that has over 100,000 records of users and has no primary key. Could you explain to me how I would accomplish the following: 1. Auto generate primary keys based on Firstname MiddleName LastName. 2. Note that some records are redundant ie same users and I want the same users to have the same primary key e.g Mary Jane Joe is appearing 20 times in the rows I would want her to have the same primary key in all appearances. Thanks in advance

2 Answers2

0

Best Option

Use a truly unique primary key by adding a not null int field with auto-increment. This will automatically give each record a unique primary key. If you still want to use the fields described above as a unique field, I would add a composite unique key between the fields.

One of the main benefits of this is that integer fields are a lot faster to query than varchars. That being said, your table only has 100,000 records so it will be negligible at this time.

Alternate

If you still want to use the keys you defined, then you have a few options both with the same process.

  1. Add a regular, nullable varchar field to your table, not a primary key yet(like this)
  2. Find duplicates and delete them
  3. Loop through each record and create your primary key based on the values you described and update the current records field defined in step 1 with the key.
  4. Alter the table to now add your primary key (like this)

I do have to caution you though, it sounds like your idea of a primary key is not truly unique, which defeats the purpose of a primary key.

Community
  • 1
  • 1
gwnp
  • 1,127
  • 1
  • 10
  • 35
0

Build the new table with the auto-inc (if needed) and the uniqueness constraint on name:

CREATE TABLE new (
    id INT UNSIGNED AUTO_INCREMENT,
    PRIMARY KEY(id),
    UNIQUE(Firstname, MiddleName, LastName)
);

Copy all non-dup rows into that table. (This assumes "dup" involves just the 3 columns, not just the name.)

INSERT IGNORE INTO new
                   (Firstname MiddleName LastName, ...)
    SELECT DISTINCT Firstname MiddleName LastName, ...
        FROM real;

Switch tables (if desired):

RENAME TABLE real TO old, new TO real;
DROP TABLE old;
Rick James
  • 135,179
  • 13
  • 127
  • 222