+ Reply to Thread
Results 1 to 6 of 6

Thread: Relational table methods

  1. #1
    Drunk Barn Owl dr_rock will become famous soon enough dr_rock's Avatar
    Join Date
    Jun 2008
    Location
    Melbourne, Australia
    Posts
    180
    Rep Power
    4

    Relational table methods

    Hi guys, just curious about how you relate tables in SQL. I have just upgraded a very flat DB design to be relational. I use 3 tables for a relation in this system.

    Lets say we have a selection of shops and a selection of fruit, we want to link fruit to the shops you can buy the fruit, I would do something like:

    Table 1
    index = shop_id, int
    col1 = shop_name, varchar(100)

    Table 2
    index = fruit_id, int
    col1 = fruit_name, varchar(100)

    Table 3
    index = relation_id, int (optional)
    col1 = shop_id, int
    col2 = fruit_id, int

    I would then query table 3 and link table 1 and 2 using the id's

    That works great for me although if we start adding extra conditions and relations it does start to get a little messy (say varieties of apples, or isles in the shops).

    The question is, do you use this method? Are there better methods out there? mainly a curiosity thing...

  2. #2
    The Barnfather jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead's Avatar
    Join Date
    Mar 2008
    Location
    Reston, VA
    Posts
    4,547
    Blog Entries
    9
    Real Name
    Jason
    Rep Power
    22

    Define: "messy"

    Your design looks correct, but let me give you an example:

    ContactPersons
    PersonID int, identity
    FirstName varchar(25)
    LastName varchar(30)

    Addresses
    AddressID int, identity
    Street1 varchar(100)
    Street2 varchar(100)
    City varchar(50)
    ZipCode int
    Country char(2)
    AddressTypeID int relationship with AddressTypes
    PersonID int relationship with ContactPersons

    AddressTypes
    AddressTypeID int identity
    AddressType varchar(50)

    This database is well normalized. Now it seems your concern is with the actual query. For designs like this you have to perform many joins (depending on the number of related tables). Am I correct in assuming this?

    To answer what I think your question is: yes, I use this method and yes, there are other methods. However, I find this method to be most efficient. If you write your SQL in a neat way, it will appear to be less "messy":

    Code:
    SELECT
         p.[FirstName],
         p.[LastName],
         a.[Street1],
         a.[Street2],
         a.[City],
         a.[ZipCode],
         a.[Country],
         t.[AddressType]
    FROM ContactPersons p LEFT JOIN Addresses a 
    ON p.[PersonID] = a.[PersonID] INNER JOIN AddressTypes t
    ON a.[AddressTypeID] = t.[AddressTypeID]
    WHERE p.[PersonID] = @PersonID
    
    We could add many more joins to this and it would still look pretty neat.
    jmurrayhead
    If you agree, give me rep.
    If you like it here...throw us a few bones to help support us.


  3. #3
    Drunk Barn Owl dr_rock will become famous soon enough dr_rock's Avatar
    Join Date
    Jun 2008
    Location
    Melbourne, Australia
    Posts
    180
    Rep Power
    4

    ahh sweet, see I never actually studied relational table I just adopted that method when presented with a problem and have never checked if it is the best way to do it.

    I meant messy when trying to nut it out in your head, no matter how neat you write it anything with multiple joins makes for complicated logic, but thats what makes our jobs interesting right?

    I lay out my SQL code very similar to you, its good to confirm that im doing things right.

  4. #4
    Wolfmaster Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy's Avatar
    Join Date
    Mar 2008
    Location
    Peoria, IL
    Posts
    2,386
    Blog Entries
    5
    Real Name
    Wolff
    Rep Power
    15

    Actually, if you lay out your tables and defines the relationships well, the JOIN syntax starts to come naturally. Sure, like everything else, it takes a little practice, but they will soon come rolling off your fingers like everything else.

    One way to de-messy the query is to create a View that has the JOIN logic. It's then a simple matter to query using the View. If you find yourself writing the same JOIN query over and over, then consider making it into a VIEW (or Saved Query in Access)
    Wolffy
    .-- ----- ..-. ..-. -.--
    Opinions expressed are my own and do not necessity reflect those of any sane person. Any code provided is intended to be an example and is provided AS IS. Void where prohibited by law. Not valid in California. Your mileage may vary.

  5. #5
    The Barnfather jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead's Avatar
    Join Date
    Mar 2008
    Location
    Reston, VA
    Posts
    4,547
    Blog Entries
    9
    Real Name
    Jason
    Rep Power
    22

    Quote Originally Posted by dr_rock View Post
    ahh sweet, see I never actually studied relational table I just adopted that method when presented with a problem and have never checked if it is the best way to do it.
    lol I began studying it way back when I noticed how redundant the data in my tables were. I noticed how others were designing their databases and looked further into the subject. That's when I found how much more efficient it was and how it prevented major problems further down the road.

    Quote Originally Posted by dr_rock View Post
    I meant messy when trying to nut it out in your head, no matter how neat you write it anything with multiple joins makes for complicated logic, but thats what makes our jobs interesting right?
    That's true. When you're doing the logic in your head it does get a bit confusing....which is why I type it out in Query Analyzer so I can keep track of myself

    Quote Originally Posted by dr_rock View Post
    I lay out my SQL code very similar to you, its good to confirm that im doing things right.
    Indeed, it always feels better to have some reassurrance
    jmurrayhead
    If you agree, give me rep.
    If you like it here...throw us a few bones to help support us.


  6. #6
    Moderator don94403 is a jewel in the rough don94403 is a jewel in the rough don94403 is a jewel in the rough don94403's Avatar
    Join Date
    Mar 2008
    Location
    San Mateo, CA, USA
    Posts
    313
    Blog Entries
    8
    Real Name
    Don Ravey
    Rep Power
    6

    I agree with the previous responders, but thought I would add the following, since you are asking a pretty general question.

    Prior to about 1970, many programmers were asking questions about whether a particular "method" was a good one. Than along came Dr. E. F. Codd, a mathematician at IBM, who developed the mathematical set theory of relational databases. Since then, it really is not a matter of rating one method against another. There is one fundamental way to design relational databases so that they will be guaranteed to work with SQL. Now that I've stated that so categorically, it's only fair to admit that practical considerations often drive experienced developers to modify the "theoretical" design under certain conditions. But that should be done only after a fully normalized structure has been evaluated and understood.

    The way to think of a new database is to determine the scope of the data model, then specify the entities that you will represent, then their relationships and their attributes. If you always approach database design in this way, your task will be much easier and you will make intelligent decisions. Defining scope and entities can be a little "fuzzy" at times, but it forces you to consider all the ill-defined issues at the beginning of your project, not after you've invested a lot of effort into a design that turns out to be weak or even completely unworkable.

    I recommend that one of the best investments of your time would be to read some tutorials on database normalization. There are lots of them online and in books.

+ Reply to Thread

Similar Threads

  1. Replies: 4
    Last Post: April 5th, 2010, 12:15 AM
  2. Create History Table / Outcome results
    By Rebelle in forum Database Design Help
    Replies: 11
    Last Post: June 10th, 2009, 11:54 AM
  3. How to retrieve unknown recordset field names in table?
    By BLaaaaaaaaaarche in forum ASP Development
    Replies: 2
    Last Post: August 10th, 2008, 09:33 AM
  4. Returning a table in sp executed in an sp...
    By Lauramc in forum SQL Development
    Replies: 1
    Last Post: August 6th, 2008, 10:05 PM
  5. Relational Database Normalization Basics
    By AOG123 in forum Database Design Help
    Replies: 0
    Last Post: March 31st, 2008, 09:01 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

SEO by vBSEO