3

I have a data called registration number like follows

COL/A-000001,
KAL/B-000023,
BAL/A-000452,

I know how to validate this type of format using php.But i want to do it when i create the table.IS it possible ?

5
  • What do you mean with "when I create the table" ? Commented Jan 5, 2014 at 17:01
  • We assumed if this column type is VARCHAR(12) i want to validate it with 4 strings ,2 symbols with 6 integers Commented Jan 5, 2014 at 17:03
  • @BartFriederichs So does it possible when create table ? Commented Jan 5, 2014 at 17:04
  • If you want to do validation on the database you'll have to use sql triggers. Here is an example: cvuorinen.net/2013/05/validating-data-with-triggers-in-mysql Commented Jan 5, 2014 at 17:06
  • Related to stackoverflow.com/questions/16005283/… Commented Jan 5, 2014 at 17:10

1 Answer 1

3

Try this:

change (example_tbl / field_name) to your (table / field) names respectively.

DELIMITER $$

CREATE TRIGGER example_before_insert
     BEFORE INSERT ON example_tbl FOR EACH ROW
     BEGIN
          IF NEW.field_name NOT_REGEXP '^[A-Z]{3}\/[A-Z]-\d{6}$' THEN
               SIGNAL SQLSTATE '45000'
                    SET MESSAGE_TEXT = 'Cannot add or update row: regex failed';
          END IF;
     END;
$$
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.