有时候删除某张表记录的时候,会报错外键约束不能删除.
如果不了解表之间的关系,可以通过以下语句查询到外键是建在哪张表上的:
select * from dba_constraints where constraint_name='xxx' and constraint_type = 'R';
例如:我的程序日志中报如下错误,我要知道外键是在那个表上.
例如:
执行delete from tablename时报错:
可以通过执行
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表记录了.
查看表索引、主键、外键、约束
(包括索引名,类型,构成列)
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' ----指定表
(包括名称,构成列)
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' -----指定表名
(包括表名称,构成列)
SELECT CU.COLUMN_NAME,AU.TABLE_NAME
AND AU.CONSTRAINT_TYPE = 'U'
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;
①.、查找表的所有索引(包括索引名,类型,构成列):
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
要查询的表
cu.*
user_cons_columns
cu,
user_constraints
au
cu.constraint_name
au.constraint_name
au.constraint_type
'P'
au.table_name
column_name
'U'
*
c
c.constraint_type
'R'
c.table_name
查询外键约束的列名:
cl
cl.constraint_name
外键名称
查询引用表的键的列名:
外键引用表的键名
以上就是土嘎嘎小编为大家整理的如何查询oracle外键相关主题介绍,如果您觉得小编更新的文章只要能对粉丝们有用,就是我们最大的鼓励和动力,不要忘记讲本站分享给您身边的朋友哦!!