2012-07-26 44 views
0

如何使message_id成爲外鍵,使其在註釋和消息之間是一對多的關係? (一個消息可以有很多評論。)MySql查詢瀏覽器:爲外鍵更改表

mysql> use nntp; 
Reading table information for completion of table and column names 
You can turn off this feature to get a quicker startup with -A 

Database changed 
mysql> show tables; 
+----------------+ 
| Tables_in_nntp | 
+----------------+ 
| comments  | 
| messages  | 
+----------------+ 
2 rows in set (0.00 sec) 

mysql> describe comments; 
+------------+---------+------+-----+---------+-------+ 
| Field  | Type | Null | Key | Default | Extra | 
+------------+---------+------+-----+---------+-------+ 
| id   | int(11) | NO | PRI | NULL |  | 
| message_id | int(11) | NO |  | NULL |  | 
| comment | text | NO |  | NULL |  | 
| stamp  | date | NO |  | NULL |  | 
+------------+---------+------+-----+---------+-------+ 
4 rows in set (0.00 sec) 

mysql> describe messages; 
+-----------+---------+------+-----+---------+-------+ 
| Field  | Type | Null | Key | Default | Extra | 
+-----------+---------+------+-----+---------+-------+ 
| id  | int(11) | NO | PRI | NULL |  | 
| newsgroup | text | NO |  | NULL |  | 
| subject | text | NO |  | NULL |  | 
| content | text | NO |  | NULL |  | 
| number | text | NO |  | NULL |  | 
+-----------+---------+------+-----+---------+-------+ 
5 rows in set (0.00 sec) 

mysql> quit 
Bye 
[email protected]:~/NetBeansProjects/USENET$ 

我使用的是MySQL查詢瀏覽器,看看:

enter image description here

雖然我可以從查詢瀏覽器或命令行中輸入SQL,我對它不是很熟悉。如果可能的話,我寧願使用GUI查詢瀏覽器。

+0

你在查詢瀏覽器用戶界面中不理解什麼?你的屏幕截圖顯示你知道它在哪裏...只需要填寫外國專欄... – Romain 2012-07-26 13:23:01

+0

看到我的「答案」生成的SQL,這導致了一個錯誤(我沒有寫下來).. – Thufir 2012-07-26 13:34:11

回答

2

應該做的伎倆:

ALTER TABLE comments ADD FOREIGN KEY (message_id) REFERENCES messages(id); 

正如人們可以從閱讀MySQL documentation告訴。