Solved

Join two tables in sql server 2008

Posted on 2011-03-08
8
352 Views
Last Modified: 2012-05-11
I just created a new table and I want to join it to an existing table using the Primary key in the original table as a foreign key in the new table.  In sql server 2000I did this via a diagram.  I don't have diagrams here.  How can I complete this task otherwise?

Thanks!
0
Comment
Question by:Bob Schneider
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 15

Assisted Solution

by:derekkromm
derekkromm earned 425 total points
ID: 35075988
Right click the table with the PK, click design
From the top menu, Table Designer, Relationships

Add a relationship, on the "Tables and Columns Specification", click the "..." button to select the table/columns
0
 
LVL 1

Accepted Solution

by:
lucky20 earned 25 total points
ID: 35076032
There are various types of joins.. you can find here ..
SQL Joins

sql table1:
empid(pk)
empname



sql table2:
eid(fk) from <sqltable1>
deptid(pk)
dept_name


sql st:
select t1.empid, t1.empname, t2.dept_name from sqltable1 t1,sqltable2 t2 where t1.empid=t2.empid
0
 

Author Comment

by:Bob Schneider
ID: 35076128
On the solutioin by derekkrom, when I try to create a relationship it joins the PK table to itself?
0
NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

 
LVL 23

Assisted Solution

by:OP_Zaharin
OP_Zaharin earned 50 total points
ID: 35076167
Sample:

TableA
   - Id (Primary Key)
   - Description

TableB
   - Id (Primary Key)
   - TableAId (FK to TableA)

select *
from
    TableA a
        left join
    TableB b
        on
            a.Id = b.Id
0
 
LVL 15

Assisted Solution

by:derekkromm
derekkromm earned 425 total points
ID: 35076168
Sorry, you want to do that on the table that contains the FK, not the PK. Apologies.
0
 
LVL 23

Expert Comment

by:OP_Zaharin
ID: 35076208
this is a sql statement to do the foreign key:

CREATE TABLE STOCK
(Stock_ID integer primary key,
StockIn_Date datetime,
Description char(100),
Supplier_SID integer references SUPPLIER(SID),
Amount double);
0
 
LVL 23

Assisted Solution

by:OP_Zaharin
OP_Zaharin earned 50 total points
ID: 35076230
Further explanation - SUPPLIER is the foreign table name and SID is the primary key in SUPPLIER table
0
 

Author Comment

by:Bob Schneider
ID: 35076247
Awesome.  Thanks!
0

Featured Post

The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

Question has a verified solution.

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

I would like to start this tip/trick by saying Thank You, to all who said that this could not be done, as it forced me to make sure that it could be accomplished. :) To start, I want to make sure everyone understands the importance of utilizing p…
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

856 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