在字符串前面加上分隔符,然后像这样匹配:
REGEXP_LIKE(
';' || animal,
'^(;\s*(the cat|the mouse|the dog|the parrot)\s*)*$',
'i'
)
更新:
您可以使用第二个表(带有外键引用)或嵌套表:
CREATE TYPE stringlist IS TABLE OF VARCHAR2(4000);
/
CREATE TABLE animals (
id number,
animal_list stringlist
) NESTED TABLE animal_list STORE AS animals__animal;
ALTER TABLE ANIMALS__ANIMAL ADD CONSTRAINT animals__animal__chk
CHECK ( TRIM( BOTH FROM LOWER( column_value ) )
IN ( 'the cat', 'the mouse', 'the dog', 'the parrot' ) );
INSERT INTO animals VALUES ( 1, StringList( 'the cat', ' the dog ' ) );
-- Succeeds
INSERT INTO animals VALUES ( 2, StringList( 'the cat', 'the horse' ) );
-- Fails with ORA-02290: check constraint (TEST.ANIMALS__ANIMAL__CHK) violated
然后你可以这样做:
SELECT id,
( SELECT LISTAGG( COLUMN_VALUE, ';' ) WITHIN GROUP ( ORDER BY ROWNUM )
FROM TABLE( a.animal_list ) ) AS animals
FROM animals a;
哪个输出:
ID ANIMALS
-- -----------------
1 the cat; the dog