Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Unindexed Foreign Keys
Message
Information générale
Forum:
Microsoft SQL Server
Catégorie:
Syntaxe SQL
Divers
Thread ID:
01231989
Message ID:
01232272
Vues:
22
>I am running Sql Server 2000. I would like a query that i could run that would bring me back a list of all foreign key constraints that do not currently have an index associated with the fields being referenced. Any where i can find this?

Try (not tested much)
SELECT ccu.* 
	FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS tc
	JOIN INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE ccu 
		ON ccu.TABLE_NAME = tc.TABLE_NAME AND ccu.CONSTRAINT_NAME =tc.CONSTRAINT_NAME
			AND tc.CONSTRAINT_TYPE = 'FOREIGN KEY'
	WHERE NOT EXISTS ( SELECT * FROM (
		-- List of all columns in indexes
		SELECT DISTINCT OBJECT_NAME(ik.id) AS TableName,
				COL_NAME(ik.id, ik.colid) AS ColumnName
			FROM sysindexkeys ik 
				INNER JOIN sysindexes i 
					ON ik.id = i.id AND ik.indid = i.indid
			WHERE INDEXPROPERTY(i.id, i.name, 'IsAutoStatistics') = 0
				AND OBJECTPROPERTY(i.id, 'IsSystemTable') = 0						
					) ikl 
						WHERE ikl.TableName = tc.TABLE_NAME AND ccu.COLUMN_NAME = ikl.ColumnName)
--sb--
Précédent
Répondre
Fil
Voir

Click here to load this message in the networking platform