How to set the combination of two colums to be unique?
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)
- Which statement will alter this table, so that开发者_如何学运维 the combination of article_id and user_id is unique?
- Which statement tells me, if the DB has already been altered?
Thanks,
Markus
You need to create unique index:
CREATE UNIQUE INDEX unique_users_articles ON users_articles (article_id, user_id)
Please clarify why you need it?
UPDATE: UseIF 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)
精彩评论