1

I am trying to get my head around creating classpass like database design. I'm new to database design and there are a few things that are not quite for me how to implement them and I can't quite get my head around.

You can check the classpass example:

EDIT 1: So here is the idea: Each city have multiple neighbourhoods having multiple studios/venues.

After reading spencer7593's comment, here is what I came with and the things that are still not quite clear:

First try

So what I am not quite sure about is:

  1. I am not sure how to store the venue/studio address and geolocation. Is it better to have table Region which defines id | name | parent_id and stores the cities and the neighborhoods recursively? Or add a foreign key constraint to city and neighborhoods? Should I store the lan/lon into the venue table, into the address or even separate locations table? I would like to be able to perform searches like:

    • show me venues in that neighborhood or city
    • show me venues which are in radius XX from position
  2. Each class should have a schedule and currently I am not sure how to design it. For example: Spinning class, Mo, We, Fr from 9 AM till 10 AM. I would like to be able to do queries like:

    • show me venues, which have spinning classes on Mo
    • or show me all classes in category Spinning, Boxing for example
    • or even show me venues offering spinning classes
  3. Should I create an extra table schedules here? Or just create some kind of view which creates the schedule? If it's an extra table, how should I describe start, end of each day of the week?

Dimitar Petrov
  • 667
  • 1
  • 5
  • 16
  • one thing to keep in mind... Entity names are always in singular form. For example, NEIGHBORHOOD instead of NEIGHBORHOOD, CITY instead of CITIES, etc. I'll post an example in a bit. – Sometowngeek Jun 26 '16 at 22:18

3 Answers3

2

@Dimitar,

Even though @rhavendc is correct, this question should be placed in Database Adminstrator, I will answer your question in respective order to the best of my knowledge.

  1. I am not sure how to store the venue/studio address and geolocation. [...]

You can easily find Geo-Locations by searching on the web. take MyGeoPosition for example.

 

I would like to be able to perform searches like

  • show me venues in that neighborhood or city.

You can do this easily. There are a few ways to do it, and each way will require a bit of tweaking with your ERD design. With the example I attached below, you can run a query to list all the venues with the address_id followed by the city id. The yellow entities are the one I added to ensure integrity.

For example:

Refined ERD v1

-- venue.name is using the "[table].[field]" format to help
-- the engine recognize where the field is coming from.
-- This is useful if you are pulling the fields of the
-- same name from different tables.
select  venue.name, city.name
from    venue    join
        address  using (address_id)    join
        city     using (city_id);

NOTE: You don't have to include the city_name. I just threw it in there so you can try it out to see all the venues matching it.

If you would like to do it by the neighborhood, you would have to tweak the ERD I gave you by adding neighbor_id in the ADDRESS table. I have attached the example below, You would also have to add neighborhood_id From there, you can run a query like this:

Using this ERD: Refined ERD v2

-- Remember the format from the previously mentioned code.
select  venue.name, neighborhood.name
from    venue        join
        address      using (address_id)    join
        neighborhood using (neighbor_id);

 

  • show me venues which are in radius XX from position

You can calculate the amount of miles, kilometers, etc. from longitude and latitude using Haversine's Formula.

 

  1. Each class should have a schedule and currently I am not sure how to design it. For example: Spinning class, Mo, We, Fr from 9 AM till 10 AM. I would like to be able to do queries like:
    • show me venues, which have spinning classes on Mo
    • or show me all classes in category Spinning, Boxing for example
    • or even show me venues offering spinning classes

This can be easily derived from either of the ERDs I attached here. In the CLASS table, I added a field called parent_class_id which gets the class_id from the same table. This uses recursion, and I know this is a bit of a headache to understand. This recursion will allow the classes with assigned parent class to show that the classes are also offered at different times.

You can get this result by doing so:

-- Remember the format from the previously mentioned code.
select  class1.name, class1.class_id, class2.class_id
from    class as class1,
        class as class2
where   class1.parent_class_id = class2.class_id;

 

  • or even show me venues offering spinning classes

This may be a tricky one... If you are wondering which venues are offering spinning classes, where spinning is either part of or the name of the class, not a category, it's simple.

Try this...

-- Remember the format from the previously mentioned code.
select  venue_id
from    venue    join
        class    using (venue_id)
where   class_name = 'spinning';

 

NOTE: Keep in mind that most SQL languages are case-sensitive when it comes to searching for literals. You could try using where UPPER(class_name) = 'SPINNING'.

If the class name may include words other than "spinning" in its name, use this instead: where UPPER(class_name) like '%SPINNING%'.

If you are wondering which classes are offering spinning classes where spinning is a category, that's where the tricky bit comes in. I believe you would have to use a subquery for this.

Try this:

-- Remember the format from the previously mentioned code.
select  class_id
from    class             join
        class_category    using (class_id)
where   cat_id =          (select    cat_id
                           from      category
                           where     name = 'spinning');

 

Again, SQL engines are usually sensitive when it comes to literal searches. Make sure your cases are in its correct upper or lower cases.

  1. Should I create an extra table schedules here? Or just create some kind of view which creates the schedule? If it's an extra table, how should I describe start, end of each day of the week?

Yes and no. You could, but if you can understand recursion in database systems, you don't have to.

Hope this helps. :)

Community
  • 1
  • 1
Sometowngeek
  • 597
  • 1
  • 6
  • 27
1

Entity Relationship Modeling.

An entity is a person, place, thing, concept or event that can be uniquely identified, is important to the business, and we can store information about.

Based on information in the question, some candidates to consider as entities might be:

studio

class

rating

neighborhood

city

For each entity, what uniquely identifies it? Figure out the candidate keys.

And figure out the relationships between the entities, and the cardinalities. (What is related to what, and how many, required or optional?)

Is a studio related to a class?

Can a studio have more than one class?

Can a studio have zero classes?

Can a class be related to more than one studio?

Is a neighborhood related to zero, one or more city?

Can a studio be related to more than one neighborhood?

Once you've got the entities and relationships, getting the attributes assigned to each entity is pretty straightforward. Just make sure every attribute is dependent on the key, the whole key, and nothing but the key.

spencer7593
  • 106,611
  • 15
  • 112
  • 140
0

FIRST

Your question is not suited to be posted here in Stack Overflow for I guess it's best to be posted in Database Administrators.


SECOND

Here are some info for reading, just to give you a good start for building your database:

  1. Data Modeling (It's kinda broad but it's for the better)
  2. Logical Data Model (Short but comprehensive one)

THIRD

Basically, when designing your database you should first know all the data that would be needed in your system and group them (if needed) to make it small. Normalize it to reduce data redundancy.

EXAMPLE

Let's assume that table venue would be your main table or the center of all the transaction in your system. By that, venue may have subdata for example branch that may hold different branch location... and that branch may have subdata too for example schedule, teacher and/or class which may also related to each other (subdata gets data from another subdata)... so forth and so on with dependent tables.

Then you can also create independent tables but still have connections with others. For example the neighborhood table, it may contain the neighbor location and main venue location (so it should get the id of selected venue from the venuetable)... so forth and so on with related and independent tables.

NOTE

Just remember the "one-to-one, one-to-many" relationship. If a data will be going to hold many kinds of subdata, just split them in different table. If a data will be going to hold only (1) kind of subdata, then put it all in one table.

Community
  • 1
  • 1
rhavendc
  • 985
  • 8
  • 21