gpt4 book ai didi

mysql - 确保表只包含某些字段的唯一值

转载 作者:行者123 更新时间:2023-11-29 03:47:51 24 4
gpt4 key购买 nike

我有一个这样定义的 MYSQL 表:

CREATE TABLE Data (
ID BIGINT(20) NOT NULL AUTO_INCREMENT,
Unique_Hash BINARY(16) NOT NULL,
Size BIGINT(20) NOT NULL,
DateAdded DATETIME NOT NULL,
PRIMARY KEY (ID),
INDEX (Unique_Hash)
);

我希望用 1,000 条记录填充此表,但绝不能将 Unique_Hash/Size 对添加到另一条记录中。相反,我想确保忽略与我现有的 Unique_Hash/Size 值匹配的任何新记录。是 DateAdded 字段破坏了我的 ON DUPLICATE IGNORE 参数。

我如何安全地将数据导入此表以确保我的记录相对于我的 Unique_Hash 和 Size 字段是唯一的?

最佳答案

在 Unique_Hash 和 Size 字段上创建多列唯一索引。这肯定会阻止任何具有相同数据的条目被插入到表中。

在导入数据时,您可以使用 insert ignore ... 语句来忽略插入期间由于唯一索引冲突而收到的任何错误。这些将被报告为警告。

请注意使用 ignore option 的副作用:

Data conversions that would trigger errors abort the statement if IGNORE is not specified. With IGNORE, invalid values are adjusted to the closest values and inserted; warnings are produced but the statement does not abort. You can determine with the mysql_info() C API function how many rows were actually inserted into the table.

关于mysql - 确保表只包含某些字段的唯一值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35987841/

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