获取oracle 表字段,表名,以及主键之类信息

获取表名:

 Oracle的user_talbes用于记录了用户表信息。

select * from user_tables

 获取某个表的字段:

USER_TAB_COLS中记录了用户表的列信息。下面是别人写的:

SELECT USER_TAB_COLS.TABLE_NAME as 表名,USER_TAB_COLS.COLUMN_NAME as 列名 , USER_TAB_COLS.DATA_TYPE as 数据类型, USER_TAB_COLS.DATA_LENGTH as 长度, USER_TAB_COLS.NULLABLE as 是否为空,USER_TAB_COLS.COLUMN_ID as 列序号,user_col_comments.comments as 备注 FROM USER_TAB_COLS inner join user_col_comments on user_col_comments.TABLE_NAME=USER_TAB_COLS.TABLE_NAME and user_col_comments.COLUMN_NAME=USER_TAB_COLS.COLUMN_NAME

 

如何从Oracle、中取得表的注释

user_tab_comments;表注释

        user_col_comments;表字段注释

        以上两个只能获取自己用户的表的注释信息,如果要访问自己能够访问的其他用户的表,则需要使用:

        all_tab_comments;表注释

        all_col_comments;表字段注释

        当然,如果有DBA权限,则可以使用

        dba_tab_comments;表注释

        dba_col_comments;表字段注释

        dba*和all*最好指定owner条件。user*没有该字段

        user_tab_comments;表注释

        user_col_comments;表字段注释

        以上两个只能获取自己用户的表的注释信息,如果要访问自己能够访问的其他用户的表,则需要使用:

        all_tab_comments;表注释

        all_col_comments;表字段注释

        当然,如果有DBA权限,则可以使用

        dba_tab_comments;表注释

        dba_col_comments;表字段注释

        dba*和all*最好指定owner条件。user*没有该字段

 

 

关于Oracle与SqlServer中获取所有字段、主键、外键的sql语句 标签: 主键  外键  sql 
最近在做的社会网络分析原型系统需要将多种不同数据库中的表的字段、主外键信息读出,实现这些功能费了不少功夫,记录下来以备用吧
Oracle:
查询某个表中的字段名称、类型、精度、长度、是否为空
select COLUMN_NAME,DATA_TYPE,DATA_PRECISION,DATA_SCALE,NULLABLE
from user_tab_columns
where table_name ='YourTableName'
查询某个表中的主键字段名
select col.column_name
from user_constraints con,  user_cons_columns col
where con.constraint_name = col.constraint_name
and con.constraint_type='P'
and col.table_name = 'YourTableName'
查询某个表中的外键字段名称、所引用表名、所应用字段名
select distinct(col.column_name),r.table_name,r.column_name
from
user_constraints con,
user_cons_columns col,
(select t2.table_name,t2.column_name,t1.r_constraint_name
 from user_constraints t1,user_cons_columns t2
 where t1.r_constraint_name=t2.constraint_name
 and t1.table_name='YourTableName'
 ) r
where con.constraint_name=col.constraint_name
and con.r_constraint_name=r.r_constraint_name
and con.table_name='YourTableName'

SQLServer中的实现:
字段:
SELECT c.name,t.name,c.xprec,c.xscale,c.isnullable
FROM systypes t,syscolumns c
WHERE t.xtype=c.xtype
AND c.id = (SELECT id FROM sysobjects WHERE name='YourTableName')
ORDER BY c.colid

