Luís Tiago Luís Tiago - 2 months ago 18
SQL Question

SQL FOREIGN KEY's

How do prevent a table with 2 FOREIGN key's from having repeated rows with the same value.

enter image description here

Thks in advance.

Answer

Use a unique index or constraint:

alter table example
    add constraint unq_example_fk1_fk2 unique (fk1, fk2);

A unique constraint and unique index do essentially the same thing. So, you can also do:

create unique index unq_example_fk_fk on example(fk1, fk2);
Comments