程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 數據庫知識 >> SqlServer數據庫 >> 關於SqlServer >> SqlServer2005數據庫所有表和字段及其屬性

SqlServer2005數據庫所有表和字段及其屬性

編輯:關於SqlServer

select '' as rownum, TableName_SqlServer, ColumnName, FIEldType_SqlServer,
FIEldType_SqlServer +
case
when FieldLength is null or charindex('datetime',FieldType_SqlServer) != 0 or charindex('int', FIEldType_SqlServer) != 0
then ''
else '(' + FIEldLength + ')'
end as FIEldTypeLen_SqlServer
, DefaultValue_SqlServer, FIEldType_Oracle, DefaultValue_Oracle,
case when IsNullable = '1'
then 'Y'
else ''
end as IsNullable,
case when charindex('datetime', FieldType_SqlServer) != 0 or charindex('int', FIEldType_SqlServer) != 0
then ''
else FIEldLength
end as FIEldLength, isnull(IsKey, '') as IsKey, PkName, isnull(IsIndex,'') as isIndex, IndexName
from
(
select ta.name as TableName_SqlServer, c.name as ColumnName, c.
IsNullable, t.name as FIEldType_SqlServer, cast(c.prec as varchar) +
case when c.scale is null
then ''
else ','
end + isnull(cast(c.scale as varchar), '') as FIEldLength, isnull(co.text,'') as DefaultValue_SqlServer,
'' as FIEldType_Oracle, '' as DefaultValue_Oracle,
(
select top 1
case when i1.status = 2066 then 'Y' else '' end from sysindexes i1, sysindexkeys k1
where i1.id = k1.id
and i1.indid = k1.indid
and i1.id = c.id
and k1.colid = c.colid
AND i1.status = 2066
) as IsKey,
(
select top 1
case when i2.status = 2066
then i2.name
else ''
end
from sysindexes i2, sysindexkeys k2
where i2.id = k2.id
and i2.indid = k2.indid
and i2.id = c.id
and k2.colid = c.colid
AND i2.status = 2066
) as PkName,
(
select top 1
case when i3.status != 2066
then 'Y'
else ''
end
from sysindexes i3, sysindexkeys k3
where i3.id = k3.id
and i3.indid = k3.indid
and i3.id = c.id
and k3.colid = c.colid
AND i3.status != 2066
) as IsIndex,
(
select top 1
case when i4.status != 2066
then i4.name
else ''
end
from sysindexes i4, sysindexkeys k4
where i4.id = k4.id
and i4.indid = k4.indid
and i4.id = c.id
and k4.colid = c.colid
AND i4.status != 2066
) as IndexName
from sysobjects ta
inner join syscolumns c on ta.id = c.id
left join SYSTYPES t on c.xusertype = t.xusertype
left join syscomments co on c.cdefault = co.id
where ta.xtype = 'U'
) b
order by TableName_SqlServer, ColumnName

執行結果如下:

簡單的Sql:

1、查找表的所有索引(包括索引名,類型,構成列):

select t.*,i.index_type from user_ind_columns t,user_indexes i where t.index_name = i.index_name and t.table_name = i.table_name and t.table_name = 要查詢的表

2、查找表的主鍵(包括名稱,構成列):

select cu.* from user_cons_columns cu, user_constraints au where cu.constraint_name = au.constraint_name and au.constraint_type = 'P' and au.table_name = 要查詢的表

來源:(http://blog.sina.com.cn/s/blog_5421dfd20100fhvd.Html) - SqlServer2005數據庫所有表和字段及其屬性_mousekitty_新浪博客

3、查找表的唯一性約束(包括名稱,構成列):

select column_name from user_cons_columns cu, user_constraints au where cu.constraint_name = au.constraint_name and au.constraint_type = 'U' and au.table_name = 要查詢的表

4、查找表的外鍵(包括名稱,引用表的表名和對應的鍵名,下面是分成多步查詢):

select * from user_constraints c where c.constraint_type = 'R' and c.table_name = 要查詢的表

查詢外鍵約束的列名:

select * from user_cons_columns cl where cl.constraint_name = 外鍵名稱

查詢引用表的鍵的列名:

select * from user_cons_columns cl where cl.constraint_name = 外鍵引用表的鍵名

5、查詢表的所有列及其屬性

select t.*,c.COMMENTS from user_tab_columns t,user_col_comments c where t.table_name = c.table_name and t.column_name = c.column_name and t.table_name = 要查詢的表

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved