我无法用外键关系截断我的数据。截断子数据后被截断

I am unable to trucate my data with foreign key relation. After truncating the child data is truncated

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

alter table <Table Name> disable constraint <constraint name>;

禁用约束删除数据并重新添加约束。

示例 table 和数据:

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>

这就是您所做的 - 截断的细节(子级)table:

SQL> truncate table temp;

Table truncated.

截断母版(父级)table 不起作用:

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

因此,禁用外键约束,截断 master table 并启用 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.