将一张表连接到多张表
我正在构建一个评论系统,人们可以在其中对上传的文件、消息和待办事项进行评论。将评论表连接到其他各种表的最佳方法是什么?
可能的解决方案
解决方案一 - 使用两个字段外键。
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
就我个人而言,我会更加规范化设计。也许类似于:
Personally, I would normalize the design a bit more. Perhaps something like:
多点注释:
正如我看到你的问题,如果你想在这里使用约束,我将使用一个或另一个解决方案:
1-
但有条件:
2-
创建连接评论和其他表的表:
等等。希望您明白这里的要点。您通过 http://dev 添加约束。 mysql.com/doc/refman/5.0/en/innodb-foreign-key-constraints.html(如果需要)。
Multiple remarks :
As I see your problem and if you want to use constraint here, I'll use solution one or another solution :
1-
But there are conditions :
2-
Create tables joining comments and other tables :
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.
我刚刚在现在的公司学习Ruby on Rails,首选解决方案1,因为RoR的Active Record可以将其处理为多态关系。
回到主题,您使用的是 PHP,我更喜欢解决方案 1 或 3。如果将来评论表有可能用于其他表,那么解决方案 1 是更可取的。
需要注意的是,在解决方案 3 中,我认为不需要
table_name
列。您可以通过使用 id 填充files_id
、messages_id
或to_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 eitherfiles_id
,messages_id
, orto_do_id
with the id, then set 2 other foreign key with 0.我会为每个可以评论的事物创建一个连接表,即 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.