开发者

SQL Multiple Tables and a LookUp Table

I've tried searching around for answers to this but I'm still a bit lost.

I have a site using PHP/SQL to store details about music tracks. I want to search/add/edit/delete tracktitle, album, composer, bpm, timesignature, duration, instrumentation, genre, k开发者_如何学Pythoneywords (description). I have so far done a test version with just three tables and a lookup table (track, composer, album) which seems to work okay with searching.

table 'track' (id, tracktitle) table 'composer' (id, composername) table 'album' (id, albumtitle) table 'albumtrack (track.id, composer.id, album.id)

I can't however seem to get the Insert bit right when adding a new track and assigning a composer and album to it. The tracktitle goes into the track table okay, but how do I link it in the albumtrack lookup table.

Do I need some extra bits in here?

  $sql = "INSERT INTO track SET
  track.tracktitle='$tracktitle' ";

Also... am I going about this the right way by using tables for each bit (bpm table, duration table, genre table)?

A track may have more than one duration (different edits), bpms, time signature so a lot of data will be shared. I also want the user to be able to search on a range of durations or bpm's, eg tracks between 2 and 3 minutes.

Before I expand on my test version I need some advice as to whether or not I'm doing this the best way.

Thanks


Assuming that the "id" fields are autoincrementing values (the database is generating them for you) then, after you insert the track you must recover the id of the just-added row (using mysql_insert_id()). You then use that id, plus the album and composer ids, for an INSERT into albumtrack.

Also, you should be aware that the INSERT syntax you're using is MySQL specific. That's fine if you never contemplate switching DBs, but you might want to learn the "standard" INSERT syntax (INSERT INTO tablename (col1, col2. . .) VALUES (val1, val2. . .)) as well.


Your INSERT query should be something like:

  $sql = "INSERT INTO track VALUES('','$tracktitle','$duration','$etc')";

The first value is left blank for your auto-incrementing id.

"Also... am I going about this the right way by using tables for each bit (bpm table, duration table, genre table)?"

Could you not just have one table with multiple fields(bbm, duration, etc).

Hope this helps.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