r/DatabaseHelp • u/PhatKiwi • Jan 22 '16
Please give me feedback on this new DB
I am building a database for a christmas tree growing operation. I have put together, what I believe to be, a workable schema. I am hoping to get some feedback from someone, and I have no one. You are my only hope.
So, there are 3 growing plots, we will call them Orchards. Each Orchard has rows & columns, and each row/column intersection can have zero or one trees, planted in it. The rows/columns are numbers and letters, so row 3, column f, etc. Each row/column intersection has a status (empty, in use). A tree can be different species (denoted by manually created GID {Genetic ID}), modified (have a different species grafted on), or moved to a different location. So a plant can have one or many locations, and a location can contain, through history, one or many trees, but only one at a time.
Here is a diagram I put together (sorry I couldn't figure out how to insert a pic. Please let me know changes, what I missed, etc. Thank you for your time.
You can see the diagram here: http://stackoverflow.com/questions/34952577/requesting-feedback-on-database-design/34952841?noredirect=1#comment57637809_34952841
ORCHARD OrchardId - PK
OrchardName
LOCATION LocationId -PK
Row
Column
StatusId - FK (references LocationStatus)
OrchardId - FK (references Orchard)
TREE
TreeId - PK
GID
GraftYear
PlantedYear
MovedYear
TREELOCATION
TreeLocationId - PK
TreeId - FK (references Tree)
LocationId - FK (references Location)
LOCATIONSTATUS
StatusId - PK
Description
1
u/muchargh Jan 23 '16 edited Jan 25 '16
First of all: You don't have any primary keys. You might think you do, but you have row identifiers - nothing more. So let's just get rid of anything marked "PK" for the moment.
We need real primary keys so we don't do things like:
So, let's start with the basics. Our subject is Orchards, so let's begin there. We'd like to think orchards have unique names, but a quick search would show us that's not the case. So we'll need something a little better to uniquely identify a tree farm. Physical address sounds like a natural choice.
Those farms have places to grow trees. I'd imagine most of the time rows and columns are used, but you might have several growing fields at a given address - I.e. the "lower 40" or some such folksiness. Field is a clunky name when you're trying to mix computers and the real world, so let's call them Parcels. Someone would have to be really stupid to have "Parcel A" twice on the same farm, so let's say the Orchard and the name uniquely define a Parcel.
Now, it seems natural to have rows and columns on a parcel. We could get fancy and try to define start and end rows for a column, but at this point let's just trust someone to enter the data correctly. Now, one farm might use numbers, the other letters, and someone else colors. So we should probably make it easy to keep the rows and columns in an order than makes sense while displaying names the users will be familiar with.
Now we need to define actual row/column combinations where trees can be planted - "Location" isn't quite specific enough, so let's use Plot - a combination of a row and column on a parcel.
So this is where we end up so far: http://imgur.com/RXewJVj
We need some way to avoid having "Column A" or "Row IX" twice on the same plot, so we'll need an additional key on each to uniquely define a Row/Column Name for a parcel.
Also, you'll notice that having the physical address for the primary key is a bit clunky. So we'll demote that to an Alternate Key and replace it with "OrchardCode". That can be a number, but I'd recommend making it something closer to a shortened name of the farm.
This gets us to here: https://imgur.com/xU7HAz8
We're halfway there. Let's talk about trees, and not trees.
Trees, like all living things, are already organized in an agreed upon manner. Now, we're dealing with one type of organism here so we don't need to get too crazy, but we probably do want to recognize the species, subspecies, and cultivar - and we want to do it in a manner that is accepted.
So here's the goofy part - what defines a single tree? Well, DNA is fairly unique. But that's impractical to collect and store, and let's not think about clones.
So let's instead focus on something like a barcode or other ID that might be attached by the Orchard for inventory purposes. We'll still need to account for instances where collisions could occur, so the primary key that makes the most sense would be OrchardCode, InventoryCode (or the GID if that's what you're calling it).
Now, not every tree will be grafted and some trees may be moved more than once. When these events do occur, we probably care about more than the year they occurred - we want to track when they occurred and the necessary details.
Throwing this all together we get to: https://imgur.com/dLDrRfd
You'll notice the alternate key on Tree_Location - we want to make sure that no more than one tree can be planted in a location at a time.
So let's throw everything together:
https://imgur.com/X7fLPTIEdit: The alternate key for ParcelColumn and ParcelRow was incorrect. https://imgur.com/x7XrBIn
Because we have chosen our keys well, we can now have:
2
u/BinaryRockStar Jan 23 '16
Just like the SO post answerer says, if trees can be moved multiple times and that information is useful you'll want to capture it somewhere.
Also I wouldn't have Columns be 'A', 'B', 'C' etc. This makes no sense in the database world- what happens when you reach 26 columns? Is the 27th column 'AA'? How will these columns sort once you reach two letters. Make them integers.
1
u/[deleted] Jan 25 '16
Don't get too complicated with this design. Start with something simple, like this, which is just 5 tables. It's similar to your design but with a few changes to make it easier to use and and easier to record history of moves. I haven't made a visual model for this yet, but here is the idea:
The TREE_LOCATION table is an intersection table that contains the complete history of all the trees' locations. A new row is added with the current date in Start_Date and "Y" in the Current column, and the old row is updated with "N" in the Current column and a date in the End_Date.
Let me know if this makes sense, or else I can explain it further. Add as many more attributes as you need in the tables, of course. But this basic model is very easy to use and it meets your requirements nicely.