獲取oracle 表字段,表名,以及主鍵之類等等的信息

數據庫版本號: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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章