|
|
|
Say Hey Kid
      
Group: General Forum Members
Last Login: Monday, June 10, 2013 1:08 PM
Points: 679,
Visits: 2,038
|
|
"I dropped all the foreign key constraints to this table on the handful of tables referencing it. I dropped the index, and then I added the foreign keys references back."
I would like to point out that if you didn't use WITH CHECK CHECK to fully rescan those tables and revalidate the constraint, those FK's are untrusted by SQL Server, and do not provide their full benefits.
|
|
|
|
|
SSC-Enthusiastic
      
Group: General Forum Members
Last Login: Tuesday, May 28, 2013 11:42 AM
Points: 133,
Visits: 438
|
|
Nadrek (10/10/2011) "I dropped all the foreign key constraints to this table on the handful of tables referencing it. I dropped the index, and then I added the foreign keys references back."
I would like to point out that if you didn't use WITH CHECK CHECK to fully rescan those tables and revalidate the constraint, those FK's are untrusted by SQL Server, and do not provide their full benefits.
I will keep this in mind for the future. Do you have any documentation on this. Luckily for me, the data only lives in the tables for a short while so its all gone by now. Its put in an archive db\table by a rather large archive routine. Hey i didn't design it, i just support it. :)
Jimmy
"I'm still learning the things i thought i knew!"
|
|
|
|
|
Right there with Babe
      
Group: General Forum Members
Last Login: Tuesday, June 04, 2013 11:37 PM
Points: 729,
Visits: 1,335
|
|
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.
Ole Kristian Velstadbråten Bangås - Virinco - Facebook - Twitter
Concatenating Row Values in Transact-SQL
|
|
|
|
|
SSC-Enthusiastic
      
Group: General Forum Members
Last Login: Tuesday, May 28, 2013 11:42 AM
Points: 133,
Visits: 438
|
|
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!"
|
|
|
|
|
Right there with Babe
      
Group: General Forum Members
Last Login: Tuesday, June 04, 2013 11:37 PM
Points: 729,
Visits: 1,335
|
|
|
|
|
|
SSChampion
        
Group: General Forum Members
Last Login: Yesterday @ 3:07 PM
Points: 10,613,
Visits: 11,959
|
|
|
|
|
|
Forum Newbie
      
Group: General Forum Members
Last Login: Tuesday, January 08, 2013 5:56 PM
Points: 5,
Visits: 52
|
|
Hi,
I hope you guys can help me get something straightened out here. I ran this query on the Master DB:
select f.name , i.name , 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 join sys.indexes i on i.object_id = f.referenced_object_id and i.index_id = f.key_index_id With these results.
name name tablename is_unique is_primary_key type_desc key_index_id FK_ProjectWorkOrders_Projects PK_Projects Projects 1 1 CLUSTERED 1 FK_ProjectFacilities_Projects PK_Projects Projects 1 1 CLUSTERED 1 FK_WorkOrderTypeEmployees_WorkOrderTypes PK_WorkOrderTypes WorkOrderTypes 1 1 CLUSTERED 1 FK_ProjectWorkOrders_WorkOrderTypes PK_WorkOrderTypes WorkOrderTypes 1 1 CLUSTERED 1 FK_ProjectWorkOrderEmployees_ProjectWorkOrders PK_ProjectWorkOrders ProjectWorkOrders 1 1 CLUSTERED 1
I also ran this query on the DB where these tables are and got 156 results which is pretty much what I expected.
Why are only some of the indexes showing up in the Master DB? These are the most recent table additions and foreign keys.
Very sorry if this is too far off topic. If so I will repost as a new thread.
Thank you, Richard
|
|
|
|
|
SSChampion
        
Group: General Forum Members
Last Login: Yesterday @ 3:07 PM
Points: 10,613,
Visits: 11,959
|
|
rstelma (10/11/2011)
Hi, I hope you guys can help me get something straightened out here. I ran this query on the Master DB: select f.name , i.name , 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 join sys.indexes i on i.object_id = f.referenced_object_id and i.index_id = f.key_index_id With these results. name name tablename is_unique is_primary_key type_desc key_index_id FK_ProjectWorkOrders_Projects PK_Projects Projects 1 1 CLUSTERED 1 FK_ProjectFacilities_Projects PK_Projects Projects 1 1 CLUSTERED 1 FK_WorkOrderTypeEmployees_WorkOrderTypes PK_WorkOrderTypes WorkOrderTypes 1 1 CLUSTERED 1 FK_ProjectWorkOrders_WorkOrderTypes PK_WorkOrderTypes WorkOrderTypes 1 1 CLUSTERED 1 FK_ProjectWorkOrderEmployees_ProjectWorkOrders PK_ProjectWorkOrders ProjectWorkOrders 1 1 CLUSTERED 1 I also ran this query on the DB where these tables are and got 156 results which is pretty much what I expected. Why are only some of the indexes showing up in the Master DB? These are the most recent table additions and foreign keys. Very sorry if this is too far off topic. If so I will repost as a new thread. Thank you, Richard
I wouldn't expect to see any results for user tables when running that query. Those DMV's are scoped to the database that you are in. If there are FK's showing in master that means the tables, indexes, and foreign keys were created in master instead of the user database or in addition to the user database.
Jack Corbett
Applications Developer Don't let the good be the enemy of the best. -- Paul Fleming
Check out these links on how to get faster and more accurate answers: Forum Etiquette: How to post data/code on a forum to get the best help Need an Answer? Actually, No ... You Need a Question How to Post Performance Problems Crosstabs and Pivots or How to turn rows into columns Part 1 Crosstabs and Pivots or How to turn rows into columns Part 2
|
|
|
|
|
Forum Newbie
      
Group: General Forum Members
Last Login: Tuesday, January 08, 2013 5:56 PM
Points: 5,
Visits: 52
|
|
Hi Jack,
Damn database gremlins.
Now when I run the query on the Master I get no results and 156 on the DB where the tables exist and where I expect to see the Foreign Keys. I swear I ran that on the Master and kept switching back and forth.
Anyway... thank you. Very much appreciate your reply.
Richard
|
|
|
|
|
Mr or Mrs. 500
      
Group: General Forum Members
Last Login: Saturday, June 15, 2013 2:08 PM
Points: 525,
Visits: 624
|
|
Thank you for sharing this obscure issue. I hope it saves me time someday.
|
|
|
|