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

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

      <legend id='TIO70'><style id='TIO70'><dir id='TIO70'><q id='TIO70'></q></dir></style></legend>
        <tfoot id='TIO70'></tfoot>

        如何在两个字段上创建“双面"唯一索引?

        How To Create A #39;Two-Sided#39; Unique Index On Two Fields?(如何在两个字段上创建“双面唯一索引?)

            <tbody id='5w2tt'></tbody>
            <bdo id='5w2tt'></bdo><ul id='5w2tt'></ul>

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

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

                  <legend id='5w2tt'><style id='5w2tt'><dir id='5w2tt'><q id='5w2tt'></q></dir></style></legend>
                1. 本文介绍了如何在两个字段上创建“双面"唯一索引?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  如何有效地为表中的两个字段创建唯一索引,如下所示:create table t(a integer, b integer);

                  How can I efficiently create a unique index on two fields in a table like this: create table t (a integer, b integer);

                  其中两个不同数字的任何唯一组合不能在表格的同一行中出现多次.

                  where any unique combination of two different numbers cannot appear more than once on the same row in the table.

                  换句话说,如果一行存在使得 a=1 和 b=2,则在 a=2 和 b=1 或 a=1 和 b=2 的情况下不能存在另一行.换句话说,两个数字不能以任何顺序同时出现超过一次.

                  In order words if a row exists such that a=1 and b=2, another row cannot exist where a=2 and b=1 or a=1 and b=2. In other words two numbers cannot appear together more than once in any order.

                  我不知道这样的约束被称为什么,因此标题中的双边唯一索引"名称.

                  I have no idea what such a constraint is called, hence the 'two-sided unique index' name in the title.

                  更新:如果我在 (a,b) 列上有一个复合键,并且数据库中存在一行 (1,2),则可以插入另一行 (2,1) 没有错误.我正在寻找一种方法来防止同一对数字被多次使用以任何顺序...

                  Update: If I have a composite key on columns (a,b), and a row (1,2) exists in the database, it is possible to insert another row (2,1) without an error. What I'm looking for is a way to prevent the same pair of numbers from being used more than once in any order...

                  推荐答案

                  如何控制表中的内容,以便始终将最小的数字存储在第一列中,而将最大的数字存储在第二列中?当然,只要它意味着"同样的事情.在它进入数据库之前完成它可能更便宜.

                  How about controlling what goes into the table so that you always store the smallest number into the first column and the largest one in the second? As long as it 'means' the same thing of course. It's probably less expensive to do it before it even gets to the database.

                  如果这是不可能的,您可以按原样保存字段,但将它们按数字顺序复制到两个其他字段中,您将在其上创建主键(伪代码):

                  If this is impossible, you could save the fields as is but have them duplicated in numerical order into two OTHER fields, on which you would create the primary key (pseudo code-ish) :

                  COLUMN A : 2
                  COLUMN B : 1
                  
                  COLUMN A_PK : 1  ( if new.a < new.b then new.a else new.b )
                  COLUMN B_PK : 2  ( if new.b > new.a then new.b else new.a )
                  

                  这可以通过触发器轻松完成(如 Ronald 的响应)或在应用程序中的更高层处理.

                  This could easily be done with a trigger (as in Ronald's response) or handled higher up, in the application.

                  这篇关于如何在两个字段上创建“双面"唯一索引?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                  相关文档推荐

                  ibtmp1是非压缩的innodb临时表的独立表空间,通过innodb_temp_data_file_path参数指定文件的路径,文件名和大小,默认配置为ibtmp1:12M:autoextend,也就是说在文件系统磁盘足够的情况下,这个文件大小是可以无限增长的。 为了避免ibtmp1文件无止境的暴涨导致
                  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 按组最频繁)
                  Why Mysql#39;s Group By and Oracle#39;s Group by behaviours are different(为什么 Mysql 的 Group By 和 Oracle 的 Group by 行为不同)
                  MySQL GROUP BY DateTime +/- 3 seconds(MySQL GROUP BY DateTime +/- 3 秒)

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

                        <bdo id='P63ck'></bdo><ul id='P63ck'></ul>
                            <tbody id='P63ck'></tbody>
                          <tfoot id='P63ck'></tfoot>

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