douwen3836 2011-08-11 03:32
浏览 41

在MYSQL中操作外键输出

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.

  • 写回答

1条回答 默认 最新

  • dpxbc88022 2011-08-11 03:56
    关注

    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.

    评论

报告相同问题?

悬赏问题

  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本