Postgres unique multi-column index for join table

前端 未结 2 2037
春和景丽
春和景丽 2021-01-01 04:11

I have a many-to-many join table in Postgres that I would like to index to A) increase performance (obviously) and B) enforce uniqueness. For example:

a_id |         


        
2条回答
  •  灰色年华
    2021-01-01 05:09

    In addition to using the PRIMARY KEY and UNIQUE syntax as explained by @Michael Buen, you can also create an explicit index:

    CREATE UNIQUE INDEX foo_a_b ON tbl(a_id, b_id);
    

    This is just a normal, multi-column b-tree index (which is exactly what the KEY syntax creates implicitly).

提交回复
热议问题