Sven Janning Sven Janning - 1 month ago 7
SQL Question

PHP SQL / Duplicate entries

I have a sql database and i donĀ“t know, how to solve the following problem. For example - the database looks like this

Col A | Col B | Col C

It should be possible that Col A has some dublicate entries / Col B has some duplicate entries - but it should not be possible that the pair of Col A & B can be listed twice. How do i have to prepare the database and insert statement.

Answer

Create a unique index on Col A and Col B:

MySQL:

ALTER TABLE `mytable` ADD UNIQUE `unique_index`(`ColA`, `ColB`);

ANSI SQL example:

ALTER TABLE mytable ADD CONSTRAINT unique_index UNIQUE (ColA,ColB);