22Kon

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 $

 

分类:

技术点:

相关文章:

  • 2022-12-23
  • 2021-12-06
  • 2021-12-22
  • 2022-12-23
  • 2021-11-23
猜你喜欢
  • 2022-12-23
  • 2021-07-11
  • 2021-09-19
  • 2021-12-13
  • 2021-04-27
  • 2022-02-20
  • 2021-09-10
相关资源
相似解决方案