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)");

```