数据库版本号:select * from v$version

数据库名:select * from v$instance

注意:
我在C#项目中查询语句的时候报“ORA-00911: 无效字符” 的错误,原因竟然是在查询语句后面多了个分号“;”的原因,分号在plsql中属于正常,在项目中则属于无效字符,希望可以给大家提醒。

参考出处:http://www.cnblogs.com/zhangronghua/archive/2007/08/29/874484.html

使用关键字作为字段名

1:使用关键字作为字段别名
2:使用关键字作为数据库的字段名

在我们开发或使用的过程中经常要使用特定的字段名,这时候我们可以给数据库的字段取别名,这个别名有可能是数据库的关键字,这时我们可以给别名加上双引号:

select 'abcd' "group" from dual
select systimestamp "select" from dual

在使用数据库的时候DBA就需要定义字段的名字就是数据库的关键字,这时也必须使用双引号,并且关键字必须大写,经常用到的语句有:

CREATE 、INSERT 、UPDATE 、SELECT 这些语句必须使用双引号,并且关键字必须大写。

总结:

1:如果在别名中使用关键字,必须使用双引号括起来"关键字",关键字可以不大写。
2:如果是数据库字段名就是关键字,在操作时语句必须使用双引号,并且关键字必须大写。
3:如果不是特殊情况最好不要使用,以免引起代码编写过程中的错误。

参考出处:http://www.2cto.com/database/201212/172909.html

获取表名:

 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,data_default
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 col.column_name , uc.constraint_type,case uc.constraint_type when 'P' then '√' else '' end "PrimaryKey"
from user_tab_columns col left join user_cons_columns ucc on ucc.table_name=col.table_name and ucc.column_name=col.column_name
left join user_constraints uc on uc.constraint_name = ucc.constraint_name and uc.constraint_type='P'
where 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

出处:http://blog.csdn.net/jack_zy1981/article/details/5699787

获取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 表字段,表名,以及主键之类等等的信息。

    获取表名:  Oracle的user_talbes用于记录了用户表信息. select * from user_tables  获取某个表的字段: USER_TAB_COLS中记录了用户表的列信息.下 ...

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

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

随机推荐

  1. 更新Code First生成的数据库

    1,首次访问时会自动生成数据库 2,某个Model增加一个字段后,再次访问会报,数据库不是最新 操作 1,Enable-Migrations 注意选择Default project为Star.Core ...

  2. Karel运行环境配置

    1.下载 见http://wenku.baidu.com/view/24762ced998fcc22bcd10d5e.html 2.界面空白问题 问题:运行Karel后,发现整个界面空白一片,没有任何 ...

  3. ArcEngine 通过SpatialRelDescription删除不相交要素

    ISpatialFilter.SpatialRel设置为esriSpatialRelRelate,并且设置SpatialRelDescription为某个字符串.该字符串的构造方法:该字符串为长度为9 ...

  4. centos6.5 安装fctix 五笔输入法

    摸索了大半晚上,终于搞定,网上的东西看了N多篇不是这问题就是那问题,看来不同的OS下,小白我还是太嫩了些. 1,删除输入法,这一步是清除输入法,操作完成后,桌面/系统/首先项/输入法的IM Choos ...

  5. swfupload 参数说明

    一.配置参数对象中的常用属性及说明 属性 类型 默认值 描述 upload_url String   处理上传文件的服务器端页面的url地址,可以是绝对地址,也可以是相对地址,当为相对地址时相对的是当 ...

  6. 堪称最好的A*算法

    原文地址:http://theory.stanford.edu/~amitp/GameProgramming/ 相关链接:http://www-cs-students.stanford.edu/%7E ...

  7. ylb:SQL Server中的时间函数

    ylbtech-SQL Server:SQL Server-SQL Server中的时间函数 SQL Server中的时间函数. 1,SQL Server中的时间函数 返回顶部 1.   当前系统日期 ...

  8. 性能测试之LoardRunner 测试场景监控关注的几点

    1.系统业务处理能力,即通常我们在进行性能测试的时候,在特定的硬件和软件环境下考察的业务处理能力,即“事物”,需要关注当前.平时.峰值以及长远未来业务发展情况,考虑不同业务的处理数量,从而设定相应的业 ...

  9. JavaScript专业规则12条

    学习JavaScript是困难的.它发展的如此之快,以至于在任何一个特定的时刻,你都不清楚自己是否“做错了”.有些时候,感觉像是坏的部分超过了好的部分.然而,讨论这些并没有意义,JavaScript正 ...

  10. iframe根据子页面自动调整大小

    //iframe高度自适应 function IFrameReSize(iframename) { var pTar = document.getElementById(iframename); if ...