<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 中强制执行数据检查

        Is it Possible to Enforce Data Checking in MySQL using Regular expression(是否可以使用正则表达式在 MySQL 中强制执行数据检查)

        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 中强制执行数据检查的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  ibtmp1是非压缩的innodb临时表的独立表空间,通过innodb_temp_data_file_path参数指定文件的路径,文件名和大小,默认配置为ibtmp1:12M:autoextend,也就是说在文件系统磁盘足够的情况下,这个文件大小是可以无限增长的。 为了避免ibtmp1文件无止境的暴涨导致
                  What does SQL clause quot;GROUP BY 1quot; mean?(SQL 子句“GROUP BY 1是什么意思?意思是?)
                  MySQL groupwise MAX() returns unexpected results(MySQL groupwise MAX() 返回意外结果)
                  MySQL SELECT most frequent by group(MySQL SELECT 按组最频繁)
                  Why Mysql#39;s Group By and Oracle#39;s Group by behaviours are different(为什么 Mysql 的 Group By 和 Oracle 的 Group by 行为不同)
                  MySQL GROUP BY DateTime +/- 3 seconds(MySQL GROUP BY DateTime +/- 3 秒)

                  <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>

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

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