6

Is there any concern in referencing a unique index from a foreign key table?

Using SQL Server 2008 R2

asked Feb 8, 2013 at 6:05

1 Answer 1

9

No, a FOREIGN KEY constraint can reference a column combination that is either the PRIMARY KEY or has a UNIQUE constraint or has a UNIQUE INDEX.

Copied from MSDN page, CREATE TABLE:

FOREIGN KEY REFERENCES
Is a constraint that provides referential integrity for the data in the column or columns. FOREIGN KEY constraints require that each value in the column exists in the corresponding referenced column or columns in the referenced table. FOREIGN KEY constraints can reference only columns that are PRIMARY KEY or UNIQUE constraints in the referenced table or columns referenced in a UNIQUE INDEX on the referenced table. Foreign keys on computed columns must also be marked PERSISTED.

The only difference between the first (primary key) option and the other two is that unique* constraints and indexes can be defined on nullable columns. Primary key columns have to be NOT NULL.

* Note that there are some slight differences between these in SQL-Server (unique indexes offer some options that are not available with unique constraints): When should I use a unique constraint instead of a unique index?

For example, (as @gbn points), a unique index can be partial. But then, it can't be referenced by a FK.

answered Feb 8, 2013 at 8:24
2
  • 2
    Note: if the parent unique index is filtered, you can't use it for an FK. Minor point though Commented Feb 8, 2013 at 11:00
  • @gbn This seems to be the same in PostgreSQL. dba.stackexchange.com/q/339836/50410 Commented May 28, 2024 at 10:26

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.