Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 

Get your favorite SSC scripts directly in SSMS with the free SQL Scripts addin. Search for scripts directly from SSMS, and instantly access any saved scripts in your SSC briefcase from the favorites tab.
Download now (direct download link)

Find botched constraints, idxs, fks

By William Talada,

ShowColumnUsage presents table columns in a format for you to see all constraints, indexes, and foreign keys affecting each column. You'll be shocked at how many errors you'll find in your constraints and indexes within seconds of running it. I never deploy to customers without running this first.

I'll walk you through it quickly. Create the proc in msdb and do the following to see the results for all tables:

exec ShowColumnUsage '%'


Notice that all constraints and indexes numbered 1 are clustered.

Now let's see all the botched indexes in msdb...

-- non-unique clustered index is in reverse order from the foreign key definition
-- seems illogical but maybe they have their reasons
exec ShowColumnUsage 'DTA_reports_querytable'

-- idx2 is a complete waste since pk1.1 has it covered
-- only makes sense in a few critical situations
exec ShowColumnUsage 'log_shipping_primary_secondaries'

-- ak2 and udx3 are equal so one is redundant
exec ShowColumnUsage 'log_shipping_primary_databases'

-- this one is just plain weird having an alternate key that is part of a primary key???
exec ShowColumnUsage 'sysdtspackages'

Now run it in your databases and fix errors you could never see before.

 

Total article views: 1414 | Views in the last 30 days: 3
 
Related Articles
FORUM

Violation of PRIMARY KEY constraint

Violation of PRIMARY KEY constraint

FORUM

Primary keys and indexes

Primary keys and indexes

FORUM

Constraints

Violating the primary key constraint

FORUM

Can't Delete Index b/c of Constraint

Can't Delete Index b/c of Constraint

FORUM

Creating a Primary Key over an existing Unique, Clustered Index

Primary Key, Clustered Index, Modification

Tags
constraints    
foreign keys (fk)    
indexing    
table    
 
Contribute

Join the most active online SQL Server Community

SQL knowledge, delivered daily, free:

Email address:  

You make SSC a better place

As a member of SQLServerCentral, you get free access to loads of fresh content: thousands of articles and SQL scripts, a library of free eBooks, a weekly database news roundup, a great Q & A platform… And it’s our huge, buzzing community of SQL Server Professionals that makes it such a success.

Join us!

Steve Jones
Editor, SQLServerCentral.com

Already a member? Jump in:

Email address:   Password:   Remember me: Forgotten your password?
Steve Jones