【问题标题】:MySql query browser: alter table for foreign keyMySql 查询浏览器:alter table for foreign key
【发布时间】:2012-07-25 02:14:08
【问题描述】:

如何使 message_id 成为外键,以便 cmets 和消息之间是一对多的? (一条消息可以有多个 cmets。)

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
thufir@dur:~/NetBeansProjects/USENET$ 

我用的是MySql查询浏览器看看:

虽然我可以从查询浏览器或命令行输入 SQL,但我对它不是很熟悉。如果可能,我更愿意为此使用 GUI 查询浏览器。

【问题讨论】:

  • 查询浏览器 UI 中有哪些不明白的地方?您的屏幕截图显示您知道它在哪里...只需要填写外国列...
  • 查看我对生成的 SQL 的“答案”,这导致了一个错误(我没有写下来)..

标签: mysql sql database-design orm entity-relationship


【解决方案1】:

应该做的伎俩:

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

从阅读MySQL documentation 可以看出。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-03-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-19
    • 2012-09-28
    相关资源
    最近更新 更多