简体   繁体   English

SQL多个表和一个查找表

[英]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. 我有一个使用PHP / SQL来存储有关音乐曲目的详细信息的站点。 I want to search/add/edit/delete tracktitle, album, composer, bpm, timesignature, duration, instrumentation, genre, keywords (description). 我想搜索/添加/编辑/删除曲目标题,专辑,作曲家,bpm,时间签名,时长,乐器,类型,关键字(描述)。 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) 表'track'(id,tracktitle)表'composer'(id,composername)表'album'(id,Albumtitle)表'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)? 另外...我是否通过使用每个位的表(bpm表,持续时间表,流派表)来以正确的方式进行操作?

A track may have more than one duration (different edits), bpms, time signature so a lot of data will be shared. 一首曲目可能具有多个持续时间(不同的编辑),bpms,时间签名,因此将共享许多数据。 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. 我还希望用户能够搜索持续时间或bpm的范围,例如2到3分钟之间的曲目。

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() ). 假设“ id”字段是自动递增的值(数据库正在为您生成它们),则在插入轨道之后,您必须恢复刚添加的行的id(使用mysql_insert_id() )。 You then use that id, plus the album and composer ids, for an INSERT into albumtrack. 然后,您可以使用该ID加上唱片集和作曲者ID,将其插入到Albumtrack中。

Also, you should be aware that the INSERT syntax you're using is MySQL specific. 另外,您应该知道,您使用的INSERT语法是MySQL特定的。 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. 如果您从不打算切换数据库,那很好,但是您可能还想学习“标准” INSERT语法( INSERT INTO tablename (col1, col2. . .) VALUES (val1, val2. . .) )。

Your INSERT query should be something like: 您的INSERT查询应该类似于:

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

The first value is left blank for your auto-incrementing id. 自动递增ID的第一个值保留为空白。

"Also... am I going about this the right way by using tables for each bit (bpm table, duration table, genre table)?" “还有……我是否通过使用每个位的表(bpm表,持续时间表,流派表)来正确地做到这一点?”

Could you not just have one table with multiple fields(bbm, duration, etc). 您是否只能拥有一个包含多个字段(bbm,持续时间等)的表。

Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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