logitech2004
logitech2004

Reputation: 53

Primary key for multiple columns?

I have this MySQL Table, the PRIMARY KEY (ID).

ID  | COLOR   | SIZE
====================
| 1 |   RED   | 1  |
| 2 |   RED   | 2  |
| 3 |   RED   | 3  |
| 4 |   GREEN | 1  |
| 5 |   GREEN | 2  |
| 6 |   BLUE  | 1  |
===================

When i insert this row for example

INSERT INTO `test`(`COLOR`, `SIZE`) VALUES (RED',1);

i have a duplicate row 1, RED, 1 and 7, RED, 1 how to avoid this problem?

Thanks

Upvotes: 2

Views: 437

Answers (2)

Before you run this empty your table first or remove the duplicate row from your table than run it.

ALTER TABLE  `test` ADD UNIQUE (color, size)

Upvotes: 0

ScaisEdge
ScaisEdge

Reputation: 133410

You can use alter table

ALTER TABLE `test`
ADD UNIQUE (color, size)

Upvotes: 1

Related Questions