Solved

Join two tables in sql server 2008

Posted on 2011-03-08
8
347 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
 
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
Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

 
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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

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 I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

920 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now