Monday, January 31, 2011

Advanced Relational Database /* Inner Join between two tables */

The JOIN keyword is used in an SQL statement to query data from two or more tables, based on a relationship between certain columns in these tables.Tables in a database are often related to each other with keys.A primary key is a column (or a combination of columns) with a unique value for each row. Each primary key value must be unique within the table. The purpose is to bind data together, across tables, without repeating all of the data in every table.
--McIntyre Inner Join Pop Tarts
use master;  
 create table #PopTartsStore  
      (  
      StoreId int primary key identity (1,1)  
      , Flavor varchar (20)  
      , Price dec(6,2)  
      )  
 insert into #PopTartsStore  
 values  
 ('Apple Cinnamon', 9999.99)  
 insert into #PopTartsStore  
 values  
 ('Brown Sugar', 9999.99)  
 insert into #PopTartsStore  
 values  
 ('Strawberry', 9999.99)  
 create table #PopTartsDistributor  
      (  
      DID int primary key identity (1,1)  
      , StoreId int  
      , Distributors varchar(50)  
      )  
 insert into #PopTartsDistributor  
 values  
 (1, 'Walgreens')  
 insert into #PopTartsDistributor  
 values  
 (2, 'Walmart')  
 insert into #PopTartsDistributor  
 values  
 (3, 'Wally''s')  
 --st = #PopTartsStore  
 --di = #PopTartsDistributor  
 SELECT st.StoreId, st.Flavor, st.Price, di.Distributors  
 From #PopTartsStore as st  
      Inner Join #PopTartsDistributor as di  
      on st.StoreId = di.StoreId  
select * from #PopTartsStore
select * from #PopTartsDistributor

No comments:

Post a Comment

Classes

Programming II

Advanced Relational Database

Followers