如何向 MySQL 数据字段添加正则表达式?

发布于 2024-09-19 18:12:18 字数 323 浏览 0 评论 0原文

我有下表:

Table Account{
    [...]
    email varchar(100),
    [...]
}

以及相应的正则表达式:

/^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i

如何使用 MySQL 模型,将正则表达式链接到数据字段“电子邮件”,以便可以通过 php 和 a 来读取正则表达式触发或者甚至更好,在某种约束中?

我想直接使用 MySQL Workbench 来定义它,所以单独的表对我来说不起作用。

I have the following table:

Table Account{
    [...]
    email varchar(100),
    [...]
}

and a corresponding regular expression:

/^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i

How can I use the MySQL model, to link the regular expression to the the data-field "email", so that the regex is accessible to read out through php as well as a trigger or even better, in a kind of constraint?

I want to define this directly using MySQL workbench, so a separate table won't work for me.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

奈何桥上唱咆哮 2024-09-26 18:12:18

在我看来,像这样的输入验证实际上并不是数据库的任务。这些验证规则可能会改变。有一天您甚至可以让用户可以配置它们。如果规则被隐藏在某个地方的某个触发器中,这将变得非常困难。

此外,无论如何,您都需要在应用程序级别处理验证错误 - 显示某种消息。

让 MySQL 中的 PHP 可以访问它们的最佳位置可能是列注释,您可以使用 SHOW COLUMNSFULL 关键字结合使用。

In my opinion, input validation like this is not really the task of the database. Those validation rules may change. You may even make them configurable by the user some day. This becomes awfully difficult if the rules are buried in some trigger somewhere.

Also, you will need to handle a validation error on application level anyway - showing a message of some sort.

The best place to make them accessible to PHP inside mySQL may be a column comment, that you can fetch using SHOW COLUMNS in conjunction with the FULL keyword.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文