选择哪种数据类型以及如何在脚本上实现它(多对多关系)

发布于 2024-10-27 02:51:43 字数 256 浏览 1 评论 0原文

我有一个表单,用户必须使用复选框选择大量设施。我正在考虑将每个复选框保存为 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 技术交流群。

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

发布评论

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

评论(3

那请放手 2024-11-03 02:51:43

我通常使用具有以下定义的 TINYINT(1) 列:

`MyBooleanColumn` TINYINT(1) UNSIGNED DEFAULT 0

我不知道这是否有任何性能优势,但它允许您执行 2 个以上的值,可能最多为 255 TINYINT 是一个字节。但我认为 ENUM 字段需要多个字节。

也就是说,如果您尝试将多个设施与多个用户连接起来,那么也许这样的事情是合适的?

CREATE TABLE `facilities_users` (
  `facility_id` BIGINT() UNSIGNED NOT NULL COMMENT 'foreign key -> facilities.id',
  `user_id` BIGINT() UNSIGNED NOT NULL COMMENT 'foreign key -> users.id',
)

通过上表,您可以加入并获取用户想要拥有的设施列表。您甚至不需要存储布尔值,因为该表中存在的行以及将其链接到用户的设施 ID 意味着他们选择了它。

I usually use a TINYINT(1) column with the following definition:

`MyBooleanColumn` TINYINT(1) UNSIGNED DEFAULT 0

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 multiple users then perhaps something like this is in order?

CREATE TABLE `facilities_users` (
  `facility_id` BIGINT() UNSIGNED NOT NULL COMMENT 'foreign key -> facilities.id',
  `user_id` BIGINT() UNSIGNED NOT NULL COMMENT 'foreign key -> users.id',
)

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.

审判长 2024-11-03 02:51:43

如果您的数据库支持它,我建议使用 BOOLEAN 数据类型。

如果这是 MySQL:

布尔值,布尔值

这些类型是 TINYINT(1) 的同义词。零值被视为错误。非零值被视为 true:

-- 数字类型概述< /a>

If your database supports it, I'd suggest using the BOOLEAN data type.

If this is MySQL:

BOOL, BOOLEAN

These types are synonyms for TINYINT(1). A value of zero is considered false. Nonzero values are considered true:

-- Numeric Types Overview

渡你暖光 2024-11-03 02:51:43

如果您有一个用户表和一个设施表,那么您必须将其保存在 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.

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