<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>惜分飞 &#187; ORA-02266</title>
	<atom:link href="https://www.xifenfei.com/tag/ora-02266/feed" rel="self" type="application/rss+xml" />
	<link>https://www.xifenfei.com</link>
	<description>提供7*24专业数据库(Oracle,SQL Server,MySQL,PostgreSQL等)恢复和技术支持@Tel:17813235971</description>
	<lastBuildDate>Sun, 25 Oct 2026 13:02:42 +0000</lastBuildDate>
	<language>zh-CN</language>
		<sy:updatePeriod>hourly</sy:updatePeriod>
		<sy:updateFrequency>1</sy:updateFrequency>
	
	<item>
		<title>ORA-02266: unique/primary keys in table referenced by enabled foreign keys</title>
		<link>https://www.xifenfei.com/2011/09/ora-02266-uniqueprimary-keys-in-table-referenced-by-enabled-foreign-keys.html</link>
		<comments>https://www.xifenfei.com/2011/09/ora-02266-uniqueprimary-keys-in-table-referenced-by-enabled-foreign-keys.html#comments</comments>
		<pubDate>Sun, 18 Sep 2011 13:32:04 +0000</pubDate>
		<dc:creator><![CDATA[惜分飞]]></dc:creator>
				<category><![CDATA[ORA-xxxxx]]></category>
		<category><![CDATA[ORA-02266]]></category>

		<guid isPermaLink="false">https://www.xifenfei.com/?p=1593</guid>
		<description><![CDATA[联系：手机/微信(+86 17813235971) QQ(107644445)  &#8230; <a href="https://www.xifenfei.com/2011/09/ora-02266-uniqueprimary-keys-in-table-referenced-by-enabled-foreign-keys.html">继续阅读 <span class="meta-nav">&#8594;</span></a>]]></description>
				<content:encoded><![CDATA[<div id="xifenfeicopyright">
<p>联系：<a href="https://www.xifenfei.com/about_xifenfei" target="_blank">手机/微信(+86 17813235971)   QQ(107644445)</a><a target="_blank" href="http://wpa.qq.com/msgrd?v=3&#038;uin=107644445&#038;site=qq&#038;menu=yes"><img src="https://www.xifenfei.com/wp-content/themes/img/site_qq.jpg" alt="QQ咨询惜分飞" title="QQ咨询惜分飞"/></a></p>
<p>标题：<a href="https://www.xifenfei.com/2011/09/ora-02266-uniqueprimary-keys-in-table-referenced-by-enabled-foreign-keys.html" target="_blank">ORA-02266: unique/primary keys in table referenced by enabled foreign keys</a></p>
<p>作者：<a href="https://www.xifenfei.com" target="_blank">惜分飞</a>©版权所有[未经本人同意,不得以任何形式转载,否则有进一步追究法律责任的权利.]</p>
</div>
<p>truncate清空一张表记录发现ORA-02266，进行模拟测试</p>
<pre class="brush: sql; title: ; notranslate">
SQL&gt; truncate table p;
truncate table p
               *
ERROR at line 1:
ORA-02266: unique/primary keys in table referenced by enabled foreign keys

--错误原因
SQL&gt; !oerr ora 02266
02266, 00000, &quot;unique/primary keys in table referenced by enabled foreign keys&quot;
// *Cause: An attempt was made to truncate a table with unique or 
//         primary keys referenced by foreign keys enabled in another table.
//         Other operations not allowed are dropping/truncating a partition of a
//         partitioned table or an ALTER TABLE EXCHANGE PARTITION.
// *Action: Before performing the above operations the table, disable the 
//          foreign key constraints in other tables. You can see what 
//          constraints are referencing a table by issuing the following 
//          command:
//          SELECT * FROM USER_CONSTRAINTS WHERE TABLE_NAME = &quot;tabnam&quot;;

--查询是否有主键或者唯一index
SET SERVEROUTPUT ON
SET LINESIZE 1000
SET FEEDBACK OFF 
SET LONG 999999    
SET PAGESIZE 1000
EXEC DBMS_METADATA.SET_TRANSFORM_PARAM(DBMS_METADATA.SESSION_TRANSFORM,'STORAGE',false);--不显示存储信息
SQL&gt; SELECT DBMS_METADATA.GET_DDL('TABLE','P') FROM DUAL;

DBMS_METADATA.GET_DDL('TABLE','P')
--------------------------------------------------------------------------------

  CREATE TABLE &quot;CHF&quot;.&quot;P&quot;
   (    &quot;X&quot; NUMBER(*,0),
         PRIMARY KEY (&quot;X&quot;)
  USING INDEX PCTFREE 10 INITRANS 2 MAXTRANS 255
  TABLESPACE &quot;USERS&quot;  ENABLE
   ) PCTFREE 10 PCTUSED 40 INITRANS 1 MAXTRANS 255 NOCOMPRESS LOGGING
  TABLESPACE &quot;USERS

--查询主外键关系
SQL&gt; col 主键表名 for a20
SQL&gt; col 主键列名 for a20
SQL&gt; col 外键表名 for a20
SQL&gt; col 外键列名  for a20
SQL&gt; select b.table_name  主键表名,
       b.column_name 主键列名,
       a.table_name  外键表名,
       a.column_name 外键列名
  from (select a.constraint_name,
               b.table_name,
               b.column_name,
               a.r_constraint_name
          from dba_constraints a, dba_cons_columns b
         WHERE a.constraint_type = 'R'
           and a.constraint_name = b.constraint_name) a,
       (select distinct a.r_constraint_name, b.table_name, b.column_name
          from dba_constraints a, dba_cons_columns b
         WHERE a.constraint_type = 'R'
           and a.r_constraint_name = b.constraint_name) b
 where a.r_constraint_name = b.r_constraint_name
and b.table_name='P'; 

主键表名             主键列名             外键表名             外键列名
-------------------- -------------------- -------------------- --------------------
P                    X                    C                    X

1 row selected.

--处理方法一
SQL&gt; alter table p disable primary key cascade;

Table altered.

SQL&gt; truncate table p;

Table truncated.

SQL&gt; alter table p enable primary key;

Table altered.

--处理方法二
SQL&gt; delete from t;

1 row deleted.


SQL&gt; commit;

Commit complete.
</pre>
<div id="crp_related"><ul><li style="list-style-type:decimal;font-size:11px;" ><a href="https://www.xifenfei.com/2011/01/%e6%9f%a5%e8%af%a2oracle%e4%b8%ad%e4%b8%bb%e5%a4%96%e9%94%ae%e5%88%97.html" rel="bookmark" class="crp_title">查询oracle中主外键列</a></li><li style="list-style-type:decimal;font-size:11px;" ><a href="https://www.xifenfei.com/2012/07/%e4%bd%bf%e7%94%a8copy%e5%ae%9e%e7%8e%b0long%e7%b1%bb%e5%9e%8b%e8%bd%ac%e7%a7%bb%e8%a1%a8%e7%a9%ba%e9%97%b4.html" rel="bookmark" class="crp_title">使用copy实现long类型转移表空间</a></li><li style="list-style-type:decimal;font-size:11px;" ><a href="https://www.xifenfei.com/2010/08/oracle%e4%b9%8b%e7%ba%a6%e6%9d%9f.html" rel="bookmark" class="crp_title">oracle之约束</a></li><li style="list-style-type:decimal;font-size:11px;" ><a href="https://www.xifenfei.com/2012/05/%e4%bd%bf%e7%94%a8dbms_metadata-get_ddl%e5%87%ba%e7%8e%b0ora-31605%e9%94%99%e8%af%af.html" rel="bookmark" class="crp_title">使用dbms_metadata.get_ddl出现ORA-31605错误</a></li><li style="list-style-type:decimal;font-size:11px;" ><a href="https://www.xifenfei.com/2012/03/%e5%85%b3%e4%ba%8esmon_scn_time%e8%8b%a5%e5%b9%b2%e9%97%ae%e9%a2%98%e8%af%b4%e6%98%8e.html" rel="bookmark" class="crp_title">关于SMON_SCN_TIME若干问题说明</a></li><li style="list-style-type:decimal;font-size:11px;" ><a href="https://www.xifenfei.com/2010/08/oracle%e4%b9%8b%e4%b8%b4%e6%97%b6%e8%a1%a8%e5%88%9b%e5%bb%ba.html" rel="bookmark" class="crp_title">oracle之表管理</a></li><li style="list-style-type:decimal;font-size:11px;" ><a href="https://www.xifenfei.com/2012/03/%e8%af%af%e5%88%a0%e9%99%a4dual%e8%a1%a8%e6%81%a2%e5%a4%8d.html" rel="bookmark" class="crp_title">误删除dual表恢复</a></li><li style="list-style-type:decimal;font-size:11px;" ><a href="https://www.xifenfei.com/2011/05/mysql%e5%88%86%e5%8c%ba%e7%ae%a1%e7%90%86.html" rel="bookmark" class="crp_title">MySql分区表管理</a></li><li style="list-style-type:decimal;font-size:11px;" ><a href="https://www.xifenfei.com/2026/05/ora-39083-ora-14102.html" rel="bookmark" class="crp_title">impdp报ORA-39083 ORA-14102错误处理</a></li><li style="list-style-type:decimal;font-size:11px;" ><a href="https://www.xifenfei.com/2011/02/goldengate-%e5%bc%82%e5%b8%b8.html" rel="bookmark" class="crp_title">goldengate 异常处理</a></li><li style="list-style-type:decimal;font-size:11px;" ><a href="https://www.xifenfei.com/2011/08/%e5%9c%a8%e7%ba%bf%e9%87%8d%e5%ae%9a%e4%b9%89%e5%8e%9f%e7%90%86%e6%8e%a2%e8%ae%a8.html" rel="bookmark" class="crp_title">在线重定义原理探讨</a></li><li style="list-style-type:decimal;font-size:11px;" ><a href="https://www.xifenfei.com/2014/10/%e8%a7%a3%e5%86%b3ora-00701-object-necessary-for-warmstarting-database-cannot-be-altered%e9%94%99%e8%af%af.html" rel="bookmark" class="crp_title">bootstrap$核心index(I_OBJ1,I_USER1,I_FILE#_BLOCK#,I_IND1,I_TS#,I_CDEF1等)异常恢复&#8212;ORA-00701错误解决</a></li></ul></div>]]></content:encoded>
			<wfw:commentRss>https://www.xifenfei.com/2011/09/ora-02266-uniqueprimary-keys-in-table-referenced-by-enabled-foreign-keys.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
