Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. Database & SysAdmin
  3. Database
  4. T-SQL - Select with relationships

T-SQL - Select with relationships

Scheduled Pinned Locked Moved Database
databasequestion
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • realJSOPR Offline
    realJSOPR Offline
    realJSOP
    wrote on last edited by
    #1

    I have two tables with a relationship set on a given column (the related column names are the same in each table, and both tables have that column set as the primary key). Do I still have to do a join to get data from both tables, or does the configured relationship magically enable it? Here's what I have so far (I actually list the individual columns I want to retrieve, but for the sake of brevity, I'm using "SELECT *". Also, The ID column is the primary key and the column on which the tables are related.

    SELECT *
    FROM TableA a, TableB b
    WHERE a.ID = b.ID

    .45 ACP - because shooting twice is just silly
    -----
    "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
    -----
    "The staggering layers of obscenity in your statement make it a work of art on so many levels." - J. Jystad, 2001

    A I 2 Replies Last reply
    0
    • realJSOPR realJSOP

      I have two tables with a relationship set on a given column (the related column names are the same in each table, and both tables have that column set as the primary key). Do I still have to do a join to get data from both tables, or does the configured relationship magically enable it? Here's what I have so far (I actually list the individual columns I want to retrieve, but for the sake of brevity, I'm using "SELECT *". Also, The ID column is the primary key and the column on which the tables are related.

      SELECT *
      FROM TableA a, TableB b
      WHERE a.ID = b.ID

      .45 ACP - because shooting twice is just silly
      -----
      "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
      -----
      "The staggering layers of obscenity in your statement make it a work of art on so many levels." - J. Jystad, 2001

      A Offline
      A Offline
      amer shammout
      wrote on last edited by
      #2

      i want to tell you that is the relationship ypu set is not relationship but it is a constraint but the where statment "WHERE a.ID = b.ID" make the relationship or join statment

      1 Reply Last reply
      0
      • realJSOPR realJSOP

        I have two tables with a relationship set on a given column (the related column names are the same in each table, and both tables have that column set as the primary key). Do I still have to do a join to get data from both tables, or does the configured relationship magically enable it? Here's what I have so far (I actually list the individual columns I want to retrieve, but for the sake of brevity, I'm using "SELECT *". Also, The ID column is the primary key and the column on which the tables are related.

        SELECT *
        FROM TableA a, TableB b
        WHERE a.ID = b.ID

        .45 ACP - because shooting twice is just silly
        -----
        "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
        -----
        "The staggering layers of obscenity in your statement make it a work of art on so many levels." - J. Jystad, 2001

        I Offline
        I Offline
        i j russell
        wrote on last edited by
        #3

        Yes you still have to do a join if you want data from both tables. A Foreign Key constraint is there to only allow non-null values that are in the parent column. The FK plays no part in the query; If you were to remove it, the query would still work just as well. By the way, your chosen style of joins is not considered to be good practice and may well be deprecated in future versions of Sql Server.

        SELECT *
        FROM TableA a
        JOIN TableB b
        ON b.ID = a.ID

        The primary reason for this is that if you forget/remove the WHERE clause you have a CROSS JOIN. This can't happen with the suggested approach. Ian

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups