<legend id='5I8nr'><style id='5I8nr'><dir id='5I8nr'><q id='5I8nr'></q></dir></style></legend>
      • <bdo id='5I8nr'></bdo><ul id='5I8nr'></ul>

      <small id='5I8nr'></small><noframes id='5I8nr'>

        <tfoot id='5I8nr'></tfoot>
        <i id='5I8nr'><tr id='5I8nr'><dt id='5I8nr'><q id='5I8nr'><span id='5I8nr'><b id='5I8nr'><form id='5I8nr'><ins id='5I8nr'></ins><ul id='5I8nr'></ul><sub id='5I8nr'></sub></form><legend id='5I8nr'></legend><bdo id='5I8nr'><pre id='5I8nr'><center id='5I8nr'></center></pre></bdo></b><th id='5I8nr'></th></span></q></dt></tr></i><div id='5I8nr'><tfoot id='5I8nr'></tfoot><dl id='5I8nr'><fieldset id='5I8nr'></fieldset></dl></div>

        如何在 MySQL 中强制执行唯一约束?

        How to enforce unique constraint in MySQL?(如何在 MySQL 中强制执行唯一约束?)
        <legend id='OCbb6'><style id='OCbb6'><dir id='OCbb6'><q id='OCbb6'></q></dir></style></legend>

        <small id='OCbb6'></small><noframes id='OCbb6'>

      1. <tfoot id='OCbb6'></tfoot>
              <tbody id='OCbb6'></tbody>
            <i id='OCbb6'><tr id='OCbb6'><dt id='OCbb6'><q id='OCbb6'><span id='OCbb6'><b id='OCbb6'><form id='OCbb6'><ins id='OCbb6'></ins><ul id='OCbb6'></ul><sub id='OCbb6'></sub></form><legend id='OCbb6'></legend><bdo id='OCbb6'><pre id='OCbb6'><center id='OCbb6'></center></pre></bdo></b><th id='OCbb6'></th></span></q></dt></tr></i><div id='OCbb6'><tfoot id='OCbb6'></tfoot><dl id='OCbb6'><fieldset id='OCbb6'></fieldset></dl></div>

              • <bdo id='OCbb6'></bdo><ul id='OCbb6'></ul>

                  本文介绍了如何在 MySQL 中强制执行唯一约束?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我定义了一个 MySQL 表:

                  I have a MySQL table defined:

                  File
                  --------------------------
                  ID int(11) PK
                  name varchar(100)
                  customerId int(11) FK
                  isPrimaryImage tinyint(1)
                  ....
                  


                  我在这里读到 MySQL 不支持带有 WHERE 条件的 UNIQUE CONSTRAINT.对于每个 customerId,我们只有一个 primaryImage = 1.


                  I've read here that MySQL doesn't support a UNIQUE CONSTRAINT with a WHERE condition. For each customerId we'd have only one primaryImage = 1.

                  那么我还能做些什么来强制执行此约束?

                  So what else can I do to enforce this constraint?

                  推荐答案

                  MySQL 完美支持唯一约束.

                  不过,它不支持部分约束/索引,因此您需要使用 NULL 而不是 0 来标记非主要图像.

                  It does not support partial constraints/indexes, though, so you would need to mark non-primary images with a NULL instead of 0.

                  ALTER TABLE file ADD CONSTRAINT ux_file_customer_primary 
                  UNIQUE (customerId, isPrimaryImage)
                  

                  您可以在 isPrimaryImage 中插入任意数量的 NULL 值,但每个客户只能插入一个非空值.

                  You can insert arbitrary number of NULL values into isPrimaryImage but only one non-null value per customer.

                  这篇关于如何在 MySQL 中强制执行唯一约束?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  SQL query to group by day(按天分组的 SQL 查询)
                  What does SQL clause quot;GROUP BY 1quot; mean?(SQL 子句“GROUP BY 1是什么意思?意思是?)
                  MySQL groupwise MAX() returns unexpected results(MySQL groupwise MAX() 返回意外结果)
                  MySQL SELECT most frequent by group(MySQL SELECT 按组最频繁)
                  Include missing months in Group By query(在 Group By 查询中包含缺失的月份)
                  Why Mysql#39;s Group By and Oracle#39;s Group by behaviours are different(为什么 Mysql 的 Group By 和 Oracle 的 Group by 行为不同)
                      <legend id='1b4j9'><style id='1b4j9'><dir id='1b4j9'><q id='1b4j9'></q></dir></style></legend>

                      1. <small id='1b4j9'></small><noframes id='1b4j9'>

                          <tbody id='1b4j9'></tbody>
                          • <bdo id='1b4j9'></bdo><ul id='1b4j9'></ul>
                            <tfoot id='1b4j9'></tfoot>
                            <i id='1b4j9'><tr id='1b4j9'><dt id='1b4j9'><q id='1b4j9'><span id='1b4j9'><b id='1b4j9'><form id='1b4j9'><ins id='1b4j9'></ins><ul id='1b4j9'></ul><sub id='1b4j9'></sub></form><legend id='1b4j9'></legend><bdo id='1b4j9'><pre id='1b4j9'><center id='1b4j9'></center></pre></bdo></b><th id='1b4j9'></th></span></q></dt></tr></i><div id='1b4j9'><tfoot id='1b4j9'></tfoot><dl id='1b4j9'><fieldset id='1b4j9'></fieldset></dl></div>