dqo58772 2011-11-27 20:34
浏览 67
已采纳

MySQL在将数据插入不同的表时建立关系

So I have these tables (I'll simplify the real structure):

+----------------+   +----------------+
| CLIPS          |   | AUTHORS        |
+----------------+   +----------------+
| CLIP_ID        |   | AUTHOR_ID (PK) |
| LINK           |   | AUTHOR         |
| AUTHOR_ID (FK) |   | TITLE_ID (FK)  |
| TITLE_ID (FK)  |   +----------------+
| GENRE_ID (FK)  |
+----------------+

+----------------+   +----------------+
| TITLES         |   | GENRES         |
+----------------+   +----------------+
| TITLE_ID (PK)  |   | GENRE_ID (PK)  |
| TITL           |   | GENRE          |
+----------------+   +----------------+ 

How do I make relations while inserting data into these tables (any help is welcome, straight or tutorial links)?

When I insert Michael Jackson into authors, thriller into titles and music clip link into clips, I can have all those records related to (for example) same clip id.

Example: clip with id 2312123 is Michael Jackson thriller [link]

Thanks

  • 写回答

1条回答 默认 最新

  • duandingyou3331 2011-11-27 20:45
    关注

    First you insert the author, title and genre, and retrieve the primary keys for the newly inserted rows. To get the primary keys, something like this might help: http://php.net/manual/en/function.mysql-insert-id.php

    You then use the primary keys to insert the row into clips. The clips table is dependent on the other tables, so you need to fill them first.

    As a further word of advice, database tables should be named in singular (e.g. Author, Clip, etc). A table models an entity.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!