<legend id='P0H6x'><style id='P0H6x'><dir id='P0H6x'><q id='P0H6x'></q></dir></style></legend>

  • <small id='P0H6x'></small><noframes id='P0H6x'>

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

        具有重复 NULL 的 SQL Server UNIQUE 约束

        SQL Server UNIQUE constraint with duplicate NULLs(具有重复 NULL 的 SQL Server UNIQUE 约束)
      1. <tfoot id='V4KvO'></tfoot>

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

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

                2. <legend id='V4KvO'><style id='V4KvO'><dir id='V4KvO'><q id='V4KvO'></q></dir></style></legend>
                  本文介绍了具有重复 NULL 的 SQL Server UNIQUE 约束的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  可能的重复:
                  如何创建唯一约束这也允许 sql server 中的空值

                  我有一个表,我需要在其中强制列具有唯一值.此列必须可以为空,并且根据业务逻辑,应允许多个 NULL 值,而其他重复值则不允许.

                  I have a table where I need to force a column to have unique values. This column must be nullable and by business logic multiple NULL values should be permitted, whereas other duplicate values are not.

                  SQL Server UNIQUE 约束在这种情况下不好用,因为它将 NULL 视为常规值,因此它会拒绝重复的 NULL.

                  SQL Server UNIQUE constraint is no good in this situation because it considers NULL as regular values, so it will reject duplicate NULLs.

                  目前,价值唯一性是由 BLL 授予的,所以我不是在寻找一个肮脏的黑客来使它工作.我只想知道是否有一个干净的解决方案来在数据库中强制执行此约束.

                  Currently, value uniqueness is granted by the BLL so I'm not looking for a dirty hack to make it work. I just would like to know if there is a clean solution to enforce this constraint in the DB.

                  是的,我知道我可以编写一个触发器来做到这一点:触发器是唯一的解决方案吗?(还是最好的解决方案?)

                  And yeah, I know I can write a trigger to do that: is a trigger the only solution? (or the best solution anyway?)

                  推荐答案

                  如果您使用的是 SQL Server 2008(不适用于早期版本),则存在过滤索引的概念.您可以在表的过滤子集上创建索引.

                  If you're using SQL Server 2008 (won't work for earlier version) there is the concept of a filtered index. You can create the index on a filtered subset of the table.

                  CREATE UNIQUE INDEX indexName ON tableName(columns) INCLUDE includeColumns 
                  WHERE columnName IS NOT NULL
                  

                  这篇关于具有重复 NULL 的 SQL Server UNIQUE 约束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                  相关文档推荐

                  Maximum stored procedure, function, trigger, or view nesting level exceeded (limit 32)(超出最大存储过程、函数、触发器或视图嵌套级别(限制 32))
                  How to debug stored procedure in VS 2015?(如何在 VS 2015 中调试存储过程?)
                  How to find a text inside SQL Server procedures / triggers?(如何在 SQL Server 程序/触发器中查找文本?)
                  SQL Server stored procedure return code oddity(SQL Server 存储过程返回码奇怪)
                  Procedure to loop through comma separated string is not working(遍历逗号分隔字符串的过程不起作用)
                  Call a stored procedure for each row returned by a query in MySQL(为 MySQL 中查询返回的每一行调用一个存储过程)
                3. <tfoot id='c9XYQ'></tfoot>

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

                        <tbody id='c9XYQ'></tbody>

                    • <small id='c9XYQ'></small><noframes id='c9XYQ'>

                      <legend id='c9XYQ'><style id='c9XYQ'><dir id='c9XYQ'><q id='c9XYQ'></q></dir></style></legend>
                          <bdo id='c9XYQ'></bdo><ul id='c9XYQ'></ul>