当我试图更新一条记录时,出现错误#1062-键'id#u unique'的重复条目

uyto3xhc  于 2021-06-17  发布在  Mysql
关注(0)|答案(1)|浏览(229)

我正在使用以下存储过程更新表:

DELIMITER $$
CREATE DEFINER=`developer`@`localhost` PROCEDURE `update_patient`(IN `patient_id` INT(11), IN `name` VARCHAR(45), IN `surname` VARCHAR(45), IN `middle_name` VARCHAR(45), IN `email` VARCHAR(45), IN `phone` VARCHAR(45), IN `mobile` VARCHAR(45), IN `address_id` INT(11), IN `address_no` VARCHAR(8), IN `ID` VARCHAR(45), IN `DOB` DATE)
    NO SQL
UPDATE
  patient
SET name  = name,
  surname = surname,
  middle_name = middle_name,
  email = email,
  phone = phone,
  mobile = mobile,
  address_id = address_id,
  address_no = address_no,
  ID = ID,
  DOB = DOB      
WHERE
  patient_id = patient_id
LIMIT 1;
END$$
DELIMITER ;

当我想打通电话的时候 phpmyadmin 我得到一个错误:

1062 - Duplicate entry '844844' for key 'ID_UNIQUE' 844844ID 现场。我有这个领域 patient 我想更新病人的数据。然而,病人表的主键是 patiend_id 而不是 ID .

你知道怎么改正错误吗?

uinbv5nw

uinbv5nw1#

问题是存储过程的输入参数与表中的列名相同。这导致了模棱两可的行为。
例如:在 SET name = name ; mysql如何解析其中哪一个是param值,哪一个是列名?
我通常用前缀 IN 参数 in_ 以及 OUTout_ 代码可读性和避免含糊不清的行为。

DELIMITER $$
CREATE definer=`developer`@`localhost`
PROCEDURE `update_patient`(IN `in_patient_id`  INT(11),
                           IN `in_name`        VARCHAR(45),
                           IN `in_surname`     VARCHAR(45),
                           IN `in_middle_name` VARCHAR(45),
                           IN `in_email`       VARCHAR(45),
                           IN `in_phone`       VARCHAR(45),
                           IN `in_mobile`      VARCHAR(45),
                           IN `in_address_id`  INT(11),
                           IN `in_address_no`  VARCHAR(8),
                           IN `in_id`          VARCHAR(45),
                           IN `in_dob`         date)
  NO SQL

  UPDATE patient
  SET    name = in_name,
         surname = in_surname,
         middle_name = in_middle_name,
         email = in_email,
         phone = in_phone,
         mobile = in_mobile,
         address_id = in_address_id,
         address_no = in_address_no,
         id = in_id,
         dob = in_dob
  WHERE  patient_id = in_patient_id
  LIMIT  1;

END$$
DELIMITER ;

相关问题