Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8200047
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T06:18:03+00:00 2026-06-07T06:18:03+00:00

I’m trying to design a table that will help me determine if a licence

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T06:18:05+00:00Added an answer on June 7, 2026 at 6:18 am

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

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

    The tables users and licenses are fairly straightforward. They store information intrinsic to users and licenses as standalone entities.

    In the users_has_licenses cross-reference table, rows are unique across three fields: user_id, license_id, and 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
        a.license_id
    FROM
        (
            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
    INNER JOIN
        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 to a 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 rather than an INSERT and 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 = (
            SELECT 
                NOW() 
                + INTERVAL CASE WHEN DATEDIFF(expire_date, NOW()) < 0 THEN 0 ELSE DATEDIFF(expire_date, NOW()) END DAY 
                + INTERVAL 4 YEAR 
            FROM 
                users_has_licenses 
            WHERE 
                user_id = a.user_id AND 
                license_id = a.license_id
        )
    WHERE
        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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've got a string that has curly quotes in it. I'd like to replace
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I need a function that will clean a strings' special characters. I do NOT
Basically, what I'm trying to create is a page of div tags, each has
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am trying to render a haml file in a javascript response like so:
I'm trying to create an if statement in PHP that prevents a single post
I have two tables with like below codes: Table: Accounts id | username |
I am trying to understand how to use SyndicationItem to display feed which is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.