主键(参考SqlServer系统存储过程sp_pkeys):
select COLUMN_NAME = convert(sysname,c.name)              
from                                                      
sysindexes i, syscolumns c, sysobjects o                  
where o.id = object_id('[YourTableName]')                 
and o.id = c.id                                           
and o.id = i.id                                           
and (i.status & 0x800) = 0x800                            
and (c.name = index_col ('[YourTableName]', i.indid,  1) or    
     c.name = index_col ('[YourTableName]', i.indid,  2) or    
     c.name = index_col ('[YourTableName]', i.indid,  3) or    
     c.name = index_col ('[YourTableName]', i.indid,  4) or    
     c.name = index_col ('[YourTableName]', i.indid,  5) or    
     c.name = index_col ('[YourTableName]', i.indid,  6) or    
     c.name = index_col ('[YourTableName]', i.indid,  7) or    
     c.name = index_col ('[YourTableName]', i.indid,  8) or    
     c.name = index_col ('[YourTableName]', i.indid,  9) or    
     c.name = index_col ('[YourTableName]', i.indid, 10) or    
     c.name = index_col ('[YourTableName]', i.indid, 11) or    
     c.name = index_col ('[YourTableName]', i.indid, 12) or    
     c.name = index_col ('[YourTableName]', i.indid, 13) or    
     c.name = index_col ('[YourTableName]', i.indid, 14) or    
     c.name = index_col ('[YourTableName]', i.indid, 15) or    
     c.name = index_col ('[YourTableName]', i.indid, 16)      
     )

外键:
select t1.name,t2.rtableName,t2.name
from
(select col.name, f.constid as temp
 from syscolumns col,sysforeignkeys f
 where f.fkeyid=col.id
 and f.fkey=col.colid
 and f.constid in
 ( select distinct(id) 
   from sysobjects
   where OBJECT_NAME(parent_obj)='YourTableName'
   and xtype='F'
  )
 ) as t1 ,
(select OBJECT_NAME(f.rkeyid) as rtableName,col.name, f.constid as temp
 from syscolumns col,sysforeignkeys f
 where f.rkeyid=col.id
 and f.rkey=col.colid
 and f.constid in
 ( select distinct(id)
   from sysobjects
   where OBJECT_NAME(parent_obj)='YourTableName'
   and xtype='F'
 )
) as t2
where t1.temp=t2.temp

 

 

就是这些,都是平时积累下来的,摘自己需要的用吧。



