How to validate Alpha-numeric when inserting into table

I wanted to validate the employee code that is getting inserted, such that only the first 3 characters are alphabets and the other 4 characters are numbers.
Ex. ‘SUN0001’.
I am confused, and wanted to know if I can do this in CRDB.

You can do this with a CHECK constraint and the ~ (posix regex) operator:

root@:26257/> create table test.checktest (a string check (a ~ '^[A-Za-z]{3}[0-9]{4}$'));
CREATE TABLE

Time: 8.784ms

root@:26257/> insert into test.checktest values ('sun0010'), ('SUN0011');
INSERT 2

Time: 6.947ms

root@:26257/> INSERT INTO test.checktest VALUES ('*#!0010'), ('007BOND');
pq: failed to satisfy CHECK constraint (a ~ '^[A-Za-z]{3}[0-9]{4}')
1 Like

Thank you so much, this really helped.
Really feel that CRDB is a fully thought through DB, Incredible.