T
TopSpek
- Original Poster
- #1
gpietersz
It looks to me that your code does not match your requirements because
TopSpek
A particular person cannot be leasing the same app at the same time
gpietersz
Implies you need to deal with multiple apps which your code does not.
My apologies. I did think about including this, but the example was meant to be a demonstration of how natural and surrogate keys are used at the design stage.
However, my example does show that lease periods of the same app, for a particular person, not overlapping, does address your initial misunderstanding.
I was trying to keep things as simple as possible. If you would like me to show you how an entity type 'App' would be added to the database schema, then I can do this. But for now, please find below some sample Lease tuples, with a foreign key to reference apps. (Please note that there would be a 0:m relationship from App to Lease if we developed the database to include an App entity type):
Code:
Person( PersonId )
Lease( StartDatetime, EndDatetime, PersonId, AppId )
App( AppId )
Lease
--------------+-------------+----------+------
StartDatetime | EndDatetime | PersonId | AppId
--------------+-------------+----------+------
01-03-2020 | 01-04-2020 | p1 | a1
10-03-2020 | 10-04-2020 | p1 | a2
01-04-2020 | 01-05-2020 | p1 | a1
--------------+-------------+----------+------
(In the datetime stamps, for clarity, I have omitted the 'time' component and reversed the format of the date component. You will need to line up the pipe characters because the codebox won't do it.)
We can see that a1 is leased by p1 twice but the lease time periods do not intersect. p1 also leases a2, and this time period intersects with both of p1's lease periods for a2 - which is legal because a1 and a2 are different apps.
By the way, I'm not having a go at you, but you would learn more if you did some of the work for yourself instead of me having to show you everything. I don't mind spending time with anyone who wants to learn, but a developer would at least have a crack at some of these things. If we make mistakes, then that's fine - we all learn by our mistakes.
gpietersz
It rather proves my point about how easy it is to make mistakes!
Think of the requirements I gave as being from a fictitious client. The requirement that "a particular person cannot be leasing the same app at the same time" makes perfect sense. If this constraint were not implemented, then the person could be paying for the same lease, fully or partially, twice! In other words, if a person is leasing an app, then that person should be protected from taking out another lease on the same app while the current lease is still active.
Furthermore, the constraint dictates that the lease has the natural identifier (PersonId, StartDatetime) which should be preserved in the schema. This is fundamental and axiomatic relational database theory which has been developed over decades and is now well established - like many other areas of maths.
