Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi all,
Vanilla are planning an update to the site on April 24th (next Wednesday). It is a major PHP8 update which is expected to boost performance across the site. The site will be down from 7pm and it is expected to take about an hour to complete. We appreciate your patience during the update.
Thanks all.

SQL table question?

  • 25-03-2015 1:49pm
    #1
    Registered Users Posts: 2,430 ✭✭✭


    Hi guys. I'm trying to put together a simple beer locator, along the lines of the beer finder app/map from beior.ie. ( I have e-mailed them to let them know I'm doing this, it's not for commercial gain:))

    Im having a little trouble with my database.
    I have a table for beers: (ID, brewery, name, type, abv, price, country).
    and one for bars:(ID, name, address, lat, lang)

    I want each bar to have a selection of beers available, but i'm having trouble doing this. I had initially intended to have a column called "available beers" in the "bars" table, but seeing as each bar will obviously have more than one beer available this won't work as it will break the rule that cells should be atomic.

    Im thinking I may have to do a table of bars vs beers?????


Comments

  • Registered Users Posts: 10,455 ✭✭✭✭28064212


    RINO87 wrote: »
    Im thinking I may have to do a table of bars vs beers?????
    This. It's a many-to-many relationship i.e. one bar can have multiple beers, and one beer can be in multiple bars. So you need a junction table

    Boardsie Enhancement Suite - a browser extension to make using Boards on desktop a better experience (includes full-width display, keyboard shortcuts, and dark mode). Now available through the extension stores

    Firefox: https://addons.mozilla.org/addon/boardsie-enhancement-suite/

    Chrome/Edge/Opera: https://chromewebstore.google.com/detail/boardsie-enhancement-suit/bbgnmnfagihoohjkofdnofcfmkpdmmce



  • Registered Users Posts: 306 ✭✭yes there


    A really simple way would be to create a table beerLists, put it's beer list ID as a value in your available beers column.Each beer list ID can have multiple beerIDs.


  • Registered Users Posts: 2,430 ✭✭✭RINO87


    Thanks for the input folks!


  • Registered Users Posts: 4,468 ✭✭✭CruelCoin


    Have a boolean table maybe?

    Bar1
    beer1 (true/false), beer2(true/false), etc.

    Bar2
    beer1 (true/false), beer2(true/false), etc.

    And so on.


  • Registered Users Posts: 2,430 ✭✭✭RINO87


    CruelCoin wrote: »
    Have a boolean table maybe?

    Bar1
    beer1 (true/false), beer2(true/false), etc.

    Bar2
    beer1 (true/false), beer2(true/false), etc.

    And so on.

    Yeah I had though about going along those lines, however Im hoping to work with 70 beers and 30 bars. It could get messy


  • Advertisement
  • Registered Users Posts: 306 ✭✭yes there


    You could also just annotate that column with a manyToOne bi directional relationship when you implement it in software when making your entity classes depending on your ORM. I know that would work if you are using Hibernate.


  • Registered Users Posts: 1,757 ✭✭✭Deliverance XXV


    Keep it simple and easy to manage.

    Beers
    ID, brewery, name, type, abv, country

    Bars
    ID, name, address, lat, lang

    Stock
    ID, barID, beerID, price

    So one bar would have multiple entries of their different beers in the stock table:

    Stock Example Data
    1, 12, 34, 4.00
    2, 12, 14, 4.00
    3, 12, 10, 4.00
    4, 12, 6, 4.00
    5, 12, 21, 4.00
    ...

    This will make queries much easier. You can query for the cheapest beer with the ID 34 in the stock table, etc.


  • Registered Users Posts: 2,430 ✭✭✭RINO87


    That's perfect, simple really when you see it but the ideas still don't come to me immediately. Thanks folks.


  • Registered Users Posts: 2,781 ✭✭✭amen


    +1 Deliverance XXV but you also need the following tables (normalise)
    Brewery
    BreweryID, Name,TypeID,abvID, CountryId

    BeerType
    TypeID, Beer

    ABV
    ABVid,ABV

    Country
    CountryID, Country

    Beers
    ID, BreweryID, name, typeID, abvID, countryID


  • Registered Users Posts: 40,102 ✭✭✭✭ohnonotgmail


    amen wrote: »
    +1 Deliverance XXV but you also need the following tables (normalise)
    Brewery
    BreweryID, Name,TypeID,abvID, CountryId

    BeerType
    TypeID, Beer

    ABV
    ABVid,ABV

    Country
    CountryID, Country

    Beers
    ID, BreweryID, name, typeID, abvID, countryID

    why would you need a table for the ABV? Surely thats just a number?


  • Advertisement
  • Closed Accounts Posts: 19,777 ✭✭✭✭The Corinthian


    CruelCoin wrote: »
    Have a boolean table maybe?

    Bar1
    beer1 (true/false), beer2(true/false), etc.

    Bar2
    beer1 (true/false), beer2(true/false), etc.

    And so on.
    You go sit in the corner for that.


  • Registered Users Posts: 40,102 ✭✭✭✭ohnonotgmail


    You go sit in the corner for that.

    I've seen worse.


  • Registered Users Posts: 586 ✭✭✭Aswerty


    I've seen worse.

    Bar1
    beer1 ('yes'/'no'), beer2('yes'/'no'), etc.

    Bar2
    beer1 ('yes'/'no'), beer2('yes'/'no'), etc.

    :pac:


  • Registered Users Posts: 2,781 ✭✭✭amen


    hmm I suppose if abv is only a number then you could leave it
    for some reason I had text in mind


Advertisement