1、查找表的所有索引(包括索引名,類型,構(gòu)成列):
目前成都創(chuàng)新互聯(lián)已為數(shù)千家的企業(yè)提供了網(wǎng)站建設(shè)、域名、雅安服務器托管、成都網(wǎng)站托管、企業(yè)網(wǎng)站設(shè)計、岐山網(wǎng)站維護等服務,公司將堅持客戶導向、應用為本的策略,正道將秉承"和諧、參與、激情"的文化,與客戶和合作伙伴齊心協(xié)力一起成長,共同發(fā)展。
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、查找表的外鍵(包括名稱,引用表的表名和對應的鍵名,下面是分成多步查詢):
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、查詢表的所有列及其屬性
以oracle自帶的用戶scott為例。
create?table?dept(
deptno?number(2)?primary?key,?--deptno?為?dept表的主鍵
dname?varchar2(10),
loc?varchar2(9)
);
create?table?emp(
empno?number(4)?primary?key,?--empno?為?emp表的主鍵
ename?varchar2(10),
job?varchar2(9),
mgr?number(4),
hiredate?date,
sal?number(7,2),
comm?number(7,2),
deptno?number(2)?references?dept(deptno)?--dept表中deptno字段?為?emp表的外鍵
);
查看表索引、主鍵、外鍵、約束
(包括索引名,類型,構(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;
有時候刪除某張表記錄的時候,會報錯外鍵約束不能刪除。
如果不了解表之間的關(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表記錄了。
--使用表級約束
CREATE TABLE table_name
(column_1 datatype ,
column_2 datatype ,
...
CONSTRAINT fk_column
FOREIGN KEY (column_1, column_i, ... column_n)
REFERENCES parent_table (column_1, column_i, ... column_n)
);
--使用列級約束
CREATE TABLE table_name
(column_1 datatype ,
column_2 datatype CONSTRAINT fk_column REFERENCES parent_table (column_name),
...
);
文章名稱:oracle如何表外鍵,oracle創(chuàng)建表外鍵
文章轉(zhuǎn)載:http://chinadenli.net/article32/heeipc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供全網(wǎng)營銷推廣、網(wǎng)站改版、品牌網(wǎng)站設(shè)計、靜態(tài)網(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)