<tfoot id='1Mov2'></tfoot>
  • <legend id='1Mov2'><style id='1Mov2'><dir id='1Mov2'><q id='1Mov2'></q></dir></style></legend>
  • <i id='1Mov2'><tr id='1Mov2'><dt id='1Mov2'><q id='1Mov2'><span id='1Mov2'><b id='1Mov2'><form id='1Mov2'><ins id='1Mov2'></ins><ul id='1Mov2'></ul><sub id='1Mov2'></sub></form><legend id='1Mov2'></legend><bdo id='1Mov2'><pre id='1Mov2'><center id='1Mov2'></center></pre></bdo></b><th id='1Mov2'></th></span></q></dt></tr></i><div id='1Mov2'><tfoot id='1Mov2'></tfoot><dl id='1Mov2'><fieldset id='1Mov2'></fieldset></dl></div>
    1. <small id='1Mov2'></small><noframes id='1Mov2'>

          <bdo id='1Mov2'></bdo><ul id='1Mov2'></ul>

        是否可以使用正则表达式在 MySQL 中强制执行数据检查

        时间:2023-05-23

        1. <tfoot id='xPf6I'></tfoot>

          <small id='xPf6I'></small><noframes id='xPf6I'>

              <tbody id='xPf6I'></tbody>
              <legend id='xPf6I'><style id='xPf6I'><dir id='xPf6I'><q id='xPf6I'></q></dir></style></legend>
              • <i id='xPf6I'><tr id='xPf6I'><dt id='xPf6I'><q id='xPf6I'><span id='xPf6I'><b id='xPf6I'><form id='xPf6I'><ins id='xPf6I'></ins><ul id='xPf6I'></ul><sub id='xPf6I'></sub></form><legend id='xPf6I'></legend><bdo id='xPf6I'><pre id='xPf6I'><center id='xPf6I'></center></pre></bdo></b><th id='xPf6I'></th></span></q></dt></tr></i><div id='xPf6I'><tfoot id='xPf6I'></tfoot><dl id='xPf6I'><fieldset id='xPf6I'></fieldset></dl></div>
                  <bdo id='xPf6I'></bdo><ul id='xPf6I'></ul>
                  本文介绍了是否可以使用正则表达式在 MySQL 中强制执行数据检查的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  限时送ChatGPT账号..

                  假设我有一个名为电话号码的属性,并且我想对该字段的条目强制执行某些有效性.我可以为此目的使用正则表达式吗,因为正则表达式在定义约束方面非常灵活.

                  Suppose I have an attribute called phone number and I would like to enforce certain validity on the entries to this field. Can I use regular expression for this purpose, since Regular Expression is very flexible at defining constraints.

                  推荐答案

                  是的,你可以.MySQL 支持正则表达式 (http://dev.mysql.com/doc/refman/5.6/en/regexp.html) 并且对于数据验证,您应该使用触发器,因为 MySQL 不支持 CHECK 约束(您可以随时移动到 PostgreSQL 作为替代:).注意!请注意,尽管 MySQL 确实具有 CHECK 约束构造,但不幸的是 MySQL(到目前为止 5.6)不会根据检查约束验证数据.根据 http://dev.mysql.com/doc/refman/5.6/en/create-table.html:CHECK 子句被解析但被所有存储引擎忽略."

                  Yes, you can. MySQL supports regex (http://dev.mysql.com/doc/refman/5.6/en/regexp.html) and for data validation you should use a trigger since MySQL doesn't support CHECK constraint (you can always move to PostgreSQL as an alternative:). NB! Be aware that even though MySQL does have CHECK constraint construct, unfortunately MySQL (so far 5.6) does not validate data against check constraints. According to http://dev.mysql.com/doc/refman/5.6/en/create-table.html: "The CHECK clause is parsed but ignored by all storage engines."

                  您可以为列电话添加检查约束:

                  You can add a check constraint for a column phone:

                  CREATE TABLE data (
                    phone varchar(100)
                  );
                  
                  DELIMITER $$
                  CREATE TRIGGER trig_phone_check BEFORE INSERT ON data
                  FOR EACH ROW 
                  BEGIN 
                  IF (NEW.phone REGEXP '^(\\+?[0-9]{1,4}-)?[0-9]{3,10}$' ) = 0 THEN 
                    SIGNAL SQLSTATE '12345'
                       SET MESSAGE_TEXT = 'Wroooong!!!';
                  END IF; 
                  END$$
                  DELIMITER ;
                  
                  
                  INSERT INTO data VALUES ('+64-221221442'); -- should be OK
                  INSERT INTO data VALUES ('+64-22122 WRONG 1442'); -- will fail with the error: #1644 - Wroooong!!! 
                  

                  但是,您不应该仅仅依赖 MySQL(在您的情况下是数据层)进行数据验证.数据应在您应用的各个级别进行验证.

                  However you should not rely merely on MySQL (data layer in your case) for data validation. The data should be validated on all levels of your app.

                  这篇关于是否可以使用正则表达式在 MySQL 中强制执行数据检查的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:Mysql插入2个表 下一篇:6000万个条目,选择某个月份的条目.如何优化数据库?

                  相关文章

                  <small id='6o1Qo'></small><noframes id='6o1Qo'>

                    <i id='6o1Qo'><tr id='6o1Qo'><dt id='6o1Qo'><q id='6o1Qo'><span id='6o1Qo'><b id='6o1Qo'><form id='6o1Qo'><ins id='6o1Qo'></ins><ul id='6o1Qo'></ul><sub id='6o1Qo'></sub></form><legend id='6o1Qo'></legend><bdo id='6o1Qo'><pre id='6o1Qo'><center id='6o1Qo'></center></pre></bdo></b><th id='6o1Qo'></th></span></q></dt></tr></i><div id='6o1Qo'><tfoot id='6o1Qo'></tfoot><dl id='6o1Qo'><fieldset id='6o1Qo'></fieldset></dl></div>
                    <tfoot id='6o1Qo'></tfoot>

                      • <bdo id='6o1Qo'></bdo><ul id='6o1Qo'></ul>

                    1. <legend id='6o1Qo'><style id='6o1Qo'><dir id='6o1Qo'><q id='6o1Qo'></q></dir></style></legend>