Apparently what you are trying to do is a CHECK against a POSIX regular expression
The syntax would then be:
CREATE TABLE products (
product_no integer NOT NULL,
name text NOT NULL,
price numeric
CONSTRAINT ck_nombre CHECK (name ~ '[A-Z][0-9][0-9]')
);
Where ~
is the operator to say it matches and is case-sensitive.
Currently, when using LIKE
, you are literally defining the text.
Note 1: Your regular expression says: "A capital letter followed by two numbers", but accept that the sequence is in any part of the text. Therefore the text: 'hola abuelita A25 te quiero mucho'
meets the check.
If what you want is exactly that sequence, with nothing before or after, your check should be defined as
CONSTRAINT ck_nombre CHECK (name ~ '^[A-Z][0-9][0-9]$'::text)
Or, simpler:
CONSTRAINT ck_nombre CHECK (name ~ '^[A-Z]\d{2}$'::text)
Where the special characters ^
and $
respectively mean "start with ..." and "end with ...". (and \d
means a numeric character).