V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
corningsun
V2EX  ›  PostgreSQL

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

  •  
  •   corningsun · 2018-05-18 09:55:06 +08:00 · 2436 次点击
    这是一个创建于 2142 天前的主题,其中的信息可能已经有所发展或是发生改变。

    对于用户表

    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 条回复    2018-05-21 15:53:19 +08:00
    CRVV
        1
    CRVV  
       2018-05-18 10:32:51 +08:00   ❤️ 1
    CREATE UNIQUE INDEX unique_user_idx ON table_user (user_telephone) WHERE user_status <> -1;
    corningsun
        2
    corningsun  
    OP
       2018-05-18 10:46:42 +08:00
    @CRVV

    可行,灰常感谢~~~
    RorschachZZZ
        3
    RorschachZZZ  
       2018-05-18 11:43:23 +08:00
    @corningsun 请问是 mysql 吗?
    corningsun
        4
    corningsun  
    OP
       2018-05-18 18:05:38 +08:00
    @RorschachZZZ

    PostgreSQL
    whx20202
        5
    whx20202  
       2018-05-21 15:53:19 +08:00
    这个应该是部分索引,据我所知 mysql 没有,如果迁移可能需要留神
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   4123 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 28ms · UTC 05:24 · PVG 13:24 · LAX 22:24 · JFK 01:24
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.