首页 > 数据库 > SQL Server > 正文

如何获取MSSQLServer,Oracel,Access中的数据字典信息

2024-08-31 00:48:32
字体:
来源:转载
供稿:网友

----------------------------------------------
--------------- ms sqlserver -----------------
----------------------------------------------

--表说明
select dbo.sysobjects.name as tablename,
      dbo.sysproperties.[value] as tabledesc
from dbo.sysproperties inner join
      dbo.sysobjects on dbo.sysproperties.id = dbo.sysobjects.id
where (dbo.sysproperties.smallid = 0)
order by dbo.sysobjects.name

--字段说明
select dbo.sysobjects.name as tablename, dbo.syscolumns.colid,
      dbo.syscolumns.name as colname, dbo.sysproperties.[value] as coldesc
from dbo.sysproperties inner join
      dbo.sysobjects on dbo.sysproperties.id = dbo.sysobjects.id inner join
      dbo.syscolumns on dbo.sysobjects.id = dbo.syscolumns.id and
      dbo.sysproperties.smallid = dbo.syscolumns.colid
order by dbo.sysobjects.name, dbo.syscolumns.colid

 

--主键、外键信息(简化)
select
 c_obj.name    as constraint_name
 ,t_obj.name    as table_name
 ,col.name    as column_name
 ,case col.colid 
  when ref.fkey1 then 1   
  when ref.fkey2 then 2   
  when ref.fkey3 then 3   
  when ref.fkey4 then 4   
  when ref.fkey5 then 5   
  when ref.fkey6 then 6   
  when ref.fkey7 then 7   
  when ref.fkey8 then 8   
  when ref.fkey9 then 9   
  when ref.fkey10 then 10   
  when ref.fkey11 then 11   
  when ref.fkey12 then 12   
  when ref.fkey13 then 13   
  when ref.fkey14 then 14   
  when ref.fkey15 then 15   
  when ref.fkey16 then 16
 end      as ordinal_position
from
 sysobjects c_obj
 ,sysobjects t_obj
 ,syscolumns col
 ,sysreferences  ref
where
 permissions(t_obj.id) != 0
 and c_obj.xtype in ('f ')
 and t_obj.id = c_obj.parent_obj
 and t_obj.id = col.id
 and col.colid   in
 (ref.fkey1,ref.fkey2,ref.fkey3,ref.fkey4,ref.fkey5,ref.fkey6,
 ref.fkey7,ref.fkey8,ref.fkey9,ref.fkey10,ref.fkey11,ref.fkey12,
 ref.fkey13,ref.fkey14,ref.fkey15,ref.fkey16)
 and c_obj.id = ref.constid
union
 select
 i.name     as constraint_name
 ,t_obj.name    as table_name
 ,col.name    as column_name
 ,v.number    as ordinal_position
from
 sysobjects  c_obj
 ,sysobjects  t_obj
 ,syscolumns  col
 ,master.dbo.spt_values  v
 ,sysindexes  i
where
 permissions(t_obj.id) != 0
 and c_obj.xtype in ('uq' ,'pk')
 and t_obj.id = c_obj.parent_obj
 and t_obj.xtype  = 'u'
 and t_obj.id = col.id
 and col.name = index_col(t_obj.name,i.indid,v.number)
 and t_obj.id = i.id
 and c_obj.name  = i.name
 and v.number  > 0
  and v.number  <= i.keycnt
  and v.type  = 'p'

order by constraint_name, ordinal_position


--主键、外键对照(简化)
select
 fc_obj.name   as constraint_name
 ,i.name     as unique_constraint_name
from 
 sysobjects fc_obj
 ,sysreferences r
 ,sysindexes i
 ,sysobjects pc_obj
where
 permissions(fc_obj.parent_obj) != 0
 and fc_obj.xtype = 'f'
 and r.constid  = fc_obj.id
 and r.rkeyid  = i.id
 and r.rkeyindid  = i.indid
 and r.rkeyid  = pc_obj.id


----------------------------------------------
------------------- oracle -------------------
----------------------------------------------

--表信息
select * from all_tab_comments t
where owner='dbo'

--列信息
select * from all_col_comments t
where owner='dbo'

--主键、外键对照
select owner, constraint_name, constraint_type, table_name, r_owner, r_constraint_name
from all_constraints
where owner='dbo' and (constraint_type='p' or constraint_type='r')


--主键、外键信息
select *
from all_cons_columns
where owner='dbo'
order by constraint_name, position


----------------------------------------------
------------------- access -------------------
----------------------------------------------
//access中的系统表msysobjects存储属性的字段是二进制格式,不能直接分析
//可以采用ado自带的openschema方法获得相关信息

  //use adoint.pas
  //po: tablename
  //dbcon:tadoconnection
  /ds:tadodataset
 
--表信息
  dbcon.openschema(sitables, vararrayof([null, null, 'table']), emptyparam, ds);

--列信息
  dbcon.openschema(sicolumns, vararrayof([null, null, 'po']), emptyparam, ds);
 
--主键
  dbcon.openschema(siprimarykeys, emptyparam, emptyparam, ds);
 
 
--主键、外键对照
  dbcon.openschema(siforeignkeys, emptyparam, emptyparam, ds);


 
  • 网站运营seo文章大全
  • 提供全面的站长运营经验及seo技术!
  • 发表评论 共有条评论
    用户名: 密码:
    验证码: 匿名发表