sqlserver中提供了相当丰富的系统视图,能够从宏观到微观,从静态到动态反应数据库对象的存储结果、系统性能、系统等待事件等等。同时 也保留了与早期版本兼容性的视图,主要差别在于sqlserver2008提供的新系统视图一是更加全面和丰富、二是更注重命名规则。
sqlserver2008的几乎所有对象信息都存在于sys.objects系统视图中,同时又在不同的系统视图中保留了相应的副本,对于函数、视图、 存储过程、触发器等相应的文本对象,把相应的对象的详细资料存于新的sys.sql_modules视图中。
序号 对象类型 对象类型描述 相关系统表
1 af = 聚合函数 (clr) aggregate_function n/a
2 c = check 约束 check_constraint check_constraints
3 d = default(约束或独立) default_constraint default_constraints
4 f = foreign key 约束 foreign_key_constraint foreign_keys
5 fn = sql 标量函数 sql_scalar_function sql_modules
6 fs = 程序集 (clr) 标量函数 clr_scalar_function n/a
7 ft = 程序集 (clr) 表值函数 clr_table_valued_function n/a
8 if = sql 内联表值函数 sql_inline_table_valued_function sql_modules
9 it = 内部表 internal_table internal_tables
10 p = sql 存储过程 sql_stored_procedure procedures
sql_modules
11 pc = 程序集 (clr) 存储过程 clr_stored_procedure n/a
12 pg = 计划指南 plan_guide plan_guides
13 pk = primary key 约束 primary_key_constraint key_constraints
14 r = 规则(旧式,独立) rule sql_modules
15 rf = 复制筛选过程 replication_filter_procedure sql_modules
16 s = 系统基表 system_table objects
17 sn = 同义词 synonym synonyms
18 sq = 服务队列 service_queue service_queuess
19 ta = 程序集 (clr) dml 触发器 clr_trigger n/a
20 tf = sql 表值函数 sql_table_valued_function sql_modules
21 tr = sql dml 触发器 sql_trigger triggers
sql_modules
22 u = 表(用户定义类型) user_table tables
23 uq = unique 约束 unique_constraint key_constraints
24 v = 视图 view views
sql_modules
25 x = 扩展存储过程 extended_stored_procedure extended_procedures
对于数据库层面的存储结构,我们可以参看以下视图:
--数据库实例的概要情况
select*from sys.servers
where server_id=0
--兼容性视图select*from sys.sysservers
--各个数据库的详细信息
select*from sys.databases
--兼容性视图select*from sys.sysdatabases
--文件组的详细信息
select*from sys.filegroups
--兼容性视图select*from sys.sysfilegroups
--各个数据库文件的详细信息
select*from sys.master_files
--兼容性视图select*from sys.sysaltfiles
--当前数据库文件的详细信息
select*from sys.database_files
--兼容性视图select*from sys.sysfiles
--数据空间的详细情况,可以是文件组或分区方案
select*from sys.data_spaces
关于数据库表的存储信息,通过以下系统表我们可以大致了解数据库表在数据库中是如何定义的。以下视图提供了基本的数据库对象信息。
#div_code img { border: 0px none; }
--我们首先创建一张表和一些索引
create table dbo.test
(
idintidentity(1,1)notnull,
name char(100)null,
constraint pk_test primary key clustered (idasc)
)
create nonclustered index ix_testondbo.test(name)
--表和对象详细信息,根据表名称查询出object_id为
--事实上几乎所有的用户对象都出自于sys.objects表
select*from sys.objects
where type_desc='user_table' and name='test'
--兼容性视图sysobjects
--如果要查询与该表相关的其他所有对象,则可以执行以下语句
select*from sys.objects
where type_desc='user_table' and name='test' or
parent_object_id in
(selectobject_id from sys.objects
where type_desc='user_table' and name='test')
--表字段详细信息,可以查询出相关column_id
select*from sys.columns
where object_id=5575058
--兼容性视图syscolumns
--表索引详细情况,可以清楚的看到存在两个索引
select*from sys.indexes where object_id=5575058
--兼容性视图sysindexes
--表分区情况,数据库中所有表和索引的每个分区在表中各对应一行
--此处可以看到该表有两个分区,聚集索引即表本身,还有一个是name的非聚集索引
--partition_id 即分区的id
--hobt_id包含此分区的行的数据堆或b树的id
select*from sys.partitions where object_id=5575058
--分配单元情况,数据库中的每个分配单元都在表中占一行
--该表只有和sys.partitions配合使用才有意义
select*from sys.allocation_units
--sys.allocation_units和sys.partitions一起使用能够反映出某个对象的页面分配和使用情况
select*from sys.allocation_units u,sys.partitions p
where u.type in (1,3)andu.container_id=p.hobt_idandp.object_id=5575058
union all
select*from sys.allocation_units u,sys.partitions p
where u.type=2andu.container_id=p.partition_idandp.object_id=5575058
--返回每个分区的页和行计数信息
select*from sys.dm_db_partition_stats where object_id=5575058
--返回索引的详细字段情况
select*from sys.index_columns where object_id=5575058
--兼容性视图sysindexkeys
--以下为根据某个索引名称获取其相关字段的语句
declare @index_field_names varchar(500)
set@index_field_names='';
select@index_field_names=@index_field_names+c.name+','
from sys.index_columns a,sys.indexes b,sys.columns c
where a.object_id=b.object_idanda.index_id=b.index_id
anda.object_id=c.object_idanda.column_id=c.column_id
andb.name='ix_test2'
order by a.index_column_id
set@index_field_names=left(@index_field_names,len(@index_field_names)-1)
print @index_field_names
--check约束,数据来源sys.objects.type='c'
select*from sys.check_constraints where object_id=?
--兼容性视图sysconstraints
--数据来源sys.objects.type=d
select*from sys.default_constraints where object_id=?
--兼容性视图sysconstraints
--主键或唯一约束,数据来源sys.objects.type pk 和uq
select*from sys.key_constraints where object_id=?
--兼容性视图sysconstraints
--外键,数据来源sys.object.type=f
select*from sys.foreign_keys where object_id=?
--兼容性视图sysreferences
--触发器
select*from sys.triggers where object_id=?
--注释
select*from sys.sql_modules
--兼容性视图syscomments
--数据库用户表
select*from sys.database_principals
--兼容性视图sysusers
--数据库数据类型表
select*from sys.types
--兼容性视图systypes
