Here is an example. I have a table with a column that contains Enzyme Commission numbers, typically in the form 1.1.1.1
-- i.e. four period-delimited integers where the integers can be 1-3 digits (e.g., 4.2.3.181
...).
I can ensure that pattern through the following CHECK constraint:
...
ec_num TEXT NOT NULL CHECK (ec_num ~ '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}')
...
where
- the tilde (
~
) is a regular expression match ("similarity comparison") operator, described in ref. [1] and [2] (below)
[0-9]
matches any single digit, modified by {n}
which specifies the degree of repetition of the preceding expression -- see ref. [3]
- the period is
\
-escaped, otherwise it would match any single character -- again, see ref [3]
If I try to insert a malformed number, e.g. 1.1.1,4
(note the comma preceding the 4) then PostgreSQL throws an error, similar to:
ERROR: 23514: new row for relation "kegg_enzymes" violates
check constraint "kegg_enzymes_ec_num_check"
DETAIL: Failing row contains
(4, 1.1.1,4, (r,r)-butanediol dehydrogenase, (R,R)-butanediol dehydrogenase).
[1] Difference between LIKE and ~ in Postgres
[2] https://www.postgresql.org/docs/current/functions-matching.html
[3] https://www.gnu.org/software/sed/manual/html_node/Regular-Expressions.html