Unique key vs. unique index on SQL Server 2008

前端 未结 7 1511
抹茶落季
抹茶落季 2021-02-01 00:59

I have a table called countries and I define the country_name column to be unique by creating a “Index/Key” of type “Unique Key” on SQL Server 2008 R2.

7条回答
  •  深忆病人
    2021-02-01 01:11

    If you are using SqlMetal.exe to output DBML or LinqToSql entities:

    • If a foreign key uses a unique key, then you will get an association as expected.
    • If a foreign key uses a unique index, it won't show up.

    The reason is in the implementation of SqlMetal. It queries the database information schema, specifically key column usage. Unique keys are represented there, but unique indexes are not.

    SELECT TABLE_NAME, CONSTRAINT_NAME, COLUMN_NAME, ORDINAL_POSITION
    FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE;
    

提交回复
热议问题