gpt4 book ai didi

mysql - 当我尝试更新记录时出现错误 #1062 - 键 'ID_UNIQUE' 的重复条目

转载 作者:行者123 更新时间:2023-11-29 07:26:29 26 4
gpt4 key购买 nike

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

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'

844844 指的是ID字段。我在 patient 表中有这个字段,我想更新患者的数据。但是patient表的主键是patiend_id而不是ID

你知道如何修复错误吗?

最佳答案

问题是存储过程的输入参数与表中的列名相同。这会导致模棱两可的行为。

例如:在SET name = name ; MySQL 是如何解析哪一个是参数值,哪一个是列名?

我通常在 IN 参数前加上 in_ 和在 OUT 前加上 out_ 以提高代码可读性并避免模棱两可的行为。

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 ;

关于mysql - 当我尝试更新记录时出现错误 #1062 - 键 'ID_UNIQUE' 的重复条目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53448120/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com