开发者

How to set the combination of two colums to be unique?

开发者 https://www.devze.com 2023-01-21 06:52 出处:网络
I have a question about DB modeling, I have a table created as following: CREATE TABLE \"users_articles\"

I have a question about DB modeling, I have a table created as following:

CREATE TABLE "users_articles" 
  ("id"         INTEGER  PRIMARY KEY  NOT NULL,
   "article_id" INTEGER,
   "user_id"    INTEGER)
  1. Which statement will alter this table, so that开发者_如何学运维 the combination of article_id and user_id is unique?
  2. Which statement tells me, if the DB has already been altered?

Thanks,

Markus


  1. You need to create unique index:

    CREATE UNIQUE INDEX unique_users_articles ON users_articles (article_id, user_id)

  2. Please clarify why you need it?
    UPDATE: Use IF NOT EXISTS to ignore index creation if it already exists:

    CREATE UNIQUE INDEX IF NOT EXISTS unique_users_articles ON users_articles (article_id, user_id)

0

精彩评论

暂无评论...
验证码 换一张
取 消