将一个表连接到许多表 [英] Connecting One table to Many tables
问题描述
可能的解决方案
解决方案一使用两个字段的外键。
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);
最好的解决方案是什么?感谢您的投入,请让我知道,如果我可以澄清任何事情
编辑将table_name从解决方案三这是一个copy_paste错误
对于Joe的响应
假设:
1)所有数据都已被转义。我们真的需要看到吗?
2)$ fileId =146。
3)$ userId =432。
4)$评论=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 INTOcomments_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)");
Personally, I would normalize the design a bit more. Perhaps something like:
这篇关于将一个表连接到许多表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!