public static void test() {
		Connection conn = null;
		Statement stat = null;
		ResultSet rs = null;
		StringBuffer stringBuffer = new StringBuffer();
		stringBuffer.append("<html><head></head><body lang=ZH-CN style='tab-interval:21.0pt;text-justify-trim:punctuation'>");
		List<Object[]> objArrList = new ArrayList<Object[]>();
		try {
			conn = Pub.getConn();
			stat = conn.createStatement();
			String sql = "select * from user_tab_comments";
			System.out.println("sql=="+sql);
			rs = stat.executeQuery(sql);
			int i = 0;
			while (rs.next()) {
				i ++;
				String table_name = rs.getString("TABLE_NAME");
				String comments = rs.getString("COMMENTS");
				Object[] objArr = new Object[3]; 
				objArr[0] = i;
				objArr[1] = table_name;
				objArr[2] = comments;
				objArrList.add(objArr);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			Pub.close(conn, stat, rs);
		}
		
		for (int i = 0; i < objArrList.size(); i++) {
			Object[] objArr = objArrList.get(i);
			String table_name = objArr[1].toString();
			String comments = objArr[2].toString();
			stringBuffer.append("<font size=\"5\">").append("<strong>").append(objArr[0].toString()).append("、").append(comments).append("  ").append(table_name).append("</strong>").append("</font>").append("\n");
			stringBuffer.append("<table border=1 cellspacing=0 cellpadding=0 style='border-collapse:collapse;border:none;mso-border-alt:solid black .5pt; mso-border-themecolor:text1;mso-yfti-tbllook:1184;mso-padding-alt:0cm 5.4pt 0cm 5.4pt'>");
			stringBuffer.append("<tr style='mso-yfti-irow:0;mso-yfti-firstrow:yes'>");
			stringBuffer.append("<td width=95 valign=top style='background-color:#ccc;border:solid black 1.0pt;'>").append("英文名").append("</td>");
			stringBuffer.append("<td width=95 valign=top style='background-color:#ccc;border:solid black 1.0pt;'>").append("中文名").append("</td>");
			stringBuffer.append("<td width=95 valign=top style='background-color:#ccc;border:solid black 1.0pt;'>").append("类型").append("</td>");
			stringBuffer.append("<td width=95 valign=top style='background-color:#ccc;border:solid black 1.0pt;'>").append("长度").append("</td>");
			stringBuffer.append("<td width=95 valign=top style='background-color:#ccc;border:solid black 1.0pt;'>").append("是否为空").append("</td>");
			stringBuffer.append("<td width=95 valign=top style='background-color:#ccc;border:solid black 1.0pt;'>").append("主键/外键").append("</td>");
			stringBuffer.append("</tr>");
			try {
				conn = Pub.getConn();
				stat = conn.createStatement();
				String sql = "SELECT USER_TAB_COLS.COLUMN_NAME , USER_TAB_COLS.DATA_TYPE, USER_TAB_COLS.DATA_LENGTH,user_tab_cols.DATA_PRECISION,user_tab_cols.DATA_SCALE, USER_TAB_COLS.NULLABLE,user_col_comments.comments FROM USER_TAB_COLS inner join user_col_comments on user_col_comments.TABLE_NAME=USER_TAB_COLS.TABLE_NAME and user_col_comments.COLUMN_NAME=USER_TAB_COLS.COLUMN_NAME and user_col_comments.table_name='"+table_name+"' order by user_tab_cols.COLUMN_ID";
				System.out.println("sql=="+sql);
				rs = stat.executeQuery(sql);
				boolean b = true;
				while (rs.next()) {
					String column_name = rs.getString("column_name");
					String data_type = rs.getString("DATA_TYPE");
					int data_length = rs.getInt("DATA_LENGTH");
					int data_percision = rs.getInt("DATA_PRECISION");
					int data_scale = rs.getInt("DATA_SCALE");
					String nullable = rs.getString("NULLABLE");
					String ccomments = rs.getString("COMMENTS");
					String temp_data_length;
					if ("NUMBER".equals(data_type)) {
						if (data_scale == 0) {
							temp_data_length = data_percision+"";
						}else {
							temp_data_length = data_percision+","+data_scale;
						}
					}else {
						temp_data_length = data_length+ "";
					}
					String pk ="";
					if (b) {
						pk = "PK";
						b = false;
					}
					stringBuffer.append("<tr style='mso-yfti-irow:0;mso-yfti-firstrow:yes'>");
					stringBuffer.append("<td width=95 valign=top style='border:solid black 1.0pt;'>").append(column_name).append("</td>");
					stringBuffer.append("<td width=95 valign=top style='border:solid black 1.0pt;'>").append(ccomments).append("</td>");
					stringBuffer.append("<td width=95 valign=top style='border:solid black 1.0pt;'>").append(data_type).append("</td>");
					stringBuffer.append("<td width=95 valign=top style='border:solid black 1.0pt;'>").append(temp_data_length).append("</td>");
					stringBuffer.append("<td width=95 valign=top style='border:solid black 1.0pt;'>").append(nullable).append("</td>");
					stringBuffer.append("<td width=95 valign=top style='border:solid black 1.0pt;'>").append(pk).append("</td>");
					stringBuffer.append("</tr>");
				}
			} catch (SQLException e) {
				e.printStackTrace();
			} finally {
				Pub.close(conn, stat, rs);
			}
			stringBuffer.append("</table>");
			stringBuffer.append("<br>");
		}
		stringBuffer.append("</body></html>");
		FileOperate fo = new FileOperate();
		fo.createFile("D:\\My Documents\\桌面", "写中文文字.htm");
		fo.writeFile("D:\\My Documents\\桌面\\写中文文字.htm", stringBuffer.toString());
		System.out.println(stringBuffer.toString());
	} 


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值