gpt4 book ai didi

MySql重复返回错误

转载 作者:行者123 更新时间:2023-11-29 06:27:53 25 4
gpt4 key购买 nike

在触发 INSERT 查询时,如果我希望插入的值之一已经存在于表中,是否可以返回错误(或任何其他指示)?

INSERT INTO `private_customers`(`first_name`, `phone_number`) 
VALUES ('the_first_name','1122334455')
// if the phone number is already exist in the table then don't insert nothing and return indication that it is already exist

最佳答案

是的,只需添加唯一索引:

create unique index idx_private_custoemrs_fn on private_customers(first_name);
create unique index idx_private_custoemrs_pn on private_customers(phone_number);

如果您希望值是唯一的,则在上创建索引:

create unique index idx_private_custoemrs_fn_pn on private_customers(first_name, phone_number);

(并且不包括其他两个索引。)

关于MySql重复返回错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29845592/

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