查看表索引、主鍵、外鍵、約束

創(chuàng)新互聯(lián)是一家專注于成都網(wǎng)站建設(shè)、網(wǎng)站建設(shè)與策劃設(shè)計,蒲縣網(wǎng)站建設(shè)哪家好?創(chuàng)新互聯(lián)做網(wǎng)站,專注于網(wǎng)站建設(shè)十多年,網(wǎng)設(shè)計領(lǐng)域的專業(yè)建站公司;建站業(yè)務(wù)涵蓋:蒲縣等地區(qū)。蒲縣做網(wǎng)站價格咨詢:18982081108
(包括索引名,類型,構(gòu)成列)
SELECT T.*, I.INDEX_TYPE
FROM USER_IND_COLUMNS T,USER_INDEXES I
WHERE T.INDEX_NAME = I.INDEX_NAME
AND T.TABLE_NAME = I.TABLE_NAME
AND T.TABLE_NAME = 'ORG_DLF' ----指定表
AND T.TABLE_OWNER= 'ODSRPT_SIT2'; ----指定用戶
(包括名稱,構(gòu)成列)
SELECT CU.*
FROM DBA_CONS_COLUMNS CU, DBA_CONSTRAINTS AU
WHERE CU.CONSTRAINT_NAME = AU.CONSTRAINT_NAME
AND AU.CONSTRAINT_TYPE = 'P'
AND AU.TABLE_NAME = 'LOAN_APPLICATION_FEE' -----指定表名
AND CU.OWNER='ODSRPT_SIT2'; -----指定用戶名
(包括表名稱,構(gòu)成列)
SELECT CU.COLUMN_NAME,AU.TABLE_NAME
FROM DBA_CONS_COLUMNS CU, DBA_CONSTRAINTS AU
WHERE CU.CONSTRAINT_NAME = AU.CONSTRAINT_NAME
AND AU.CONSTRAINT_TYPE = 'U'
AND AU.OWNER='RPT_UAT2' -----指定用戶名
AND AU.TABLE_NAME = 表名 ; -----指定表名
Select a.Owner 外鍵擁有者,
a.Table_Name 外鍵表,
c.Column_Name 外鍵列,
b.Owner 主鍵擁有者,
b.Table_Name 主鍵表,
d.Column_Name 主鍵列,
c.Constraint_Name 外鍵名,
d.Constraint_Name 主鍵名
From User_Constraints a,
user_Constraints b,
user_Cons_Columns c, --外鍵表
user_Cons_Columns d --主鍵表
Where a.r_Constraint_Name = b.Constraint_Name
And a.Constraint_Type = 'R'
And b.Constraint_Type = 'P'
And a.r_Owner = b.Owner
And a.Constraint_Name = c.Constraint_Name
And b.Constraint_Name = d.Constraint_Name
And a.Owner = c.Owner
And a.Table_Name = c.Table_Name
And b.Owner = d.Owner
And b.Table_Name = d.Table_Name;
需要通過查詢系統(tǒng)表user_constraints來獲得外鍵名稱。
例如,查詢跟emp表相關(guān)的外鍵,可用如下語句:
select?*?from?user_constraints?where?table_name='EMP';
查詢結(jié)果:
其中紅框部分即為外鍵名稱。
有時候刪除某張表記錄的時候,會報錯外鍵約束不能刪除。
如果不了解表之間的關(guān)系,可以通過以下語句查詢到外鍵是建在哪張表上的:
select * from dba_constraints where constraint_name='xxx' and constraint_type = 'R';
例如:我的程序日志中報如下錯誤,我要知道外鍵是在那個表上.
2015-09-08
18:28:18 [ main:261597003 ] - [ ERROR ] java.sql.SQLException:
ORA-02291: 違反完整約束條件 (IRP.FK66EC57AF5158B9FB) - 未找到父項關(guān)鍵字
select * from dba_constraints where constraint_name='FK66EC57AF5158B9FB' and constraint_type = 'R';
例如:
執(zhí)行delete from tablename時報錯:
ORA-02292: integrity constraint (CCSYS.FK_T_BME_TASKRUNRESULT_TASKID) violated - child record found
可以通過執(zhí)行
select table_name from dba_constraints where constraint_name='FK_T_BME_TASKRUNRESULT_TASKID' and constraint_type = 'R';
查詢出外鍵是建在T_BME_TASKRUNRESULT表上的,先把T_BME_TASKRUNRESULT表刪除,就可以刪除 t_bme_task表記錄了。
1、查找表的所有索引(包括索引名,類型,構(gòu)成列):
select
t.*,i.index_type
from
user_ind_columns
t,user_indexes
i
where
t.index_name
=
i.index_name
and
t.table_name
=
i.table_name
and
t.table_name
=
要查詢的表
2、查找表的主鍵(包括名稱,構(gòu)成列):
select
cu.*
from
user_cons_columns
cu,
user_constraints
au
where
cu.constraint_name
=
au.constraint_name
and
au.constraint_type
=
'P'
and
au.table_name
=
要查詢的表
3、查找表的唯一性約束(包括名稱,構(gòu)成列):
select
column_name
from
user_cons_columns
cu,
user_constraints
au
where
cu.constraint_name
=
au.constraint_name
and
au.constraint_type
=
'U'
and
au.table_name
=
要查詢的表
4、查找表的外鍵(包括名稱,引用表的表名和對應(yīng)的鍵名,下面是分成多步查詢):
select
*
from
user_constraints
c
where
c.constraint_type
=
'R'
and
c.table_name
=
要查詢的表
查詢外鍵約束的列名:
select
*
from
user_cons_columns
cl
where
cl.constraint_name
=
外鍵名稱
查詢引用表的鍵的列名:
select
*
from
user_cons_columns
cl
where
cl.constraint_name
=
外鍵引用表的鍵名
5、查詢表的所有列及其屬性
在toad中選中表名,按F4,選擇Referential,即可看到關(guān)于此表關(guān)聯(lián)的主鍵或外鍵的表了,如附件圖。
select * from user_constraints c where c.constraint_type = 'R' and c.table_name = 要查詢的表 。
查詢外鍵約束的列名: select * from user_cons_columns cl where cl.constraint_name = 外鍵名稱
查詢引用表的鍵的列名: select * from user_cons_columns cl where cl.constraint_name = 外鍵引用表的鍵名
查詢表的所有列及其屬性 select t.*,c.COMMENTS from user_tab_columns t,user_col_comments c where t.table_name = c.table_name and t.column_name = c.column_name and t.table_name = 要查詢的表。
甲骨文股份有限公司(Oracle)是全球大型數(shù)據(jù)庫軟件公司,總部位于美國加州紅木城的紅木岸。在2008年,甲骨文股份有限公司是繼Microsoft及IBM后,全球收入第三多的軟件公司。
Oracle數(shù)據(jù)庫產(chǎn)品為財富排行榜上的前1000家公司所采用,許多大型網(wǎng)站也選用了Oracle系統(tǒng)。甲骨文股份有限公司于1989年正式進入中國,在北京、上海、廣州和成都均設(shè)立了分支機構(gòu)。
網(wǎng)站題目:oracle怎么找外鍵,oracle查看外鍵
文章URL:http://chinadenli.net/article45/dseoehi.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供外貿(mào)網(wǎng)站建設(shè)、企業(yè)建站、品牌網(wǎng)站設(shè)計、網(wǎng)站改版、做網(wǎng)站、網(wǎng)站排名
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)