0

I have a database something which appears like

Field1  |      Field 2     |     Field 3
abcdef          16 Apr            first
uyiuyt          16 Apr            second
abcdef          20 May            third
uyiuyt          20 May            four
abcdef          15 Jun            first
uyiuyt          15 Jun            second

I thought making Field1 and Field2 as unique keys so field1 and field2 are not getting repeated. I mean Field 1 can be repeated but for different date values.

I wanted the db to allow

 abcdef  16 Apr
 abcdef  20 May
 abcdef  15 Jun

But not

abcdef   16 Apr
 abcdef   16 Apr

But in db table, when I assign unique index to field1 and field2, even second row uyiuyt 16 Apr is not getting inserted, throwing the error duplicate entry for Field2.

Can somebody help me to solve this issue? thanks

  • Take a look at this: http://stackoverflow.com/questions/635937/how-do-i-specify-unique-constraint-for-multiple-columns-in-mysql – g_tec Apr 15 '14 at 22:21

1 Answers1

0

You can create an index involving 2 fields. As the code:

CREATE UNIQUE INDEX indice ON table_name(Field1, Field2);

OR

ALTER TABLE table_name ADD UNIQUE INDEX(Field1, Field2);
Lucas Henrique
  • 1,380
  • 1
  • 11
  • 15