Regardless of row, is the only column
P粉378264633
2023-09-03 17:26:33
<p>Is there a way to make the entire row unique regardless of the value in the column? </p>
<p>So, if column <code>a</code> has <code>1</code>, column <code>b</code> has <code>2</code> , then if you insert <code>2</code> into column <code>a</code>, insert <code>1</code> into column <code>b</code> , an error will be thrown. </p>
<pre class="brush:none;toolbar:false;"> --- ---
| a | b |
--- ---
| 1 | 2 |
| 2 | 1 | <- Since the above already exists, an error will be thrown when inserting
--- ---
</pre></p>
You can use triggers to enforce execution. But I can't think of a good way to achieve this using UNIQUE KEY constraints.
If you change the way you store the values so that they are in a single column, in multiple rows of the attached table, it will be easier to use UNIQUE KEY to enforce uniqueness.
If you want to indicate related items:
If you need each member to appear in only one group:
Define 2 virtual generated columns, respectively the minimum value and the maximum value, and set unique constraints on them:
ViewDemo.
Or, for MySql 8.0:
ViewDemo.