204k views
3 votes
What is the best type of query for validating the format of an email address in a MySQL table?

a) SELECT
b) VALIDATE
c) REGEX
d) CHECK

User Luis Jose
by
8.4k points

1 Answer

3 votes

Final answer:

To validate email addresses in a MySQL table, use a SELECT query combined with REGEX (option c). A CHECK constraint can prevent invalid formats from being stored, while REGEX can help find existing invalid emails.

Step-by-step explanation:

When validating the format of an email address in a MySQL table, the best type of query you can use is a CHECK constraint to enforce the correct format for data before it's added to the database. However, if you need to validate emails that are already stored, you would use a SELECT statement in conjunction with a regular expression through the REGEX function (or RLIKE operator).

For example, the following MySQL query can be used to find invalid email addresses assuming the table is named 'users' and the column containing the email addresses is named 'email':

SELECT * FROM users WHERE email NOT REGEXP '^[^at sign]+at sign[^at sign]+\.[^at sign]{2,}$'

This query checks for common elements in an email address, such as the presence of an 'at sign' symbol, followed by a domain. However, it is important to note that MySQL's REGEXP is not fully equipped to catch every possible invalid email according to the complete specifications of a valid email address defined by RFC 5322.

Hence, the answer is option c.

User Kindzoku
by
8.4k points