首页 > 数据库技术 > 详细

Check restriction for mysql

时间:2017-04-15 14:07:37      阅读:463      评论:0      收藏:0      [点我收藏+]

Check restriction for mysql

Mysql does not suppot for check restriction but oracle does. Here using trigger we can achive the same restrction as check does.

Scenario:

Here is a IDcard field in table student with a restriction that the length of IDCrad must less than 18.

4 parameters we can use:

  1. trigger-name: credit_tri

  2. target-table:student

  3. target-field:IDCard

  4. error-msg:"length is above 18,cannot insert"

mysql> delimiter $
mysql> create trigger credit_tri before insert 
    -> on student for each row
    -> begin
    ->     declare msg varchar(200); 
    ->     if (new.IDCard > 18) then  
    ->         set msg = "length is above 18. Cannot insert.";    
    ->             signal sqlstate HY000 SET message_text = msg;    
    ->     end if;
    -> end $

 

Check restriction for mysql

原文:http://www.cnblogs.com/22Kon/p/6713688.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!