简体   繁体   中英

How would I go about inserting values that affect multiple tables?

My schema is this:

Tables:
Titles ->         ID |     TitleID    | TitleName | ArtistNameFull | Length
Artists->         ID | ArtistNameRoot
ArtistRelation->  ID | TitleID | ArtistID | Relationship

ArtistNameFull is artist name including: "feat, with, including, featuring, presents, etc."

ArtistNameRoot is just the artist without "feat, with including, featuring, etc."

The relationship between them in ArtistRelation is in another table which has indexes 1-15 and their corresponding "suffix" essentially. As in, index 3 would be "featuring" and index 7 would be "with".

I have about 1000 rows of data that I need to insert into my tables, which is just values of (Title, Artist, length).

Methods so far: My methods so far have been to place all this data that I need inserted into a temporary table itself and then call sql functions that select unique values between the Title table and the temporary table and then insert them, which works if I'm inserting into just the Titles table, but I also need to insert into Artists which is a little more tricky because I need to get rid of anything but the root artist. So then I tried using php to grab data from my temporary table and using regular expressions to get just the root artist, which works wonders for getting just the root artist, but connecting all this together and figuring out how to insert both into artist relation when there could already be artists from previous insertions is hard to wrap my head around.

Anyway, I guess I just need words of wisdom for if I'm going about this in a really inefficient way, or this is how it is usually done and I just have to keep going.

PHP is much better than SQL at manipulating strings. So keep doing that. Also using the temp table is a valid method.

You will need to use PHP to pull all of the data from the temp table.

For each result parse out the artist name and insert it into artists.

After each insert you want to get the last ID that was inserted (PDO and mysqli_ have methods for this) and then store that ID and the artist name in an array so that the ID can be used later.

Then insert the title information.

After each insert get the last ID that was inserted and use it and the artist ID to insert a record into the relations table.

Does this help?

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