选择哪种数据类型以及如何在脚本上实现它(多对多关系)
我有一个表单,用户必须使用复选框选择大量设施。我正在考虑将每个复选框保存为 ENUM('0','1') 数据类型,即设施表,然后将它们显示在我的视图中,如下所示:
<?php
$jacuzzi = $facilities['jacuzzi'] == 1?"jacuzzi available":"jacuzzi not available";
?>
我想知道这是否是最好的方法...
提前致谢。
I have a form where the user must select a large number of facilities using checkboxes. I am thinking to save each checkbox as a ENUM('0','1') datatype is the facilities_table and then display them in my view like this:
<?php
$jacuzzi = $facilities['jacuzzi'] == 1?"jacuzzi available":"jacuzzi not available";
?>
I wonder if this is the best way...
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我通常使用具有以下定义的 TINYINT(1) 列:
我不知道这是否有任何性能优势,但它允许您执行 2 个以上的值,可能最多为 255 TINYINT 是一个字节。但我认为 ENUM 字段需要多个字节。
也就是说,如果您尝试将多个
设施
与多个用户
连接起来,那么也许这样的事情是合适的?通过上表,您可以加入并获取用户想要拥有的设施列表。您甚至不需要存储布尔值,因为该表中存在的行以及将其链接到用户的设施 ID 意味着他们选择了它。
I usually use a TINYINT(1) column with the following definition:
I'm not aware if there is any sort of performance benefit to this, but It would allow you to do more than 2 values, it could be likely up to 255 as TINYINT is a byte. But I imagine that an ENUM field requires more than one byte.
That said, if you're trying to link up multiple
facilities
with multipleusers
then perhaps something like this is in order?With the above table you can just join and get a list of facilities the user would like to have. You don't even need to store a boolean value because the fact that a row in this table exists with the facility id linking it to a user means they chose it.
如果您的数据库支持它,我建议使用
BOOLEAN
数据类型。如果这是 MySQL:
-- 数字类型概述< /a>
If your database supports it, I'd suggest using the
BOOLEAN
data type.If this is MySQL:
-- Numeric Types Overview
如果您有一个用户表和一个设施表,那么您必须将其保存在 user_facility 表中,以保持所有内容标准化。
If you have a user table and a facility table then you must save this in the user_facility table just to keep everything normalized.