I’m trying to design a table that will help me determine if a licence has expired or not. I’d like suggestions for better ways to structure the table.
Specifically I want to do the below 3 things:
- I want to know if a licence exists
- I want to know if the licence has
expired - I want to extend the licence if it hasn’t expired
I came up with the below table. When a user’s licence is extended the first entry for that user’s licence is marked as expired.
I thought this method was good because if there was some kind of problem I still have the history to go by.
----------------------------------------------------------------------------
| user_id | licence | start | stop | expired |
----------------------------------------------------------------------------
| 22 | other | 03JUL2010 | 03JUL2012 | true |
| 55 | commercial | 03JUL2012 | 03JUL2014 | true | <= marked as expired because it was extended.
| 55 | commercial | 04JUL2012 | 03JUL2016 | false |
----------------------------------------------------------------------------
NOTE: 04JUL2012 shows the day the licence was extended.
Does this seem like a good approach? Is there anything you would change?
I would have three tables, because it seems that there is a N:M (many-to-many relationship) between users and licenses:
The tables
usersandlicensesare fairly straightforward. They store information intrinsic to users and licenses as standalone entities.In the
users_has_licensescross-reference table, rows are unique across three fields:user_id,license_id, andissued.issuedis a datetime field representing the issue date or start date for the license. Since a user can renew licenses more than once, and you require a history to be kept of the license renewals, we include this field as part of the composite primary key.You can do without an
expiredfield because you can already tell whether or not it’s expired from the data itself, and you won’t have to do routine updates to keep the data up to date. For example, to find a user’s expired licenses, you can just execute the following query:It’s a bit more hectic than you might expect, but only because you are retaining the data for past license renewals. In that case, you must do a group-wise maximum to make sure you’re getting the most recent license period. The first sub-select figures out the most recent of each of a particular user’s licenses, and then joins on the condition that the
expire_datehas already passed.If you want to get all of the user’s licenses (most recent period) whether or not they’re expired and still want to tell whether they are expired or not, just change the
INNER JOINto aLEFT JOIN, and all non-expired licenses will haveNULLvalues for the joined table.To renew a user’s license, you just need one
INSERTrather than anINSERTand anUPDATEsuch as would be the case with your current design:EDIT: Addressing the asker’s comment made to this answer:
Assuming you have chosen not to retain the history of past license periods, you can just do an
UPDATEto the current record:That will update
issuedto the current date and theexpire_dateto the current date + the remaining days of the old period + the regular expiry period(whatever it may be). If the past license has already expired, then the remaining days will be negative, in which case we would just add the regular expiry period.As a sidenote in regards to the above schema I posted to the original question, you no longer need to include
issuedas part of the primary key.