Hi All,
I'm fairly competent at creating SQL queries but am working on some higher-level structuring of my queries so that they can be "built" by an algorithm based on a standard. I hope I can express this in a way that will elicit some links to tutorials or articles that would be assisting.
MOST queries involve some sort of root object stored in a table. Invoices, for example. One table may have the client name in the invoice, but obviously a better table would have the client name normalized out to another table.
furthermore, there may be other fields which I wish to add by joining other tables. Most of those involve something like this
SELECT
{root fields},
tableN.LabelField (like ClientName, etc.)
FROM rootTable, tableN
WHERE /*add this constraint: */ rootTable.ForeignkeyID = tableN.ID
However I've evolved to structuring that query like this:
SELECT
{root fields},
tableN.LabelField
FROM rootTable
LEFT JOIN tableN ON rootTable.ForeignkeyID = tableN.ID
Why you may ask?
1. Because this method seems functionally equivalent but I don't know if it's slower
2. This method only adds length to the FROM clause and doesn't encumber the WHERE clause
3. In the case that the rootTable doesn't have a dependent record in the child table, this method will produce a NULL for the field for that record, vs. making the record disappear. I figure if I have to babysit the disappearance of childless records that should have a child, my code wasn't written well to begin with, so why use a query to enforce it.
What i am visualizing a query build as is the following:
1. the root table is always identifyable (if not my convention here won't work - but most objects will)
2. each LEFT joined table is a branch
3. each branch may have more than one table to "get to" the data i need as:
SELECT
{rootFields},
{fields in table c}
FROM rootTable
LEFT JOIN table1 ON rootTable.ForeignkeyID=table1.ID
LEFT JOIN table2 ON table1.ForeignkeyID=table2.ID
- therefore, table1 can be aliased reliably as r1b1t1 (root table 1, branch one, table 1) and table2 can be aliased as b1t2 (root table 1, branch one, table2)
- if I need to join another table through an inner join, as
SELECT
{rootFields},
{fields in table c},
{fields in table n}
FROM rootTable
LEFT JOIN table1 ON rootTable.ForeignkeyID=table1.ID
LEFT JOIN table2 ON table1.ForeignkeyID=table2.ID,
secondaryTable
{possible LEFT JOINs here}
WHERE rootTable.somefield=secondaryTable.somefield
..then any LEFT JOIN table tied to secondaryTable would start as r2b1t1 (root table2, branch 1, table 1), r2b1t2 etc.
One question is if there is some way to implement the structure of the last query so as to avoid impinging on the WHERE clause as I did above. But besides that I'm hoping some developers will a)see the pattern I'm seeing here and b) see if it grossly overlooks some fundamentals OR slows my code down. Remember this is designed to be a convention for an algorithm to build columns for the end user - I'm looking for a system to do that.
Thanks for your thoughtful input!