Stop executing query if data is missing
Let`s s开发者_开发问答ay I have the following db schema.
user_id (int) email (varchar) phone (varchar)
How to stop executing of insert query if the value of phone is empty. I could do it with script validation, but I`m interesing of mysql solution.
You could write a procedure and use it to validate and insert rows. Or you could write a trigger to check NEW value and throw an exception. To throw the exception you may use SIGNAL statement in MySQL 5.5, or call unknown stored procedure.
For a solution that works completely within mysql, you can use "triggers". This involves:
- Defining a mysql stored procedure to perform the validation
- Create a trigger to use the validation procedure, e.g. on INSERT
- Possibly do something with the error message if validation fails.
For a complete example, take a look at Roland Bouman's blog
精彩评论