search

Home  >  Q&A  >  body text

mysql - sql 如何约束多列中所有值均不同?

如有一表结构:

CREATE TABLE t1(
    c1 INT,
    c2 INT,
    c3 INT
);

我如何写约束使得对任何两行,第一行的 c1, c2, c3 中任何一个与第二行的 c1, c2, c3 的任何一个均不同,且每一行的 c1, c2, c3 彼此不同?

Update:
查了 stackexchange 人家表示 unique 约束写不了,不过可以写 check 解决~
看看童鞋们有没有好办法。。。

伊谢尔伦伊谢尔伦2768 days ago585

reply all(3)I'll reply

  • PHP中文网

    PHP中文网2017-04-17 12:59:32

    You can only write check with constraints

    reply
    0
  • PHP中文网

    PHP中文网2017-04-17 12:59:32

    I feel that this constraint may not be achieved.
    If you think about it differently, you need to get the relevant rows and make a judgment when performing INSERT, UPDATE, and DELETE on the table.

    reply
    0
  • ringa_lee

    ringa_lee2017-04-17 12:59:32

    Another way of thinking: Put the multi-column values ​​to be stored into one column, add a unique constraint to this column, and add an additional attribute identifier for different purposes

    reply
    0
  • Cancelreply