Mysql does not suppot for check restriction but oracle does. Here using trigger we can achive the same restrction as check does.
Here is a IDcard field in table student with a restriction that the length of IDCrad must less than 18.
trigger-name: credit_tri
target-table:student
target-field:IDCard
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 $
原文:http://www.cnblogs.com/22Kon/p/6713688.html