在Oracle ALL_CONSTRAINTS中仅查找用户定义的检查约束

问题描述 投票:4回答:3

考虑这个表定义:

CREATE TABLE foo (
  a int not null,              -- Implicit not null constraint
  b int check (b is not null), -- Explicit not null constraint
  c int check (c > 1)          -- Explicit constraint
);

我想发现所有显式检查约束,即用户使用CHECK语法在其DDL语句中定义的约束。这些约束可能会也可能不会被命名。在上面的例子中,它们没有命名。我怎样才能发现“显式”检查约束,忽略隐含约束?

例如。当我查询ALL_CONSTRAINTS时:

SELECT *
FROM all_constraints
WHERE constraint_type = 'C'
AND table_name = 'FOO';

我没有看到任何区分显性/隐含性的方法:

CONSTRAINT_NAME   SEARCH_CONDITION   GENERATED
---------------------------------------------------
SYS_C00120656     "A" IS NOT NULL    GENERATED NAME
SYS_C00120657     b is not null      GENERATED NAME
SYS_C00120658     c > 1              GENERATED NAME
sql oracle check-constraints
3个回答
2
投票

SYS.CDEF$.TYPE#知道隐式和显式检查约束之间的区别。隐式检查约束存储为7,显式检查约束存储为1。

--Explicit constraints only.
select constraint_name, search_condition
from dba_constraints
where (owner, constraint_name) not in
    (
        --Implicit constraints.
        select dba_users.username, sys.con$.name
        from sys.cdef$
        join sys.con$
            on cdef$.con# = con$.con#
        join dba_users
            on sys.con$.owner# = dba_users.user_id
        where cdef$.type# = 7
    )
    and constraint_type = 'C'
    and table_name = 'FOO'
order by 1;


CONSTRAINT_NAME   SEARCH_CONDITION
---------------   ----------------
SYS_C00106940     b is not null
SYS_C00106941     c > 1

该解决方案具有依赖于未记录的表的明显缺点。但它似乎比依赖条件的文本更准确。某些隐式检查约束不是使用双引号创建的。我不能重现那个问题,但我发现它发生在桌子上SYS.TAB$


3
投票

我当然可以使用精确的"COLUMN_NAME" IS NOT NULL语法(包括双引号)对某人的不太可能进行启发式操作:

SELECT *
FROM all_constraints
WHERE constraint_type = 'C'
AND table_name = 'FOO'
AND search_condition_vc NOT IN (
  SELECT '"' || column_name || '" IS NOT NULL'
  FROM all_tab_cols
  WHERE table_name = 'FOO'
  AND nullable = 'N'
);

这给了我想要的结果:

CONSTRAINT_NAME   SEARCH_CONDITION   GENERATED
---------------------------------------------------
SYS_C00120657     b is not null      GENERATED NAME
SYS_C00120658     c > 1              GENERATED NAME

我在这里作为答案,因为这对某些人来说可能已经足够了,但我真的想要一个更可靠的解决方案。


2
投票

想法:您可以将表格与其“影子”对应表进行比较。 CREATE TABLE AS不保留用户定义的检查约束:

-- original table
CREATE TABLE foo (
  id int PRIMARY KEY NOT NULL,
  a int not null,              -- Implicit not null constraint
  b int check (b is not null), -- Explicit not null constraint
  c int check (c = 1),          -- Explicit constraint
  d INT CONSTRAINT my_check CHECK (d = 3)
);

-- clone without data(it should be stored in different schema than actual objects)
CREATE TABLE shadow_foo
AS
SELECT * 
FROM foo 
WHERE 1=2;

-- for Oracle 18c you could consider private temporary tables
CREATE PRIVATE TEMPORARY TABLE ora$shadow_foo ON COMMIT DROP DEFINITION
AS
SELECT * FROM foo WHERE 1=2;

主要查询:

SELECT c.*
FROM (SELECT * FROM all_constraints WHERE TABLE_NAME NOT LIKE 'SHADOW%') c
LEFT JOIN (SELECT * FROM all_constraints WHERE TABLE_NAME LIKE 'SHADOW%') c2
  ON c2.table_name = 'SHADOW_' || c.table_name
 AND c2.owner = c.owner
 AND c2.search_condition_vc = c.search_condition_vc
WHERE c2.owner IS NULL
  AND c.constraint_type = 'C'
  AND c.owner  LIKE 'FIDDLE%'

db<>fiddle demo

© www.soinside.com 2019 - 2024. All rights reserved.