前置分隔符的字符串匹配,那麼像這樣的列:
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
來源
2017-04-20 09:06:51
MT0
不這樣做。不要在一個列中存儲由某些字符分隔的多個值。閱讀數據庫規範化。不要走這條路。 –