Is There A Database Engine that Allows for Queriable Field Constraint Specified by RegEx?

后端 未结 2 2047
醉梦人生
醉梦人生 2020-12-12 05:47

I\'m looking for a database engine that can handle data constraints specified via RegEx. So in addition to the datatype, I want to be able to control the format of the data.

相关标签:
2条回答
  • 2020-12-12 06:26

    Here is a wildcard example in sql server for this.

    create table #Something
    (
        SomeValue varchar(255)
        , constraint MyCheck CHECK (SomeValue like '[a-z][a-z][0-9]%')
    )
    
    insert #Something
    select 'ab3adoofnod'
    --(1 row(s) affected)
    
    insert #Something
    select 'a3b3adoofnod'
    
    Msg 547, Level 16, State 0, Line 1
    The INSERT statement conflicted with the CHECK constraint "MyCheck". The conflict occurred in database "tempdb", table "dbo.#Something__________________________________________________________________________________________________________0000000000DD", column 'SomeValue'.
    The statement has been terminated.
    

    If you want to use some t-sql to view the definitions of your check constraints you can use the sys.check_constraints catalog view.

    Here is an example to view ALL the check constraints for the table above. The definition columns will provide the wildcard searching as defined in the constraint.

    select * 
    from tempdb.sys.check_constraints
    where parent_object_id = object_id('tempdb..#Something')
    
    0 讨论(0)
  • 2020-12-12 06:38

    In Oracle you can specify custom constraints, in which you can use functions that evaluate regexp; for example:

    SQL> create table test_pattern ( txt varchar2(1000))
      2  /
    
    Table created.
    
    SQL> alter table test_pattern add constraint check_pattern check (regexp_instr(txt, '^START') != 0)
      2  /
    
    Table altered.
    
    SQL> insert into test_pattern values ('START a d f  g ')
      2  /
    
    1 row created.
    
    SQL> insert into test_pattern values ('_START a d f  g ')
      2  /
    insert into test_pattern values ('_START a d f  g ')
    *
    ERROR at line 1:
    ORA-02290: check constraint (SIUINTEGRA.CHECK_PATTERN) violated
    

    You can get informations on constraints you set with something like:

    select *
    from dba_constraints       
    where table_name = 'TEST_PATTERN'
    
    0 讨论(0)
提交回复
热议问题