来自重复条目的MySQL清理表和依赖表中的重新链接FK

时间:2015-03-02 03:38:29

标签: mysql foreign-keys duplicates duplicate-removal

以下是我的情况:我有2个表,patientstudy

每个表都有自己的使用自动增量的PK。

就我而言,pat_id应该是唯一的。它没有在数据库级别被声明为唯一,因为它可能是非常独特的一些用途(它不是自制系统)。我发现如何配置系统以将pat_id视为唯一,但是我现在需要清理重复患者的数据库并将重复的研究表中的患者重新链接到剩余的独特患者,然后再删除重复的患者

Patient表:

CREATE TABLE `patient` (
  `pk` BIGINT(20) NOT NULL AUTO_INCREMENT,
  `pat_id` VARCHAR(250) COLLATE latin1_bin DEFAULT NULL,
...
  `pat_name` VARCHAR(250) COLLATE latin1_bin DEFAULT NULL,
...
  `pat_custom1` VARCHAR(250) COLLATE latin1_bin DEFAULT NULL
....
  PRIMARY KEY (`pk`)
)ENGINE=InnoDB;

研究表:

CREATE TABLE `study` (
  `pk` BIGINT(20) NOT NULL AUTO_INCREMENT,
  `patient_fk` BIGINT(20) DEFAULT NULL,
...
  PRIMARY KEY (`pk`),
...
  CONSTRAINT `patient_fk` FOREIGN KEY (`patient_fk`) REFERENCES `patient` (`pk`)
)ENGINE=InnoDB;

我发现了一些类似的问题,但问题并不完全相同,特别是它错过了外键与其余独特患者的联系。

Cleanup Update for Duplicate Entries

Update only first record from duplicate entries in MySQL

1 个答案:

答案 0 :(得分:1)

这就是我的做法。

我重复使用patient表中未使用的字段来标记非重复(N),第1个重复(X)和其他重复患者(Y)。您也可以为此添加一个列(并在使用后将其删除)。

以下是我为清理数据库而采取的步骤:

/*1: List duplicated */
select pk,pat_id, t.`pat_id_issuer`, t.`pat_name`, t.pat_custom1
from patient t
where pat_id in (
select pat_id from (
select pat_id, count(*)
from patient 
group by 1
having count(*)>1
) xxx);    

/*2: Delete orphan patients */
delete from patient where pk not in (select patient_fk from study);

/*3: Reset flag for duplicated (or not) patients*/
update patient t set t.`pat_custom1`='N';

/*4: Mark all duplicated */
update patient t set t.`pat_custom1`='Y' 
where pat_id in (
select pat_id from (
select pat_id, count(*)
from patient 
group by 1
having count(*)>1
) xxx) ;

/*5: Unmark the 1st of the duplicated*/
update patient t 
join (select pk from (
select min(pk) as pk, pat_id from patient 
where  pat_custom1='Y'  
group by pat_id
) xxx ) x
on (x.pk=t.pk)
set t.`pat_custom1`='X' 
where  pat_custom1='Y'
  ;

/*6: Verify update is correct*/
select pk, pat_id,pat_custom1  
from `patient` 
where  pat_custom1!='N'
order by pat_id, pat_custom1;

/*7: Verify studies linked to duplicated patient */
select p.* from study s
join patient p on (p.pk=s.patient_fk)
where p.pat_custom1='Y';

/*8: Relink duplicated patients */
update study s
join patient p on (p.pk=s.patient_fk)
set patient_fk = (select pk from patient pp
where pp.pat_id=p.pat_id and pp.pat_custom1='X')
where p.pat_custom1='Y';

/*9: Delete newly orphan patients */
delete from patient where pk not in (select patient_fk from study);

/* 10: reset flag */
update patient t set t.`pat_custom1`=null;

/* 11: Commit changes */
commit;

肯定有一种更短的方式,有一些更聪明(复杂?)的SQL,但我个人更喜欢这种简单的方式。这也让我可以检查每一步是否正在做我期望的事情。