You are on page 1of 4

SQL Queries

Space for each table


SELECT sum ( used_page_count ) * 8 AS SizeKB, sum(row_count) AS [RowCount], object_name ( object_id ) AS TableName FROM sys.dm_db_partition_stats WHERE index_id=0 OR index_id=1 GROUP BY object_id ORDER BY sum ( used_page_count ) DESC;

List of Tables in a FileGroup


select case when indexes.type_desc in ('HEAP','CLUSTERED') then 'Table-' + indexes.type_desc else ' NC Index' end as indexType, rtrim(cast( case when indexProperty(objects.object_id,indexes.name,'IsUnique') = 1 then 'unique ' else '' end + case when isNull(objectProperty(object_id(schemas.name + '.' + indexes.name),'IsConstraint'),0) = 1 then 'constraint ' else '' end + case when indexProperty(objects.object_id,indexes.name,'IsAutoStatistics') = 1 then 'auto ' else '' end + case when indexProperty(objects.object_id,indexes.name,'IsStatistics') = 1 then 'statistics ' else '' end + case when indexProperty(objects.object_id,indexes.name,'IsHypothetical') = 1 then 'hypothetical ' else '' end as varchar(30))) as indexProperties, schemas.name + '.' + objects.name as tableName, coalesce(indexes.name,'') as indexName, filegroups.name as filegroup from join sys.indexes as indexes sys.objects as objects on indexes.object_id = objects.object_id join sys.schemas as schemas on objects.schema_id = schemas.schema_id join sys.filegroups as filegroups on indexes.data_space_id = filegroups.data_space_id

where objectproperty(indexes.object_id,'IsMSShipped') = 0 order by tableName, case when indexes.type_desc in ('HEAP','CLUSTERED') then 0 else 1 end

Table description
SELECT o.name AS [Table Name], o.type, c.name AS [Col Name], s.name AS [Col Type], c.prec, c.scale, c.isnullable FROM dbo.sysobjects AS o INNER JOIN dbo.syscolumns AS c ON c.id = o.id INNER JOIN dbo.systypes AS s ON c.xtype = s.xtype WHERE ( o.type = 'U' and o.name = 'table_name' and s.name<>'sysname' )

Foreign Keys on a table


SELECT K_Table = FK.TABLE_NAME, FK_Column = CU.COLUMN_NAME, PK_Table = PK.TABLE_NAME, PK_Column = PT.COLUMN_NAME, Constraint_Name = C.CONSTRAINT_NAME FROM INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS C INNER JOIN INFORMATION_SCHEMA.TABLE_CONSTRAINTS FK ON C.CONSTRAINT_NAME = FK.CONSTRAINT_NAME INNER JOIN INFORMATION_SCHEMA.TABLE_CONSTRAINTS PK ON C.UNIQUE_CONSTRAINT_NAME = PK.CONSTRAINT_NAME INNER JOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE CU ON C.CONSTRAINT_NAME = CU.CONSTRAINT_NAME INNER JOIN ( SELECT i1.TABLE_NAME, i2.COLUMN_NAME FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS i1 INNER JOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE i2 ON i1.CONSTRAINT_NAME = i2.CONSTRAINT_NAME WHERE i1.CONSTRAINT_TYPE = 'PRIMARY KEY' ) PT ON PT.TABLE_NAME = PK.TABLE_NAME and PK.TABLE_NAME='table_name' ORDER BY 1,2,3,4

SELECT xtype, name FROM systypes ORDER BY xType and the results XType Datatype ---------------34 image 35 text 36 uniqueidentifier 48 tinyint 52 smallint

56 int 58 smalldatetime 59 real 60 money 61 datetime 62 float 98 sql_variant 99 ntext 104 bit 106 decimal 108 numeric 122 smallmoney 127 bigint 165 varbinary 167 varchar 173 binary 175 char 189 timestamp 231 nvarchar 231 sysname 239 nchar 241 xml

You might also like