Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I think part of the difficulty you're having (especially with recursive counting - if you're trying to do something in SQL on a row-by-row basis, that's a good indicator that you might want to look at the way you've built things) is related to how your database is structured.</p> <p>What you're attempting to do is build a classic resource allocation tracker - if you have 10 licenses for Office 2007, you want to keep track of which computers are registered to use those licenses. Having more active software registrations than you have licenses is a recipe for trouble.</p> <p>Consider the following facts about the problem: You have X licenses and Y installed registrations that you want to keep track of.</p> <p>A <em>license</em> is a type of thing which has certain properties:</p> <ul> <li>It is for a specific software version, like 'Office 2007' or 'Windows 7' or 'Office 2010' or... etc. </li> <li>It allows you to run a specific number of instances of the specified software. </li> <li>In your case, certain kinds of licenses are allowed to substitute for other licenses - for example, Office 2010 licenses can be used for Office 2007 installations.</li> </ul> <p>A <em>registration</em> is, again, a type of thing with certain properties:</p> <ul> <li>It shows the usage of a software license when performing an installation</li> <li>It shows that the install is on a specific machine.</li> </ul> <p>With these facts in mind, I constructed a set of tables:</p> <pre><code>Licenses: License_ID integer identity(1,1) Package_Name varchar( 200 ) Display_Name varchar( 400 ) Bought integer </code></pre> <p>The above table just holds the information about each license type.</p> <pre><code>License_Substitutes: Substitute_ID integer identity(1,1) License_ID integer Allowed_Sub_License integer </code></pre> <p>This table contains information about relationships between different licences; in this case, each row in this table says 'this License_ID is allowed to substitute for the specified Allowed_Sub_License'.</p> <pre><code>Registrations: Registration_ID integer identity(1,1) Installed_License integer Substitute_License integer Description varchar(80) </code></pre> <p>If you have other information in your registry entry that I'm not aware of (like who the registration is for, where or what computers, etc.) columns in this table would be the best way to store that data.</p> <p>Let's take the two licenses you mentioned (Office 2007 and Office 2010) and put them into the Licenses table:</p> <pre><code>License_ID Package_Name Display_Name Bought 1 Off2007 Microsoft Office 2007 1 2 Off2010 Microsoft Office 2010 1 </code></pre> <p>We've just bought one copy of each, nothing special. Given your rules, we also know that a 2010 license can substitute for a 2007 license, so License_Substitutes looks like:</p> <pre><code>Substitute_ID License_ID Allowed_Sub_License 1 2 1 </code></pre> <p>Once the licenses have been entered and the substitution relationships have been determined, you can register software using those licenses. If you were to register a copy of 2007 on my computer, Registrations would look like:</p> <pre><code>Registration_ID Installed_License Substitute_License Description 1 1 NULL Mikurski's computer </code></pre> <p>Substitute_License is left NULL as that registration does not need a substitute.</p> <p>However, let's say that you now install 2007 on your computer. Registrations would look like:</p> <pre><code>Registration_ID Installed_License Substitute_License Description 1 1 NULL Mikurski's computer 1 1 2 kampi's computer </code></pre> <p>Substitute_License here is recorded as 2 (for Office 2010). License_Substitutes.Substitute_ID is just an identity value to keep rows distinct.</p> <p>You'll notice that 'Free' is not here as a column anymore. This is because the number of free spots for a license can be calculated in a query:</p> <pre><code>select count(*) from Registrations where (installed_license = @Your_License_ID and substitute_license is NULL) or substitute_license = @Your_License_ID </code></pre> <p>Normalizing the database like this (for more information about normalization, I've found <a href="http://www.phlonx.com/resources/nf3/" rel="nofollow">this</a> to be a good introduction) helps minimize the amount of upkeep work you have to put into maintaining data integrity, and means the database will be easier to extend if your needs change in the future.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload