List of tables with number of records CREATETABLE #Tab ( Table_Name [varchar](max), Total_Records int ); EXEC sp_MSForEachTable @command1=' Insert Into #Tab(Table_Name, Total_Records)
SELECT ''?'', COUNT(*) FROM ?' SELECT * FROM #Tab t ORDERBY t.Total_Records DESC; DROPTABLE #Tab;
List of Primary Key and Foreign Key for a particular table SELECT DISTINCT Constraint_Name AS [Constraint], Table_Schema AS [Schema], Table_Name AS [TableName] FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE WHERE INFORMATION_SCHEMA.KEY_COLUMN_USAGE.TABLE_NAME='Table_Name' GO
List of Primary Key and Foreign Key for Whole Database SELECT DISTINCT Constraint_Name AS [Constraint], Table_Schema AS [Schema], Table_Name AS [TableName] FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE GO
Get all table that don’t have identity column: SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES where Table_NAME NOTIN ( SELECTDISTINCT c.TABLE_NAME FROM INFORMATION_SCHEMA.COLUMNS c INNER JOIN sys.identity_columns ic on (c.COLUMN_NAME=ic.NAME)) AND TABLE_TYPE ='BASE TABLE'