¿Cómo estructurar una tabla para detectar licencias vencidas en mysql?

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    | 

NOTA: 04JUL2012 shows the day the licence was extended.

Does this seem like a good approach? Is there anything you would change?

preguntado el 04 de julio de 12 a las 06:07

2 Respuestas

I would have three tables, because it seems that there is a N:M (many-to-many relationship) between users and licenses:

user_id [PK]

user_id [PK] (references users.user_id)
license_id [PK] (references licenses.license_id)
issued [PK]

license_id [PK]

Las mesas users y licenses are fairly straightforward. They store information intrinsic to users and licenses as standalone entities.

En la users_has_licenses cross-reference table, rows are unique across three fields: user_id, license_id y issued. issued is 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 expired field 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:

        SELECT user_id, license_id, MAX(issued) AS issued
        FROM users_has_licenses
        WHERE user_id = <user_id here>
        GROUP BY user_id, license_id
    ) a
    users_has_licenses b ON
        a.user_id = b.user_id AND
        a.license_id = b.license_id AND
        a.issued = b.issued AND
        b.expire_date < NOW()   

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_date has 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 JOIN a una LEFT JOIN, and all non-expired licenses will have NULL values for the joined table.

To renew a user's license, you just need one INSERT en lugar de un INSERT y an UPDATE such as would be the case with your current design:

INSERT INTO users_has_licenses VALUES (<user_id here>, <license_id here>, NOW(), NOW() + INTERVAL 4 YEAR)

EDIT: Addressing the asker's comment made to this answer:

I have one more question in regards to renewing the licence. If the user renewed their licence 6months into their first licence, how would you include that extra time in the INSERT INTO statement you listed above? I think the last value would need to be now() +remaining time + expiry period (or something like that). Unless I've misunderstood something. I'm just not sure how to get the remaining time.

Assuming you have chosen not to retain the history of past license periods, you can just do an UPDATE to the current record:

UPDATE users_has_licenses a SET
    a.issued = NOW(),
    a.expire_date = (
            + INTERVAL CASE WHEN DATEDIFF(expire_date, NOW()) < 0 THEN 0 ELSE DATEDIFF(expire_date, NOW()) END DAY 
            + INTERVAL 4 YEAR 
            user_id = a.user_id AND 
            license_id = a.license_id
    a.user_id = <user_id here> AND
    a.license_id = <license_id here>

That will update issued to the current date and the expire_date to 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 issued as part of the primary key.

Respondido 04 Jul 12, 09:07

Thanks for the detailed answer! Normally would you recommend keeping only the most recent licences? It seems I might be unnessarily complicating things by keeping the history. - Esforzarse más

@moomoochoo That's purely for you to decide based on the requirements of your application. Do you necesitas to display a history of past license periods in your application? If not, you would benefit from the advantage of having a leaner data-set in the table, but you would not have access to the history of past periods. - Zane Bien

Good point. I don't need to display the past licence periods, but it might prove useful anyway. - Esforzarse más

If you are unsure: what you could do is archive past license periods in a separate table, where the table will only be accessed when there is a special request for the history of periods. - Zane Bien

I like that idea. I will give it a go. Thanks! - Esforzarse más

you are storing the same record twice. Instead you can go for a better design


user-id  |  license  |  start | stop | expired | extended


prim_key | user_id | extended_date | extended_date_expiry

In table1 extended column is a boolean value true or false.

If extended is true then you can search for the date the user has extended to in table2.

In table2 you can store multiple extended dates for same user and get the history also. The highest date to which it is extended would be the date of validity of that user_id.

Respondido 04 Jul 12, 06:07

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.