首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >MySQL确保每一行都是唯一的

MySQL确保每一行都是唯一的
EN

Stack Overflow用户
提问于 2019-04-15 14:17:45
回答 2查看 453关注 0票数 1

我有下表,希望确保每一行都是唯一的。sku_simple_product、branding_position_de、branding_id、handling_group_id和pre_cost_id的组合必须在此表中只存在一次。

代码语言:javascript
复制
branding_position_id    int(255) Auto-Inkrement  
sku_simple_product  text     
branding_position_de    text     
branding_position_en    text NULL    
branding_position_fr    text NULL    
branding_position_es    text NULL    
branding_position_it    text NULL    
branding_position_pl    text NULL    
branding_position_nl    text NULL    
branding_id varchar(255)     
handling_group_id   varchar(255) NULL    
is_branding_incl    int(11) [0]  
pre_cost_id varchar(255)

现在我有以下问题:

  1. 这真的是SQL的对比吗?
  2. 我真的需要自动创建的"branding_position_id“吗?
  3. 在不知道记录的唯一"branding_position_id“的情况下,在不存在的情况下插入记录或更新记录的最有效方法是什么?那么基于以上描述的独特属性组合呢?提前感谢!
EN

回答 2

Stack Overflow用户

发布于 2019-04-15 14:23:25

创建UNIQUE约束,如下所示:

代码语言:javascript
复制
alter table my_table add constraint uq1 (
  sku_simple_product, 
  branding_position_de, 
  branding_id, 
  handling_group_id,
  pre_cost_id
);

但是,由于列的数据类型为TEXT,此解决方案具有很高的潜在性。TEXT列可以存储非常大的文本片段,检查它们的唯一性可能会很慢。

如果可能的话,我建议将类型更改为较小的类型,如VARCHAR

票数 0
EN

Stack Overflow用户

发布于 2019-04-15 14:30:23

  1. 是的,因为所有其他数据都会随着时间的推移而改变,而且效率也会更高。在这里你可以看到一些优势和前卫。
  2. 你可以用这样的东西。
代码语言:javascript
复制
INSERT INTO companies
    (id, full_name, address, phone_number)
VALUES
    (1, ‘Apple’, '1 Infinite Loop, Cupertino, California', 18002752273)
ON DUPLICATE KEY UPDATE
    full_name = ‘Apple’
    address = ‘1 Infinite Loop, Cupertino, California’
    phone_number = 18002752273;
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55691209

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档