从重复的条目进行MySQL清理表,并在相关表中重新链接FK

这是我的情况:我有2张桌子,病人和书房.

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

就我而言,pat_id应该是唯一的.在某些数据库用途中,它不是唯一的,因为它在某些用途上可能不是唯一的(它不是自制系统).我发现了如何配置系统以将pat_id视为唯一的,但是现在我需要清除数据库以查找重复的患者,并在删除重复的患者之前将研究表中的重复患者重新链接到其余唯一患者.

患者表:

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

解决方法:

我就是这样

我重用了患者表中未使用的字段,以标记未重复的患者(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,但是我个人更喜欢这种简单的方法.这也使我可以检查每个步骤是否正在执行我期望的工作.

上一篇:在SQL / MySQL中,当没有主ID索引时,可以使用左外部联接来查找重复项吗?


下一篇:mysqldump auto_increment导致主键错误