0

[Note: start]

Hopefully, there are some of you, professionals, that have already had to cope with this kind of situation. The particular case concerning post office is fictional and was made just to present the problem. This question is not about improving performance by adding indexes.

[Note: end]

Recently I've been wondering how to efficiently(!) create a database structure to handle volatile hierarchy levels. Let's get into the example for a better understanding of the matter.

Suppose we have a post office that stores physical mails in different ways depending on really any factor (that doesn't matter here). We are to map this situation onto a database model.

Ok, so we have those mails. Mails can be physically stored in cases, boxes, drawers, safe deposits and many many more (because we don't want to strict our storage types and structure, but instead allow it to be flexible to meet our future changes). This means that for now we have such types, and for instance one mail can be inside a box that is stored in a safe deposit, but there can be really any combination.

For simplicity of this case let's assume that a single mail is the lowest granularity we can get. We have to remember, though, that we may also have for example empty box (that has no objects both beneath and above it) and we also want to store this in DB.

This model has to:

  • be efficient - it can't (or should it?) all be stored in a single table, since we process many mails per day
  • be adjustable - we may need to change our way of storing some already existing objects, so we need those grouped hierarchies to change for some objects
  • be flexible - we are definitely going to need to add new objects in the future and so the hierarchy may change for upcoming and already existing particular objects

[My idea] So far I've come up with this idea:

Let's store all objects in one self-referencing "hierarchy" table and mark each object to be of some type, so that recursively we can see the path of where the email is located, or show every object with hierarchy that on the top depends on safe deposit. This method would require :

a) every record to be detailed in this table, which makes it contain many null values, since mails are not described by the same attributes as a drawer,

b) or each object type might have its own table describing it, with a foreign key in "hierarchy table"

[Warning] This table can grow so big that lookup may cause serious performance issues. This would also require us to add new structure (physical table) for every new object in our "hierarchy of storage units", which I think is fine.

[Question] Could you please let me know if my idea (consider B to be my chosen requirement) is the best I can get? What can I improve?

[SAMPLE DATA]

Emails Table:

id
---
1
2

Cases Table:

id
---
1
2

Boxes Table:

id
---
1

Hierarchy-relation Table:

seq | id_obj | obj_type | id_parent_obj | parent_obj_type |
#1  |  1     | Email    |  1            | Case            | -- email 1 in case 1
#2  |  1     | Case     |  1            | Box             | -- case 1 in box 1
#3  |  1     | Box      |  [null]       | [null]          | -- box 1 no parent
#4  |  2     | Email    |  [null]       | [null]          | -- email 2 no parent
#5  |  3     | Email    |  1            | Box             | -- email 3 in box 1
#6  |  1     | Box      |  [null]       | [null]          | -- box 1 no parent

Just by the look on this sample data I see that we have some redundant information for example about the box in Hierarchy-relation table in seq #3 and #6. I think there is some different approach to this, also the refferential should be kept on seq, I think. We can see though, that case 2 is empty.

5
  • So first idea: Don't do that! Second idea: Why are you trying to reinvent XML? Third idea: Please poste some data you might want to store there and explain, why the common approaches do not fit to your needs...
    – Shnugo
    Oct 28, 2015 at 9:46
  • @Shnugo thank you for your comment. I'm in need of making such model using DBMS, best if it could be a relational one. What actually are the common approaches? Oct 28, 2015 at 9:52
  • Well, this question is hard on the "to be closed because it's to broad", but I'll try to give an answer...
    – Shnugo
    Oct 28, 2015 at 10:01
  • @Shnugo I'm attaching sample data. Oct 28, 2015 at 10:02
  • Your update makes things clearer. Yes, exactly what you found yourself (redundant information) is to be normalized. The common way are isolated entities with Foreign Keys and/or mapping tables
    – Shnugo
    Oct 28, 2015 at 10:22

2 Answers 2

1

You should define your storage types in one "normal" table.

Add all information to this table, which is bound to the storage itself. But nothing about the things stored in there.

If storages can be "nested" (e.g. boxes within bigger boxes within a ...) you could either add a "self-join" (you did not state your RDBMS, SQL Server offers HIERARCHYID for this) to specify the location as a reference to the parent storage, or you could define a Locator table where you store the IDs of a storage and the IDs of its container (1:n -> One storage can be located in exactly one container).

