使用 Mysql 和 PHP 构建动态网站
简介
你好!我正在构建一个歌词网站,我会将歌词存储在 MySQL 数据库中。
我想要 2 个主表:1 个用于歌词,其中包含 id、歌词标题、歌词文本、艺术家姓名和观看次数。
另一个表将是艺术家表,其中包含:id、艺术家姓名
问题:
- 如何使用两个表中的艺术家姓名字段链接这两个表?我想从表格中显示我网站上的所有艺术家,并查看与该特定艺术家相关的所有歌词/
- 如何链接到表格中的特定记录或字段?
请帮忙,或者如果您知道任何可以帮助我学习这些内容和其他可以帮助我构建网站的网站或视频,我们将不胜感激。
Intro
Hello! I am in the process of building a lyrics website where I will store the lyrics in a MySQL database.
I want to have 2 main tables: 1 for the lyrics which will have id, lyrics title, lyrics text, artist name,and number of views.
The other table will be the artist table with: id, artist name
QUESTIONS:
- How can I link the two tables by using the artist name field in both table? I want to display all the artists on my site from the table and see all the lyrics related to that particular artist/
- How can I link to a particular record or field in a table?
Please help or if you know of any sites or videos that can help me learn these and other things that may help me in building my site would be appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不要在两个表中都存储艺术家姓名。这违背了拥有单独的艺术家表的目的。而是将 ArtistID 存储在 Lyrics 表中,并且仅将 ArtistName 存储在 Artist 表中。这样,例如,如果您拼错了一位艺术家的名字,您只需更新一个表,并且不会破坏歌词和艺术家之间的关系。
表使用外键链接在一起关系。
Do not store the artist name in both tables. That defeats the purpose of having a separate artist table. Instead store the ArtistID in the Lyrics table, and only store the ArtistName in the Artist table. That way if you, for example, misspell an artist's name, you will only have to update one table, and you will not break the relationship between Lyrics and Artist.
Tables are linked together using Foreign Key relations.
我回答第一个问题。
您确实应该使用
id_artist
字段而不是艺术家姓名
来更改歌词表。这可以确保您不会重复名称(从而浪费空间),并且您不能编写错误的名称,从而在查询期间导致错误的结果。
所以你可以做
I answer first question.
You should really change your lyrics table using an
id_artist
field instead ofartist name
.This makes sure you don't duplicate names (so wasting space) and you can't write a wrong name leading you to wrong result during queries.
So you can do