SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Holy Foreign Keys and Indexes


Holy Foreign Keys and Indexes

Author
Message
Nadrek
Nadrek
SSCrazy
SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)

Group: General Forum Members
Points: 2445 Visits: 2733
"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.
imSQrLy
imSQrLy
SSC Veteran
SSC Veteran (229 reputation)SSC Veteran (229 reputation)SSC Veteran (229 reputation)SSC Veteran (229 reputation)SSC Veteran (229 reputation)SSC Veteran (229 reputation)SSC Veteran (229 reputation)SSC Veteran (229 reputation)

Group: General Forum Members
Points: 229 Visits: 473
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. Smile

Jimmy

"I'm still learning the things i thought i knew!"
okbangas
okbangas
SSCommitted
SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)

Group: General Forum Members
Points: 1563 Visits: 1387
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
imSQrLy
imSQrLy
SSC Veteran
SSC Veteran (229 reputation)SSC Veteran (229 reputation)SSC Veteran (229 reputation)SSC Veteran (229 reputation)SSC Veteran (229 reputation)SSC Veteran (229 reputation)SSC Veteran (229 reputation)SSC Veteran (229 reputation)

Group: General Forum Members
Points: 229 Visits: 473
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!"
okbangas
okbangas
SSCommitted
SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)

Group: General Forum Members
Points: 1563 Visits: 1387
I do get the same error message, yes:
Msg 3723, Level 16, State 6, Line 2
An explicit DROP INDEX is not allowed on index 't1.ix_t1'. It is being used for FOREIGN KEY constraint enforcement.




Ole Kristian Velstadbråten Bangås - Virinco - Facebook - Twitter

Concatenating Row Values in Transact-SQL
Jack Corbett
  Jack Corbett
SSC-Insane
SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)

Group: General Forum Members
Points: 24002 Visits: 14905
I don't think I'd call this a bug. SQL Server can use the Primary Key or a Unique Constraint/index to as the reference column(s) for a foreign key. In this case (or these cases for those who have experienced it), SQL Server is probably using a unique clustered index to enforce a foreign key because it believes that it will improve performance. I'd have to assume that the lead column in the unique clustered index is the same column as the primary key. I'd be checking to see if both indexes are needed and perhaps make the unique clustered index the PK.



Jack Corbett

Applications Developer

Don't let the good be the enemy of the best. -- Paul Fleming
At best you can say that one job may be more secure than another, but total job security is an illusion. -- Rod at work

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
rstelma
rstelma
SSC Rookie
SSC Rookie (41 reputation)SSC Rookie (41 reputation)SSC Rookie (41 reputation)SSC Rookie (41 reputation)SSC Rookie (41 reputation)SSC Rookie (41 reputation)SSC Rookie (41 reputation)SSC Rookie (41 reputation)

Group: General Forum Members
Points: 41 Visits: 67
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
Jack Corbett
  Jack Corbett
SSC-Insane
SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)

Group: General Forum Members
Points: 24002 Visits: 14905
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
At best you can say that one job may be more secure than another, but total job security is an illusion. -- Rod at work

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
rstelma
rstelma
SSC Rookie
SSC Rookie (41 reputation)SSC Rookie (41 reputation)SSC Rookie (41 reputation)SSC Rookie (41 reputation)SSC Rookie (41 reputation)SSC Rookie (41 reputation)SSC Rookie (41 reputation)SSC Rookie (41 reputation)

Group: General Forum Members
Points: 41 Visits: 67
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
Misha_SQL
Misha_SQL
Ten Centuries
Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)

Group: General Forum Members
Points: 1190 Visits: 1009
Thank you for sharing this obscure issue. I hope it saves me time someday.



Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search