在 MYSQL 中操作外键输出

发布于 2024-11-29 03:36:24 字数 405 浏览 3 评论 0原文

我有一个表通过外键连接到另一个表;

  1. 首先表是品牌;它有一个brand_id和brand_name字段
  2. 第二个表是产品线;它有一个 Line_id 和 line_name 字段
  3. 第三个表是提供的线路;它有一个 id、Brand(这是第一个表中的外键)和 Line_name(这是第二个表中的外键)。

如果我查看 mysql 上的第三个表,这些字段包含外键的 id 号 。

我的问题是,存储的值是否可能是名称本身而不是 ID?或者是否可以在我的第三个表上添加一个名为 Brand_name 的字段,该字段是一个将显示确切品牌的 VARCHAR 第一个表中的名称示例。我的第三个表的值将是“1”、“3”、“ID 号为 3 的品牌的品牌名称”、“25”; - 如果是,我不知道如何执行此操作。

I have a table connected to another table through foreign key;

  1. First Table is the brand; it has a brand_id and brand_name fields
  2. Second Table is the Product Line; it has a Line_id and line_name fields
  3. Third Table is the Lines Offered; it has a id, Brand (which is a foreign key from the first table), and Line_name (which is a foreign key from the second table.

If i will look into the third table on mysql the fields contains the id numbers of the foreign keys.

My question is this, is it possible that the stored value will be the name itself and not the ID? or Is it possible to add a field on my third table named it as Brand_name which is a VARCHAR that will display the exact brand name from the first table. Example the values of my third table would be '1','3','The Brand Name of brand with id no. 3','25'; - If yes i dont have any idea how to do this.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

相思碎 2024-12-06 03:36:24

没有什么可以阻止你这样做。您只需将 brand_name 字段添加到“第三个表”...需要提出的问题是:您为什么要这样做?

brands
_________________________
| brand_id | brand_name |
|     1    |   brand1   |
|     2    |   brand2   |
|     3    |   brand3   |
|     4    |   brand4   |
|     5    |   brand5   |
-------------------------


lines
_________________________
| line_id  | line_name  |
|     1    |   line1    |
|     2    |   line2    |
|     3    |   line3    |
|     4    |   line4    |
|     5    |   line5    |
-------------------------


linked
_________________________________________________
|    id    |  line_id   | brand_id | brand_name |
|     1    |     5      |     1    |   brand1   |
|     2    |     5      |     2    |   brand2   |
|     3    |     4      |     2    |   brand2   |
|     4    |     4      |     3    |   brand3   |
|     5    |     4      |     3    |   brand3   |
|     6    |     3      |     4    |   brand4   |
|     7    |     3      |     4    |   brand4   |
|     8    |     2      |     4    |   brand4   |
|     9    |     2      |     5    |   brand5   |
|     10   |     1      |     5    |   brand5   |
------------------------------------------------

这是您建议的设置。现在,如果我们:

SELECT Brand_id, Brand_name FROM linked WHERE line_id = 4;

我们会得到:

_________________________
| brand_id | brand_name |
|     2    |   brand2   |
|     3    |   brand3   |
|     3    |   brand3   |
-------------------------

但是在没有重复数据的情况下也可以实现同样的效果(在大型数据库中,有这样的重复数据是相当不合理的),并且不需要每次品牌名称更改时都更新链接表和品牌表,方法是使用:

SELECT linked.brand_id, brands.brand_name 
FROM brands, linked
WHERE linked.line_id = 4 AND brands.brand_id = linked.brand_id;

_________________________
| brand_id | brand_name |
|     2    |   brand2   |
|     3    |   brand3   |
|     3    |   brand3   |
-------------------------

这个答案不需要那么长。我在做桌子时很开心。

Nothing is preventing you from doing this. You would simply add a brand_name field to the "third table"... The question begging to be asked is: why do you want to do this?

brands
_________________________
| brand_id | brand_name |
|     1    |   brand1   |
|     2    |   brand2   |
|     3    |   brand3   |
|     4    |   brand4   |
|     5    |   brand5   |
-------------------------


lines
_________________________
| line_id  | line_name  |
|     1    |   line1    |
|     2    |   line2    |
|     3    |   line3    |
|     4    |   line4    |
|     5    |   line5    |
-------------------------


linked
_________________________________________________
|    id    |  line_id   | brand_id | brand_name |
|     1    |     5      |     1    |   brand1   |
|     2    |     5      |     2    |   brand2   |
|     3    |     4      |     2    |   brand2   |
|     4    |     4      |     3    |   brand3   |
|     5    |     4      |     3    |   brand3   |
|     6    |     3      |     4    |   brand4   |
|     7    |     3      |     4    |   brand4   |
|     8    |     2      |     4    |   brand4   |
|     9    |     2      |     5    |   brand5   |
|     10   |     1      |     5    |   brand5   |
------------------------------------------------

That's your proposed setup. Now if we:

SELECT brand_id, brand_name FROM linked WHERE line_id = 4;

We would get:

_________________________
| brand_id | brand_name |
|     2    |   brand2   |
|     3    |   brand3   |
|     3    |   brand3   |
-------------------------

But the same could be achieved without duplicate data (in large databases it's pretty unreasonable to have duplicate data like that), and without needing to update BOTH the linked and brand tables every time the brand name changes by using:

SELECT linked.brand_id, brands.brand_name 
FROM brands, linked
WHERE linked.line_id = 4 AND brands.brand_id = linked.brand_id;

_________________________
| brand_id | brand_name |
|     2    |   brand2   |
|     3    |   brand3   |
|     3    |   brand3   |
-------------------------

That answer didn't need to be that long. I was having fun making tables.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文