<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 中强制执行唯一约束?

        时间:2024-04-16
        <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 中强制执行唯一约束?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:在关系数据库中维护子类的完整性 下一篇:多个但互斥的外键 - 这是要走的路吗?

                  相关文章

                    <legend id='1b4j9'><style id='1b4j9'><dir id='1b4j9'><q id='1b4j9'></q></dir></style></legend>

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

                      • <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>