【问题标题】:I am unable to trucate my data with foreign key relation. After truncating the child data is truncated我无法使用外键关系截断我的数据。截断子数据后被截断
【发布时间】:2020-06-23 07:07:39
【问题描述】:

我有具有外键关系的表。我已经截断了子表数据,但之后我也无法删除父表数据。为什么...?我对该外键列有主键约束。

【问题讨论】:

  • 你遇到了什么错误?
  • SQL 错误:ORA-02266:启用的外键 02266 引用的表中的唯一/主键。00000 -“启用的外键引用的表中的唯一/主键”

标签: sql oracle oracle11g sqlplus


【解决方案1】:

示例表和数据:

SQL> create table tdept (deptno number constraint pkd primary key);

Table created.

SQL> create table temp  (emp number primary key, deptno number constraint fkd
  2    references tdept (deptno));

Table created.

SQL>
SQL> insert into tdept values (100);

1 row created.

SQL> insert into temp values (1, 100);

1 row created.

SQL>

这就是你所做的 - 截断的详细信息(子)表:

SQL> truncate table temp;

Table truncated.

截断主(父)表不起作用:

SQL> truncate table tdept;
truncate table tdept
               *
ERROR at line 1:
ORA-02266: unique/primary keys in table referenced by enabled foreign keys

因此,禁用外键约束,截断主表并启用 FK:

SQL> alter table temp disable constraint fkd;

Table altered.

SQL> truncate table tdept;

Table truncated.

SQL> alter table temp enable constraint fkd;

Table altered.

【讨论】:

  • 谢谢。我明白了。
【解决方案2】:
alter table <Table Name> disable constraint <constraint name>;

禁用约束删除数据并再次添加约束。

【讨论】:

  • 我试过了,还是不行。我无法禁用约束,因为它是表的主键。
  • 再一次,没有错误或table DDL,很难猜测。
  • 错误就像不能禁用索引或表的主约束。
  • select CONSTRAINT_NAME from all_constraints where TABLE_NAME =&lt;Your table&gt; and CONSTRAINT_TYPE ='R'
  • @peridin 请禁用上述约束而不是PK
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-09-21
  • 2011-07-30
相关资源
最近更新 更多