Sven Janning Sven Janning - 1 year ago 83
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 Source

Create a unique index on Col A and Col B:


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

ANSI SQL example:

ALTER TABLE mytable ADD CONSTRAINT unique_index UNIQUE (ColA,ColB);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download