Marty
Marty

Reputation: 39456

MySQL - Making two or more columns together unique

I'm creating a database (MySQL) which will store information on Magic: The Gathering cards.

At the moment, I have the card names as unique, however I've realized just now that this is an issue with many cards being available across multiple editions, example:

enter image description here

I am also storing the edition that the card belongs to (as an ID that links to an editions table).

Is it possible to define the combination of the two columns name and edition unique rather than just name, so that I don't have issues when trying to insert both the above cards?

Upvotes: 1

Views: 114

Answers (2)

user751651
user751651

Reputation:

You can do this:

UNIQUE KEY column_Name (column1,column2)

Upvotes: 2

iouri
iouri

Reputation: 2929

Yes, you can create compound unique indexes:

UNIQUE KEY `names_editions` (`name`,`edition`)

Upvotes: 1

Related Questions