简体   繁体   中英

MySQL making relations while inserting data into diffrent tables

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

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 (eg Author, Clip, etc). A table models an entity.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM