M1X
M1X

Reputation: 5354

Add an unique ID into an existing MySQL table

visitors table
-------------------------------------------------------
id(auto_incr.) | ip | isp | count | time | (isp_id)
-------------------------------------------------------

I have this MySQL table which has all unique IP addresses and the number of times an IP has visited my website including other IP info such as ISP, time etc.

How can I add another column called isp_id which will we unique for each ISP?

For example, if a particular ISP exists 10 times, isp_id will be the same for these ISPs, and if another ISP exists 2 times, isp_id will be incremented by one.

How can I achieve this?

Sorry for my bad English. I'm from Albania.

Upvotes: 0

Views: 122

Answers (3)

Erick
Erick

Reputation: 751

I think I would make isp a foreign key to another table - probably called "isps". I think the steps are:

  1. Create and populate table "isps"
  2. Add "isp_id" as a NULL-able integer column to "visitors"
  3. Populate visitors.isp_id from the data in "isps"
  4. Make visitors.isp_id NOT NULL and a foreign key
  5. Drop the original string column from "visitors"
  6. Profit!

Something like this for #1:

CREATE TABLE IF NOT EXISTS isps (
  id INT NOT NULL AUTO_INCREMENT,
  PRIMARY KEY (id),
  KEY(isp_name)
) SELECT DISTINCT isp_name FROM visitors;

And for #2:

ALTER TABLE visitors ADD COLUMN isp_id INT;

For #3:

UPDATE visitors v
  JOIN isps i ON v.isp = i.isp_name
  SET v.isp_id = i.id;

For #4:

ALTER TABLE visitors
  ADD CONSTRAINT fk_ispID
  FOREIGN KEY (isp_id) REFERENCES isps(id);

And, finally, #5:

ALTER TABLE visitors DROP COLUMN isp;

NOTE WELL: NONE of this has been tested and I didn't go look up the syntax, so please use it as an example. Copy/paste will likely get you into trouble.

Also, once you've done this, you'll have to update all the code that adds rows to visitors and you'll also have to add code to maintain isps when new ISPs come in.

Upvotes: 1

Brian DeMilia
Brian DeMilia

Reputation: 13248

Several queries are involved, you can see a demonstration here: http://sqlfiddle.com/#!2/e87cd/1/0

In a nutshell, step 1 is to create a table to hold the ISPs (one row per ISP):

create table isps (
   id int not null auto_increment,
   isp_name varchar(20),
  primary key (id)
);

You want to populate that table based on the ISPs currently on your visitors table like so:

insert into isps
  (isp_name)
  select distinct isp from visitors;

Then add an isp_id column to your VISITORS table like so:

alter table visitors add column isp_id int;

Then update your VISITORS table to include the ISP_ID values associated w/ the rows of the ISPS table:

update visitors v
   set v.isp_id =
       (select i.id from isps i where i.isp_name = v.isp);

After doing the above it would also be my recommendation to remove the current ISP column of the VISITORS table, and keep only the ISP_ID field that was just added above. You can always join into the ISPS table to get the name for a given ISP_ID.

To do that, run:

alter table visitors drop column isp;

As you can see it's easy enough to just join at that point, and you no longer have redundant data, example: http://sqlfiddle.com/#!2/1e7d6/1/0

Upvotes: 2

Mudassir Hasan
Mudassir Hasan

Reputation: 28741

A better approach would be to create a separate ISP table containing unique isp_id and name of isp.

CREATE TABLE ispTable(isp_id int auto_increment,isp_Name varchar(100));

Now you dont have to store isp name in the visitors table posted in your question , only the isp_id is inserted during making entry in the table and you can fetch name of isp if required using join between the tables based on isp_id

visitors table
---------------------------------------------
id(auto_incr.) | ip | count | time | isp_id
---------------------------------------------

Note : isp column has been removed from visitors table .

Upvotes: 1

Related Questions