V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
corningsun
0.05D

多列唯一索引可以添加限制条件吗

  •  
  •   corningsun · May 18, 2018 · 3026 views
    This topic created in 2908 days ago, the information mentioned may be changed or developed.

    对于用户表

    table_user {
      id,
      user_name,
      user_telephone,
      user_status
    }
    

    其中 user_status:1,启用; 2, 禁用;-1,删除;

    我想保证未删除的用户中,用户手机号是唯一的,该如何创建索引?

    unique index (user_telephone, user_status) where user_status <> -1 ???

    5 replies    2018-05-21 15:53:19 +08:00
    CRVV
        1
    CRVV  
       May 18, 2018   ❤️ 1
    CREATE UNIQUE INDEX unique_user_idx ON table_user (user_telephone) WHERE user_status <> -1;
    corningsun
        2
    corningsun  
    OP
       May 18, 2018
    @CRVV

    可行,灰常感谢~~~
    RorschachZZZ
        3
    RorschachZZZ  
       May 18, 2018
    @corningsun 请问是 mysql 吗?
    corningsun
        4
    corningsun  
    OP
       May 18, 2018
    @RorschachZZZ

    PostgreSQL
    whx20202
        5
    whx20202  
       May 21, 2018
    这个应该是部分索引,据我所知 mysql 没有,如果迁移可能需要留神
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   2544 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 43ms · UTC 09:13 · PVG 17:13 · LAX 02:13 · JFK 05:13
    ♥ Do have faith in what you're doing.