将 CONSTRAINT 添加到 table oracle 时出错

get error on add CONSTRAINT to table oracle

我用的是甲骨文19c

table tbl_users 存在并且可以对 table 进行其他约束 这个问题发生在我从 table 中删除 json 约束并想再次添加

当运行这个代码

ALTER TABLE TBL_USERS
ADD CONSTRAINT TBL_USERS_JSON_chk CHECK 
(CUSTOM_DATA IS JSON
AND REQUESTS_STATUS IS JSON
AND LOCATION IS JSON
AND MULTI_ACCESS IS JSON)
ENABLE;

得到这个错误

Error report -
ORA-00604: error occurred at recursive SQL level 1
ORA-00942: table or view does not exist
00604. 00000 -  "error occurred at recursive SQL level %s"
*Cause:    An error occurred while processing a recursive SQL statement
           (a statement applying to internal dictionary tables).
*Action:   If the situation described in the next error on the stack
           can be corrected, do so; otherwise contact Oracle Support.

我可以将其他 CONSTRAINT 添加到 table 但只想添加 JSON CONSTRAINT 以获得此错误

SQL* 输出

SQL> SELECT COUNT(*) FROM ECODB.TBL_USERS;

  COUNT(*)
----------
     24364

SQL> ALTER TABLE ECODB.TBL_USERS
  2  ADD CONSTRAINT TBL_USERS_JSON_chk CHECK
  3  (CUSTOM_DATA IS JSON
  4  AND REQUESTS_STATUS IS JSON
  5  AND LOCATION IS JSON
  6  AND MULTI_ACCESS IS JSON)
  7  ENABLE;
ALTER TABLE ECODB.TBL_USERS
*
ERROR at line 1:
ORA-00604: error occurred at recursive SQL level 1
ORA-00942: table or view does not exist

Oracle 说明了一切:

ORA-00942: table or view does not exist

您不能对不存在的 table 添加约束。长得像你

  • 没有创建它
  • 放弃了
  • 输入错误的名称

请注意,并非所有 Oracle 数据库版本都支持 JSON 检查。例如,在 11g 中它不起作用:

SQL> select * From v$version;

BANNER
--------------------------------------------------------------------------------
Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production
PL/SQL Release 11.2.0.4.0 - Production
CORE    11.2.0.4.0      Production
TNS for Linux: Version 11.2.0.4.0 - Production
NLSRTL Version 11.2.0.4.0 - Production

SQL> create table tbl_users
  2    (custom_data varchar2(20),
  3     requests_status varchar2(20)
  4    );

Table created.

SQL> alter table tbl_users add constraint tbl_users_json_chk check
  2    (    custom_data     is json
  3     and requests_status is json
  4    );
  (    custom_data     is json
                          *
ERROR at line 2:
ORA-00908: missing NULL keyword


SQL>

在 12c 及更高版本中它可以工作:

SQL> select * From v$version;
<snip>
Oracle Database 19c Enterprise Edition Release 19.0.0.0.0 - Production
<snip>

SQL> create table tbl_users
  2    (custom_data varchar2(20),
  3     requests_status varchar2(20)
  4    );

Table created.

SQL> alter table tbl_users add constraint tbl_users_json_chk check
  2    (    custom_data     is json
  3     and requests_status is json
  4    );

Table altered.

SQL>

[编辑]

这真奇怪。转到 My Oracle Support 并找到文档 ID 2665636.1 - 它说在具有依赖视图 的 table 上添加 IS JSON 检查约束失败 .

如果您 运行 此查询(以 ECODB 用户身份连接),您会得到什么?

select name, type
from user_dependencies
where type = 'VIEW'
  and referenced_type = 'TABLE'
  and referenced_name = 'TBL_USERS';

如果查询返回了一些行,那么是的 - 您遇到了错误。

怎么办?应用修复它的补丁。