将一张表连接到多张表

发布于 2024-10-01 17:22:45 字数 1570 浏览 4 评论 0原文

我正在构建一个评论系统,人们可以在其中对上传的文件、消息和待办事项进行评论。将评论表连接到其他各种表的最佳方法是什么?
可能的解决方案
解决方案一 - 使用两个字段外键。

CREATE TABLE `comments`(
id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
foreign_key INT NOT NULL,
table_name enum('files','messages','to-do'),
user_id INT NOT NULL,
comment TEXT NOT NULL);

解决方案二 - 每个表都有一个数据库唯一的主键。所以我会使用 php 的 uniqid($prefix) 作为每个表的主键。

CREATE TABLE `comments`(
id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
foreign_key char(23) NOT NULL,
table_name enum('files','messages','to-do'),
user_id INT NOT NULL,
comment TEXT NOT NULL);

解决方案三 - 评论表中有多个外键

CREATE TABLE `comments`(
id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
files_id INT NOT NULL,
messages_id INT NOT NULL,
to_do_id INT NOT NULL,
user_id INT NOT NULL,
comment TEXT NOT NULL);

最好的解决方案是什么?感谢您的意见,如果我可以澄清

编辑解决方案三中删除的任何内容,请告诉我,因为它是复制粘贴错误 关于乔的回应

假设: 1)所有数据都已转义。我们真的需要看到这一点吗?
2) $fileId = "146".
3) $userId = "432".
4) $comment = “Stackoverflow 太棒了!”

插入

$link = mysql_connect('localhost', 'mysql_user', 'mysql_password');
if (!$link) {
    die('Could not connect: ' . mysql_error());
}
 mysql_select_db('mydb');
 mysql_query("INSERT INTO `comments` (user_id,comment) VALUES($userId,$comment)");
 $commentId = mysql_insert_id();
 mysql_query("INSERT INTO `comments_files_xref` (file_id,comment_id)         VALUES($fileId,$commentId)");

I am building a commenting system where people can comment on uploaded files, messages and to-do items. What is the best way to connect the comment table table to the other various tables?
Possible Solutions
Solution one - use a two field foreign key.

CREATE TABLE `comments`(
id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
foreign_key INT NOT NULL,
table_name enum('files','messages','to-do'),
user_id INT NOT NULL,
comment TEXT NOT NULL);

Solution two - Each table would have a primary key unique to the database. So I would use php's uniqid($prefix) as the primary keys for each table.

CREATE TABLE `comments`(
id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
foreign_key char(23) NOT NULL,
table_name enum('files','messages','to-do'),
user_id INT NOT NULL,
comment TEXT NOT NULL);

Solution Three - Have multiple foreign keys in the comment table

CREATE TABLE `comments`(
id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
files_id INT NOT NULL,
messages_id INT NOT NULL,
to_do_id INT NOT NULL,
user_id INT NOT NULL,
comment TEXT NOT NULL);

What is the best solution? I appreciate your input and please let me know if I can clarify anything

EDIT removed table_name from solution three as it was a copy_paste error
As to Joe's Response

Assume:
1) all data is already escaped. Do we really need to see that?
2) $fileId = "146".
3) $userId = "432".
4) $comment = "Stackoverflow is so awesome!"

INSERT

$link = mysql_connect('localhost', 'mysql_user', 'mysql_password');
if (!$link) {
    die('Could not connect: ' . mysql_error());
}
 mysql_select_db('mydb');
 mysql_query("INSERT INTO `comments` (user_id,comment) VALUES($userId,$comment)");
 $commentId = mysql_insert_id();
 mysql_query("INSERT INTO `comments_files_xref` (file_id,comment_id)         VALUES($fileId,$commentId)");

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

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

发布评论

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

