将额外数据与 MySQL 列关联

发布于 2024-10-05 19:14:39 字数 476 浏览 0 评论 0原文

我有一个典型的表,例如

id(int) name(varchar) address(varchar) date(datetime)

我还有一个引用每个验证函数的表,例如

id(int) function(varchar) fail_message(varchar)
   1    email             Please enter a valid email address
   2    required          This field can not be left blank

我希望能够将第一个表中的每一列与一个或多个验证器相关联。

我能想到的唯一方法是将 ids 填充到列名称中,例如(列名称:email;1;2)并通过 PHP 跟踪它,但这看起来很混乱。

对于关系数据库有没有好的方法来做到这一点? NoSQL 实现会更适合这个问题吗?

I have a typical table, e.g.

id(int) name(varchar) address(varchar) date(datetime)

I also have a table that references validation functions for each one, e.g.

id(int) function(varchar) fail_message(varchar)
   1    email             Please enter a valid email address
   2    required          This field can not be left blank

I'd like to be able to associate each column from the first table with one or more of these validators.

The only way I can think of doing this is to stuff the ids into the column names e.g. (column name: email;1;2) and keep track of it through PHP, but that seems very messy.

Is there a good way to do this with relational databases? Would a NoSQL implementation suit this problem better?

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

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

发布评论

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

评论(2

等待圉鍢 2024-10-12 19:14:39

与 Dan 所说的类似,在 sql 中实现关联的一个相对简单的方法是执行以下操作:

id(int) function_id(int) col_name(varchar)
1       1                address
2       1                second_address
3       2                address
4       2                name

然后当您想要进行失败检查时,使用上表将错误消息链接到列名(例如 ' select function_id from above_table where col_name="address"') 然后查询故障表。随后可以使用具有联接的视图来组合这些表,以便单个查询就足够了。

希望这有帮助。

Similar to what Dan said, a relatively easy way to implement an association in sql would be to do the following:

id(int) function_id(int) col_name(varchar)
1       1                address
2       1                second_address
3       2                address
4       2                name

And then when you want to do the failure check, use the above table to link the error message to the column name (e.g. 'select function_id from above_table where col_name="address"') and then query the failure table. These tables could subsequently be combined using a view with a join so that a single query would suffice.

Hope this helps.

独行侠 2024-10-12 19:14:39

将其放在另一个描述表列的表中,这很奇怪,这非常像扩展列出带有附加列的表列的表

假设如果您使用本地化字符串扩展示例,这意味着fail_message将成为fail_message_id并且表fail_message 将包含列(id、语言、消息)

put this in another table that describes the columns for tables oddly this is very much like extending the table that lists table columns with additional columns

let's say if you extend your example with say localized strings that would mean that the fail_message would become a fail_message_id and the table fail_message would have the columns (id, language, message)

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