• okbangas (10/11/2011)


    This does indeed happen on both SQL Server 2008 R2 (RTM) and SQL Server 2008 SP2:

    use tempdb;

    go

    create table t1 (

    id int identity primary key,

    val varchar(10)

    );

    create table t2 (

    id int identity primary key,

    val varchar(10)

    );

    go

    create unique nonclustered index ix_t1 on t1 (id);

    go

    alter table t2

    add constraint fk_t2_t1 foreign key (id) references t1 (id);

    go

    select

    f.name as ConstraintName,

    i.name as IndexName,

    object_name(i.object_id) as TableName,

    i.is_unique,

    i.is_primary_key,

    i.type_desc,

    f.key_index_id

    from

    sys.foreign_keys f

    inner join

    sys.indexes i on

    i.object_id = f.referenced_object_id and

    i.index_id = f.key_index_id

    go

    drop table t2;

    drop table t1;

    Actually, Kimberly Tripp has even a Post on it. As far as I can see, this is a very nice feature optimizing the performance of foreign key constraint validation, and not even close to a bug.

    Can you run my script that is attached on your environment? I would like to see if you get the error that i did on 2005 in your 2008 environment. A guess is that perhaps the association still happens, but you don't get the error when dropped due to some new code. Out of curiosity I will look into it that.

    As far as being a feature, I don't like features that cause errors. Perhaps the implementation could be different. Like it should auto-switch to the best index available, and if no index create whatever object is needed to hold everything together. Based on my guess above, maybe that's why there is no error in 2008.

    Jimmy

    "I'm still learning the things i thought i knew!"