Validate a column if it contains a character, using check constraint in postgresql



  • Table:

    create table tempchkconstraint ( 
      colcode varchar, 
      colname varchar, 
      lastmoddate timestamp without time zone 
    );
    

    Need to validate the column 'colcode' if any data with character '-' is trying to insert/update Constraint added:

    alter TABLE tempchkconstraint 
      add constraint chk_tempchkconstraint_code 
      check (colcode NOT ILIKE '%-%') ;
    

    The constraint is working. Is there any other better way?



  • This condition should be equivalent, but faster:

    strpos(colcode, '-') = 0
    



Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2