Solved

Join two tables in sql server 2008

Posted on 2011-03-08
8
345 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
LVL 15

Assisted Solution

by:derekkromm
derekkromm earned 425 total points
Comment Utility
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
Comment Utility
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
Comment Utility
Further explanation - SUPPLIER is the foreign table name and SID is the primary key in SUPPLIER table
0
 

Author Comment

by:Bob Schneider
Comment Utility
Awesome.  Thanks!
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Suggested Solutions

SQL Server engine let you use a Windows account or a SQL Server account to connect to a SQL Server instance. This can be configured immediatly during the SQL Server installation or after in the Server Authentication section in the Server properties …
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

743 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

13 Experts available now in Live!

Get 1:1 Help Now