SQLServer 表結構相關查詢(快速瞭解資料庫)
阿新 • • 發佈:2019-01-11
學習網址:
http://blog.csdn.net/kk185800961/article/details/8570512
- -- 表結構查詢
- SELECT
- 表名 = casewhen a.colorder=1 then d.nameelse''end,
- 表說明 = casewhen a.colorder=1 thenisnull(f.value,'') else''end,
- 欄位序號 = a.colorder,
- 欄位名 = a.name,
- 標識 = casewhen COLUMNPROPERTY( a.id,a.
- 主鍵 = casewhen exists(SELECT 1 FROM sysobjects where xtype='PK'and parent_obj=a.id andnamein (
- SELECTnameFROM sysindexes WHERE indid in( SELECT indid FROM sysindexkeys WHERE id = a.id AND colid=a.colid))) then'√'else''end,
- 型別 = b.name,
- 佔用位元組數 = a.length,
- 長度 = COLUMNPROPERTY(a.id,a.name,'PRECISION'),
- 小數位數 = isnull(COLUMNPROPERTY(a.id,a.name,'Scale'),0),
- 允許空 = casewhen a.isnullable=1 then'√'else''end,
- 預設值 = isnull(e.text,''),
- 欄位說明 = isnull(g.[value],''
- FROM syscolumns a leftjoin systypes b
- on a.xusertype=b.xusertype
- innerjoin sysobjects d
- on a.id=d.id and d.xtype='U'and d.name<>'dtproperties'
- leftjoin syscomments e
- on a.cdefault=e.id
- leftjoin sys.extended_properties g
- on a.id=g.major_id and a.colid=g.minor_id
- leftjoin sys.extended_properties f
- on d.id=f.major_id and f.minor_id=0
- where d.name='tableName'--如果只查詢指定表,加上此條件
- orderby a.id,a.colorder
- --各欄位元資料(資料字典,MSSQL 2012及以上版本)
- select * from sys.dm_exec_describe_first_result_set (N'SELECT * FROM dbo.TabName', NULL, 0)
- GO
- --檢視資料庫中所有外來鍵
- select oMain.nameAS [主表名稱]
- ,oSub.nameAS [子表名稱]
- ,fk.nameAS [外來鍵名稱]
- ,MainCol.nameAS [主表列名]
- ,SubCol.nameAS [子表列名]
- from sys.foreign_keys fk
- JOIN sys.all_objects oSub ON (fk.parent_object_id = oSub.object_id)
- JOIN sys.all_objects oMain ON (fk.referenced_object_id = oMain.object_id)
- JOIN sys.foreign_key_columns fkCols ON (fk.object_id = fkCols.constraint_object_id)
- JOIN sys.columns SubCol ON (oSub.object_id = SubCol.object_id
- AND fkCols.parent_column_id = SubCol.column_id)
- JOIN sys.columns MainCol ON (oMain.object_id = MainCol.object_id
- AND fkCols.referenced_column_id = MainCol.column_id)
- --(匯出擴充套件屬性指令碼)
- SELECT 表名 = d.name,欄位名 = a.name, 欄位說明 = isnull(g.[value],'')
- ,'EXEC sys.sp_addextendedproperty @name=N''MS_Description'', @value=N'''+CONVERT(VARCHAR(MAX),g.[value])
- +''',@level0type=N''SCHEMA'',@level0name=N''dbo'',@level1type=N''TABLE'',@level1name=N'''+CONVERT(VARCHAR(MAX),d.name)
- +''',@level2type=N''COLUMN'',@level2name=N'''+CONVERT(VARCHAR(MAX),a.name)+''''
- FROM syscolumns a leftjoin systypes b on a.xusertype=b.xusertype
- innerjoin sysobjects d on a.id=d.id and d.xtype='U'and d.name<>'dtproperties'
- leftjoin syscomments e on a.cdefault=e.id
- leftjoin sys.extended_properties g on a.id=g.major_id and a.colid=g.minor_id
- WHERE g.[value] ISNOTNULL
- ORDERBY d.name,a.name
- --當前資料庫表大小及行數
- SELECT SCHEMA_NAME(tbl.schema_id) [Schema],tbl.nameAS [TableName],
- (CAST(ISNULL((select 8 * SUM(CASEWHEN a.type <> 1 THEN a.used_pages WHEN p.index_id < 2 THEN a.data_pages ELSE 0 END)
- FROM sys.indexes as i
- JOIN sys.partitions as p ON p.object_id = i.object_id and p.index_id = i.index_id
- JOIN sys.allocation_units as a ON a.container_id = p.partition_id