Than you need a table with your items to store. If you have only one kind of item ("mails") you need a table to define a "mail" with all its attributes.

You can either put the location (the place it's stored) as a foreign key to the storages right into the "mail" table (so each mail knows its location), or - again - you can define a mapping with the mail's ID and the storage ID.

The second approach is needed, when you want to add more information to the process "I store a mail" (e.g. who, when, price, ...) and you could historize this. If you change a location you just set a "ValidUntilDatetime" and add a new line with the new location. So you can follow the process of putting things around... If this was just an FK-column within your mails table, this would not be possible.

If there are more items to store, you could think about a Master-table and as many different item tables as you have different things to store. They share an ID. General information (also the location) are part of the master table, specific data part of the sub tables.

Well, I hope this gets you on the right path...

EDIT: Answer your question from your comments: ad "Why need of locator table":

Just image a table

CREATE TABLE storage(id INT, Name VARCHAR(100), ...)

And sample data like

id    Name, ...
1     Box1  ...
2     Box2  ...
3     Case1 ...
[...]

And a table like this

CREATE TABLE Mail( id INT, Creation DATETIME, From VARCHAR(100), To VARCHAR(100),LocationID INT FOREING KEY REFERENCES Storage(id),...)

And sample data like:

id    Creation          From          To            LocationID    ...
1     2015-10-28 12:00  [email protected]  [email protected]      2         ...
2     2015-10-28 12:05  [email protected]  [email protected]      2         ...
3     2015-10-28 12:10  [email protected]  [email protected]      3         ...

This would make clear, that mails 1 and 2 are physically stored in Box2 and mail 3 is stored in Case1. But you have no information about: Who put it there? When was it put there? How long will it stay there? Who will come to pick it?...

If you have a locator table like

CREATE TABLE Location(id INT, mailID INT FOREIGN KEY REFERENCES Mail(id),storageID INT FOREIGN KEY REFERENCES Storage(id), When, ...)

You can store the "put-process". If an item is transfered from Box2 to Box1 you would change the locationID in the first example. But you would not keep the information that this item was in Box2 before, who transfered it, when this happend and so on...

ad "different ways to store": If a mail is stored "no where", you might allow the storageID to remain NULL, or you define a storage item called "Nowhere" and use this ID.

It is enough to store the ID of the direct container. The container itself must know where itself is located. For this read the first part of my answer.

11
  • Could you also attach an example, to clarify the need of Locator table (I don't see use of this information in mapping tables for each "mail") and how the master-table would work with other tables? Let's assume that we have mails, cases and boxes with combinations: mail in case in box, mail in box, mail alone, mail in case? Oct 28, 2015 at 10:32
  • The trick is that each mail can be stored differently. One can be inside a case, one can be inside a box, another one can be kept loose etc. What is the structure that keeps information about this? Oct 28, 2015 at 10:36
  • 1
    @ConsiderMe, I'm curious... Could I shine some light into this? Are there still open questions?
    – Shnugo
    Oct 28, 2015 at 19:33
  • @ConsiderMe, Just saw that your are around at the moment... Look, I invested quite some time into this and would really like to see, whether I could help you or not. So please give a little feedback, Thx!
    – Shnugo
    Oct 29, 2015 at 22:29
  • 1
    @ConsiderMe, just postet another answer. Please have a look on it!
    – Shnugo
    Oct 30, 2015 at 11:03
1

Here a complete model (air code, untested...)

CREATE TABLE StorageType(ID INT IDENTITY PRIMARY KEY
                        ,StorageTypeName VARCHAR(100) NOT NULL);
INSERT INTO StorageType VALUES('Box'),('Case'),('OtherStorage');

CREATE TABLE Storage(ID INT IDENTITY PRIMARY KEY
                    ,StorageTypeID INT NOT NULL CONSTRAINT FK_Storage_StorageTypeID FOREIGN KEY REFERENCES StorageType(ID)
                    ,StorageName VARCHAR(100) NOT NULL
                    /*more columns to describe a storage*/
                    );
INSERT INTO Storage VALUES(1,'Box1'),(1,'Box2'),(3,'SomeStorage1');                  


CREATE TABLE ObjectType(ID INT IDENTITY PRIMARY KEY
                       ,ObjectTypeName VARCHAR(100) NOT NULL);
INSERT INTO ObjectType VALUES('Mail'),('OtherItem');

CREATE TABLE MyObject(ID INT IDENTITY PRIMARY KEY
                     ,MyObjectTypeID INT NOT NULL CONSTRAINT FK_MyObject_MyObjectTypeID FOREIGN KEY REFERENCES ObjectType(ID)
                     ,MyObjectName VARCHAR(100) NOT NULL
                     /*more columns to describe an object*/
                     );
INSERT INTO MyObject VALUES(1,'Mail1'),(1,'Mail2'),(2,'SomeObject1');

CREATE TABLE StorageLocation(ID INT IDENTITY PRIMARY KEY
                            ,CreatedOn DATETIME NOT NULL
                            ,OutDate DATETIME NULL
                            ,StorageID INT NOT NULL CONSTRAINT FK_StorageLocation_StorageID FOREIGN KEY REFERENCES Storage(ID) 
                            ,ContainerID INT NULL CONSTRAINT FK_StorageLocation_ContainerID FOREIGN KEY REFERENCES Storage(ID) 
                            /*more columns to describe the "put storage" process: who, when, how long, ... */
                            );

INSERT INTO StorageLocation VALUES(GETDATE(),NULL,2,1); --puts the Box2 into Box1, current, because OutDate IS NULL
                            /*put all storages in their places*/

CREATE TABLE ObjectLocation (ID INT IDENTITY PRIMARY KEY
                            ,CreatedOn DATETIME NOT NULL
                            ,OutDate DATETIME NULL
                            ,MyObjectID INT NOT NULL CONSTRAINT FK_ObjectLocation_MyObjectID FOREIGN KEY REFERENCES MyObject(ID) 
                            ,ContainerID INT NULL CONSTRAINT FK_ObjectLocation_ContainerID FOREIGN KEY REFERENCES Storage(ID) 
                            /*more columns to describe the "put object" process: who, when, how long, ... */
                            );

INSERT INTO ObjectLocation VALUES(GETDATE(),NULL,1,2); --puts the Mail1 into Box2 (which is in Box1), current, because OutDate IS NULL
                            /*put all objects in their places*/
8
  • thank you for your effort, though, we seem to misunderstand each other. For every object type I'm creating a new table. I also want to have a table which stores object ids of every types (a container for all objects) and a parent-child relation. That's all. I don't feel your distinction between storage and object as a fit to my case. An object is a storage. Oct 30, 2015 at 12:07
  • @ConsiderMe, I'd rather see this like a file system. It is quite intelligent to separaten driectories from files. A file can be of any kind, can be described in any way, can be whatever it is, but a directory is a directory. As you need fast access you should try to keep things slim where the may be slim...
    – Shnugo
    Oct 30, 2015 at 12:13
  • @ConsiderMe, and one more point: If you store different types of objects, you just need sub tables for the type sepcific data. There is the ObjectType already. General data is part of the MyObject table. specific data can be 1:1 related in a subtable with the same ID. In your code you'd modell this as object inheritance...
    – Shnugo
    Oct 30, 2015 at 12:14
  • @ConsiderMe. one more point: You will store many objects. Yo your tables will grow big, maybe huge. But the data where the storages are located will stay rather small. Just image a recursive path of a deeply nested storage within one single table with millions of rows... I see that you try to put "everything into one" to keep it flexible... It's on you of course... But flexible equals slow... I wouldn't...
    – Shnugo
    Oct 30, 2015 at 12:20
  • you may have a point with the ObjectType, though, I fear that table is going to grow too big. This approach also stricts every MyObject to a set of columns which must describe all objects. What if they differ? As to the first comment, the thing is, at start I may have a file, since I store information as a file, but physically, this file also contains files which are not evidenced in the system, until a certain process needs to appear, so that once a file now becomes a directory for files and new files are created under it. I may have given not enough information for my case. Sorry for that Oct 30, 2015 at 12:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.