0

in mysql i trying to verify a valid email but i don't know how to use a regex with constraint i don't want use TRIGGER it is possible??!

4
  • 2
    MySQL does not support CHECK constraint so IMHO it seems not possible to do it without trigger. Commented Dec 15, 2015 at 21:22
  • Are you trying to do that in MySQL or PHP? Commented Dec 15, 2015 at 21:24
  • You are trying to verify when selecting or inserting/updating? You could verify server side for the inserting/updating. You could select with a regex in where clause. Commented Dec 15, 2015 at 21:27
  • From version 8, MySQL does support checkconstraints: dev.mysql.com/doc/refman/8.0/en/… Commented Jun 9, 2021 at 19:15

1 Answer 1

1

In mySql you could use this for valid email or NOT REGEX for find the invalid

select email from your_table where email  
        REGEXP '^[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$';

or for ORACLE

SELECT email 
FROM your_table
WHERE REGEXP_LIKE (email, '[A-Z0-9._%-]+@[A-Z0-9._%-]+\.[A-Z]{2,4}');

but remember is not an absolute solution for this you could find useful this SO post Using a regular expression to validate an email address

Sign up to request clarification or add additional context in comments.

4 Comments

no i don't went to select from database i wen't add a constraint when i went to insert vales in my table
For this you should create trigger, ( see how create trigger) and apply the regexp in answer stackoverflow.com/questions/16005283/…
thanks for your help,i'm beginner and i look for a simple method
If you want to add constraints to the database solution it is the trigger I do not think there are easier solutions. If you want to use simple solutions to write because you start checking on the values before inserting them in the db

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.