简体   繁体   English

在现有的MySQL表中添加唯一ID

[英]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. 我有这个MySQL表,其中包含所有唯一的IP地址以及IP访问我的网站的次数,包括其他IP信息,例如ISP,时间等。

How can I add another column called isp_id which will we unique for each ISP? 如何添加另一列isp_id ,我们将为每个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. 例如,如果一个特定的ISP存在10次,则这些ISP的isp_id将相同,如果另一个ISP存在2次,则isp_id将增加1。

How can I achieve this? 我该如何实现?

Sorry for my bad English. 对不起,我的英语不好。 I'm from Albania. 我来自阿尔巴尼亚。

Several queries are involved, you can see a demonstration here: http://sqlfiddle.com/#!2/e87cd/1/0 涉及几个查询,您可以在此处查看演示: 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): 简而言之,步骤1是创建一个表来保存ISP(每个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: 您要基于当前访问者表上的ISP来填充该表,如下所示:

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

Then add an isp_id column to your VISITORS table like so: 然后将isp_id列添加到VISITORS表中,如下所示:

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: 然后更新您的VISITORS表以包括与ISPS表的行关联的ISP_ID值:

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. 完成上述操作后,我也建议删除VISITORS表的当前ISP列,并仅保留上面刚刚添加的ISP_ID字段。 You can always join into the ISPS table to get the name for a given ISP_ID. 您始终可以加入ISPS表以获取给定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 如您所见,在那一点加入就很容易了,而且您不再有多余的数据,例如: http : //sqlfiddle.com/#!2/1e7d6/1/0

A better approach would be to create a separate ISP table containing unique isp_id and name of isp. 更好的方法是创建一个单独的ISP表,其中包含唯一的isp_id和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 现在,你不必存储在ISP名称visitors张贴在你的问题表,只有isp_id在表格中输入过程中插入,如果使用表之间的连接根据需要,您可以获取ISP的名称isp_id

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

Note : isp column has been removed from visitors table . 注意:isp列已从visitors表中删除。

I think I would make isp a foreign key to another table - probably called "isps". 我想我会将isp用作另一个表的外键-可能称为“ isps”。 I think the steps are: 我认为步骤是:

  1. Create and populate table "isps" 创建并填充表“ isps”
  2. Add "isp_id" as a NULL-able integer column to "visitors" 将“ isp_id”作为可为NULL的整数列添加到“访问者”
  3. Populate visitors.isp_id from the data in "isps" 从“ isps”中的数据填充visits.isp_id
  4. Make visitors.isp_id NOT NULL and a foreign key 使visits.isp_id不为NULL和外键
  5. Drop the original string column from "visitors" 从“访客”中删除原始字符串列
  6. Profit! 利润!

Something like this for #1: 对于#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: 对于#2:

ALTER TABLE visitors ADD COLUMN isp_id INT;

For #3: 对于#3:

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

For #4: 对于#4:

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

And, finally, #5: 最后,#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. 此外,完成此操作后,您将必须更新所有向访问者添加行的代码,并且还必须添加代码以在新的ISP出现时维护isps。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM