Question

I have been given some Exercise Routine content which is to be shown sequentially to user. An example in simplest form is shown below.

id  name        duration    completed
----------------------------------------
1   jumping     10          1
2   running     15          0
3   cycling     12          0
4   push ups    30          1

Usually the tables are minimum 100 rows/exercises and there are about 300 tables (different exercises for different criteria). We don't have any front end and update database directly using dbForge Studio db editor.

The previous developer had designed the table so that the unique id column also serves as sequence number. It also forms part of url. E.g. xxx/exercise/12 specifically looks for actual 12th row with id=12 (he assumed all ids are incrementing with 1 and are in sequence) in the database rather than row with id=12.

The obvious problem with this is when we want to add an exercise to table lets say after exercise 5 then we add it manually after 5, AND THEN have to manually update the ids for all the following exercises so that they are in sequence. Ideally, we should just add a row in the table and somehow explain the php code know that this exercise should appear after exercise 5.

Similarly, for example, if we delete an exercise at number 17, then from url xxx/exercise/17 the content wont be served. Ideally, id=18 is now at spot 17 in sequence, so it has to be served that content.

attempts

I have thought of adding another column sequence which will tell in what sequence to serve. But then again I am back to square 1, because I'll have to update the sequence numbers upon add/delete.

I have also thought of creating another table and storing id numbers in whatever sequence we want to serve the content. - But I am not sure if this is actually a good idea.


Can anyone please guide me with suitable database / table design for this problem?

EDIT

I think I haven't been clear in my question.
I do not want to refill / reorder the keys. I am aware thats not how DBs should be designed.
BUT The problem is If I add a content at the end of the table then how can I tell table/php that I want to serve the newly added content (with id lets say 127) after id=15 rather than serving it last.

Was it helpful?

Solution

Regarding, "I have also thought of creating another table and storing id numbers in whatever sequence we want to serve the content. - But I am not sure if this is actually a good idea."

It's not a good idea, it's a wonderful idea. Here is a skelton design. It ain't perfect, but it will get you started.

Table Exercise - ExerciseID, NameOfExercise, MeasuredIn, other fields you may want, . Sample values of NameOfExercise are push-up, measure in repetitions, and RunningOnTheSpot, measured in seconds.

Table Routine - RoutineId, NameOfRoutine, other fields you might want. Sample values are, Jane Fonda's Routine, Navy Seal Routine, and Old Fart's Routine.

Finally, table ExerciseRoutine. This is a many to may relationship. An exercise can be in more than routine and a routine can have more than one exercise. Fields would be, ExerciseID, RoutineID, Sequence, MeasuredInMultipler, and other fields you might want.

Here is some sample data.

ExerciseId, NameOfExercise,       MeasuredIn
1           Push Ups              repetitions
2           Running on the Spot   seconds
3           Jumping Jacks         repetitions

RoutineId, NameOfRoutine
1          Jane Fonda
2          Navy Seal

and finally

 RoutineID, ExerciseID, Sequence, MeasuredInMultipler
 1          2           1         60
 1          3           2         10
 2          1           1         500
 2          3           2         100

So the Jane Fonda routine is running on the spot for 60 seconds followed by 10 Jumping Jacks. Meanwhile, the Navy Seal Routine is 500 Push Ups followed by 100 Jumping Jacks

This approach allows you to have many routines, all independent of each other.

OTHER TIPS

How about leaving the current ids as they are, let them remain as the ids and work in your URIs. Make a new column for sorting on. Instead of incrementing these values by 1 you can increment by 5, 10 or a hundred for each consecutive row. This allows you to insert new rows inbetween existing ones without modifying the previous data.

You could add a sort column and then on insert or delete execute an additional update statement to correct the sort column. If you really wanted to you could look at MySQL triggers to perform the update

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top