开发者

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:

  1. Defining a mysql stored procedure to perform the validation
  2. Create a trigger to use the validation procedure, e.g. on INSERT
  3. Possibly do something with the error message if validation fails.

For a complete example, take a look at Roland Bouman's blog

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