获取表名:

 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

获取oracle 表字段,表名,以及主键之类等等的信息。的更多相关文章

  1. hibernate 获取实体的表名、主键名、列名(转载+修改)

    package com.escs.utils; import java.util.Iterator; import org.hibernate.cfg.AnnotationConfiguration; ...

  2. 分库分表的 9种分布式主键ID 生成方案,挺全乎的

    <sharding-jdbc 分库分表的 4种分片策略> 中我们介绍了 sharding-jdbc 4种分片策略的使用场景,可以满足基础的分片功能开发,这篇我们来看看分库分表后,应该如何为 ...

  3. Sqlite清空表数据以及重新设置主键操作

    Sqlite清空表数据以及重新设置主键操作 delete from 表名; //清空数据 update sqlite_sequence SET seq = 0 where name ='表名';//自 ...

  4. 怎样用sql语句复制表table1到表table2的同时复制主键

    原文:怎样用sql语句复制表table1到表table2的同时复制主键 在从table1表复制到table2的时候,我们会用语句: select * into table2 from table1 但 ...

  5. sql语句建表,并且自增加主键

    sql语句建表,并且自增加主键 use [test] CREATE TABLE [dbo].[Table_4] ( [userid] [int] IDENTITY(1,1) NOT NULL, CON ...

  6. SQL点滴4—筛选数据列的类型,字段大小,是否可为空,是否是主键,约束等等信息

    原文:SQL点滴4-筛选数据列的类型,字段大小,是否可为空,是否是主键,约束等等信息 项目需要将Access数据库中的数据导入到SQL Server中,需要检验导入后的数据完整性,数据值是否正确.我们 ...

  7. mybatis由浅入深day01_4入门程序_4.6根据用户id(主键)查询用户信息

    4 入门程序 4.1 需求 根据用户id(主键)查询用户信息 根据用户名称模糊查询用户信息 添加用户 删除 用户 更新用户 4.2 环境 java环境:jdk1.7.0_72 eclipse:indi ...

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

    数据库版本号:select * from v$version 数据库名:select * from v$instance 注意: 我在C#项目中查询语句的时候报“ORA-00911: 无效字符” 的错 ...

  9. oracle&&Sqlserver获取表名列名主键及数据类型

    SQlserver获得列名,列类型,列类型长度,scale,prec等数据类型(syscolumns,systypes,sysobjects均为视图) select a.name as colname ...

随机推荐

  1. Samba实现Linux与Window文件的传输

    Samba是在Linux和UNIX系统上实现SMB协议的一个免费软件,由服务器及客户端程序构成.SMB(Server Messages Block,信息服务块)是一种在局域网上共享文件和打印机的一种通 ...

  2. 某表含有N个字段超精简模糊查询方法

    我们在做多个字段模糊查询时,是不是觉得非常麻烦?比如我要模糊查询某表多个字段存在某数据时,如下 select * from table where a like '%key%' or b  like ...

  3. Calender的使用详解

    Calendar和GregorianCalendar简介 Calendar的中文翻译是日历,实际上,在历史上有着许多种计时的方法.所以为了计时的统一,必需指定一个日历的选择.那现在最为普及和通用的日历 ...

  4. session的一个问题

    <%@ page language="java" import="java.util.*,javax.servlet.http.Cookie.*" pag ...

  5. nginx web加密访问

    有时我们会有这么一种需求,就是你的网站并不想提供一个公共的访问或者某些页面不希望公开, 我们希望的是某些特定的客户端可以访问.那么我们可以在访问时要求进行身份认证,就如给你自己的家门加一把锁,以拒绝那 ...

  6. Vijos p1892 树上的最大匹配 树形DP+计数 被卡常我有特殊技巧heheda

    https://vijos.org/p/1892 此题需要手动开栈: <<; //256MB char *p=(char*)malloc(size)+size; __asm__(" ...

  7. Linux内核之内存管理完全剖析

    linux虚拟内存管理功能 ? 大地址空间:? 进程保护:? 内存映射:? 公平的物理内存分配:? 共享虚拟内存.实现结构剖析   (1)内存映射模块(mmap):负责把磁盘文件的逻辑地址映射到虚拟地 ...

  8. Linux 内核通知链机制的原理及实现

    一.概念: 大多数内核子系统都是相互独立的,因此某个子系统可能对其它子系统产生的事件感兴趣.为了满足这个需求,也即是让某个子系统在发生某个事件时通知其它的子 系统,Linux内核提供了通知链的机制.通 ...

  9. jquery------使用jQuery的委托方法

    index.jsp <div id="gallery"> <div class="photo"> <img src="a ...

  10. U盘中的autorun.inf

    怎么删除u盘里的autorun.inf 如果U盘中毒,刚插进机子时按住SHIFT五秒,这样就可以跳过预读,这样防止了预读时把病毒感染到机子上,在U盘盘符上点右键,看看有没有“Auto”选项: 1.如果 ...