Check restriction for mysql

MySQL触发器实现Check约束
本文介绍如何使用MySQL触发器来模拟Oracle中的Check约束功能,通过一个具体示例——限制学生表中ID卡字段长度不超过18位——展示了触发器的具体实现过程。

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 $

 

转载于:https://www.cnblogs.com/22Kon/p/6713688.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值