Is it possible to specify a format for a varchar field in MySQL?

1

I have a column in my database that always starts in the same way, for example let's say it always starts with "ABCD" and then some number Is it possible to create a CONSTRAINT within the database so that it does not allow to enter values that do not start with "ABCD"? o Should I do this already at the code level of the application that connects to the database?

Thanks for your help.

    
asked by N. Agudelo 08.09.2017 в 22:39
source

1 answer

2

You can create a trigger:


CREATE TRIGGER 'NoComienzaPor'
BEFORE INSERT ON 'tabla'
FOR EACH ROW
BEGIN
  IF(SUBSTRING(<campo>,1,4) = "ABCD") THEN
     SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = "El campo debe empezar por ABCD";
  END IF
END$$

To each record that your field does not start with ABCD, an error and the message will be returned.

    
answered by 08.09.2017 / 22:55
source