评论(4

擦肩而过的背影 2024-10-08 17:22:45

就我个人而言,我会更加规范化设计。也许类似于:

alt text

Personally, I would normalize the design a bit more. Perhaps something like:

alt text

情深如许 2024-10-08 17:22:45

多点注释:

  • 您不应该将外键称为foreign_key,因为外键是一个约束,而不是某种程度上的字段。它将一个表中的字段引用到另一个表中的索引,以与引用的表上的 PK 相同的方式调用它,或者使用可识别的名称。
  • 外键约束仅适用于 innodb,如果您使用 MyISAM,请忘记它们并使用 PHP 进行大量检查。
  • 阅读 http://dev.mysql.com/ doc/refman/5.0/en/innodb-foreign-key-constraints.html
  • 您应该在数据库中画出您想要的内容的草图,或者使用诸如 mysql 工作台(免费),有助于更好地查看架构。

正如我看到你的问题,如果你想在这里使用约束,我将使用一个或另一个解决方案:

1-

CREATE TABLE `comments`(
id INT NOT NULL AUTO_INCREMENT PRIMARY KEY, // Which is your comment index
idTable INT NOT NULL, // ID of the message
table_name enum('files','messages','to-do'), // which it comes from
user_id INT NOT NULL, // etc...
comment TEXT NOT NULL);

但有条件:

  • 文件的 PK消息 _to-do_ 必须具有相同的格式 (INT)
  • 如果您想添加模块(到文件消息 _to-do_),则会很困难

2-
创建连接评论和其他表的表:

CREATE TABLE `comments`(
id INT NOT NULL AUTO_INCREMENT PRIMARY KEY, // Which is your comment index
user_id INT NOT NULL, // etc...
comment TEXT NOT NULL);

CREATE TABLE `comments-files`(
id_comments INT NOT NULL PRIMARY KEY,
id_files INT NOT NULL PRIMARY KEY);

等等。希望您明白这里的要点。您通过 http://dev 添加约束。 mysql.com/doc/refman/5.0/en/innodb-foreign-key-constraints.html(如果需要)。

Multiple remarks :

  • You shouldn't call your foreign key foreign_key because a foreign key is a constraint, not a field in a way. it references a field in a table to an index on another, call it the same way you called the PK on the table you reference, or something recognizable.
  • Foreign keys constraints only works on innodb, if you use MyISAM forget about them and do a lot of checks with PHP.
  • read http://dev.mysql.com/doc/refman/5.0/en/innodb-foreign-key-constraints.html
  • You should make a sketch of what you want in your DB or use tools like mysql workbench (which is free) that helps seeing the schema better.

As I see your problem and if you want to use constraint here, I'll use solution one or another solution :

1-

CREATE TABLE `comments`(
id INT NOT NULL AUTO_INCREMENT PRIMARY KEY, // Which is your comment index
idTable INT NOT NULL, // ID of the message
table_name enum('files','messages','to-do'), // which it comes from
user_id INT NOT NULL, // etc...
comment TEXT NOT NULL);

But there are conditions :

  • The PK of files, messages _to-do_ must have the same format (INT)
  • If you want to add a module (to files, messages _to-do_) it'll be difficult

2-
Create tables joining comments and other tables :

CREATE TABLE `comments`(
id INT NOT NULL AUTO_INCREMENT PRIMARY KEY, // Which is your comment index
user_id INT NOT NULL, // etc...
comment TEXT NOT NULL);

CREATE TABLE `comments-files`(
id_comments INT NOT NULL PRIMARY KEY,
id_files INT NOT NULL PRIMARY KEY);

etc. Hope you see the point here. You add constraint thanks to http://dev.mysql.com/doc/refman/5.0/en/innodb-foreign-key-constraints.html if needed.

感性 2024-10-08 17:22:45

我刚刚在现在的公司学习Ruby on Rails,首选解决方案1,因为RoR的Active Record可以将其处理为多态关系。

回到主题,您使用的是 PHP,我更喜欢解决方案 1 或 3。如果将来评论表有可能用于其他表,那么解决方案 1 是更可取的。

需要注意的是,在解决方案 3 中,我认为不需要 table_name 列。您可以通过使用 id 填充 files_idmessages_idto_do_id 来确定评论针对哪个表,然后设置 2 个其他外键0。

I just learn Ruby on Rails in my current company, and solution 1 is preferred because RoR's Active Record can handle it as polymorphic relation.

Back to the topic, that you are using PHP, I prefer either solution 1 or 3. Solution 1 is preferable if there are possibilities that the comment table will be used for other table in the future.

One note, in solution 3, I think the table_name column is not needed. You can determine for which table the comment is by fill either files_id, messages_id, or to_do_id with the id, then set 2 other foreign key with 0.

孤独陪着我 2024-10-08 17:22:45

我会为每个可以评论的事物创建一个连接表,即 files_comments 表和 todo_comments 表。但解决方案 1 是一种替代方案。我会避免解决方案二或三...如果将来情况发生变化,可能会变得混乱。

I would create a join table for each thing that can be commented on, so an files_comments table and a todo_comments table. But solution 1 would be an alternative. I would avoid solutions two or three... could get messy if things change in the future.

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