Solved

Too many ID's or not really??

Posted on 2012-03-16
5
324 Views
Last Modified: 2012-03-17
I have a small database that I have normalized to about 3NF, but am finding about half of the tables have mainly foreign key ID's, and the actual data are stored in the other half of the tables. Part of the structure is hierarchical.

I am finding that I need to do JOINS for almost every type of query. Is it okay to have to do as many joins as necessary? Here is an example of the types of JOINS I am talking about:

SELECT table1.table1ID, table2.columnName1, table2.columnName2,
          table3.table3ID, table4.table4ID, table5.table5ID,
          table6.columnName1, table7.columnName1
FROM table1
INNER JOIN table2
    ON table1.columnName1ID = table2.columnName1ID
INNER JOIN table3
    ON table1.table3ID = table3.table3ID
INNER JOIN table4
    ON table3.table4ID = table4.table4ID
INNER JOIN table5
    ON table4.table5ID = table5.table5ID
INNER JOIN table6
    ON table5.table6ID = table6.table6ID
INNER JOIN table7
    ON table6.table7ID = table7.table7ID
INNER JOIN table8
    ON table7.table8ID = table8.table8ID
INNER JOIN table9
    ON table8.table9ID = table9.table9ID

I also find that I am finding it hard to test whether the data is correct since I have to follow the ID's to their actual data. I have made great success, but am also wondering whether there is an efficient way of dealing with all these ID's?
0
Comment
Question by:mitdaniels
  • 3
  • 2
5 Comments
 
LVL 42

Accepted Solution

by:
dqmq earned 250 total points
ID: 37732019
I think you mean that most of the tables have "surrogate key" id's.  And you've stumbled onto a rather controversial topic amongst database professionals.

I cannot speak with any certainty about your design, but in general I can tell you that there is nothing wrong with so many joins.  It's a normal side-effect (some say a disadvantage) of using surrogate keys.  

A common mistake is to use surrogate keys instead of natural keys.  That's wrong...surrogate keys should be declared in addition to natural keys.  That's important because natural keys carry important business meaning which you subject to risk if you do not declare them.

On the other hand, if you do declare natural keys in addition to the surrogate keys you have the option of propagating either one to the child tables and reducing the number of joins in many instances. It's a win-win for everybody.
0
 
LVL 42

Assisted Solution

by:dqmq
dqmq earned 250 total points
ID: 37732025
I forgot to mention one practice that may help you navigate the complexity of so many joins.  I'm referring to creating views to pre-join tables in commonly used combinations and return data from multiple tables in what looks and acts like a single table.

For example, you might have an "Employee" view that combines several different tables of related employee information.  Then, "Employee" encapsulates all the joins and can be used without any awareness of them.
0
 

Author Comment

by:mitdaniels
ID: 37732036
Thanks for the correction (surrogate keys).

Do you have an example?
0
 
LVL 42

Assisted Solution

by:dqmq
dqmq earned 250 total points
ID: 37732070
I can make one up.   Suppose we have these tables:

tblEmployee
---------------
EmpID (Surrogate Key)
EmpNumber 
EmpName

tblTask
--------------
TaskID (Surrogate Key)
TaskName

tblEmployeeTask
--------------------
ID (surrogate key) 
EmpID (FK)
TaskID (FK)

Then to return all tasks with assigned employee numbers:

Select TaskName, EmpNumber from  tblTask T
   inner join tblEmployeeTask ET on ET.TaskID = T.TaskID
   inner join tblEmployee E on E.EmpID = ET.EmpID  

note: two joins required

Open in new window


Now declare the same tables using natural keys:

tblEmployee
---------------
EmpNumber (Natural Key)
EmpName

tblTask
--------------
TaskName (Natural Key)

tblEmployeeTask
--------------------
EmpNumber (Natural Key, FK)
TaskName (Natural Key, FK)


Then to return all tasks with assigned employee numbers:

Select TaskName, EmpNumber from tblEmployeeTask

note: no joins required

Open in new window

0
 

Author Closing Comment

by:mitdaniels
ID: 37732124
Thanks so much, you've given me a few good pointers. I will see how to apply those to my database.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Azure Functions is a solution for easily running small pieces of code, or "functions," in the cloud. This article shows how to create one of these functions to write directly to Azure Table Storage.
This article shows the steps required to install WordPress on Azure. Web Apps, Mobile Apps, API Apps, or Functions, in Azure all these run in an App Service plan. WordPress is no exception and requires an App Service Plan and Database to install
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…

756 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question