-- 조인(Join) : Left 조인
--카테고리 테이블 설계
Create Table dbo.Categories (
CategoryID Int Identity(1, 1) Not Null Primary Key, -- 카테고리번호
CategoryName VarChar(25) Not Null -- 카테고리명
)
Go
Insert Categories Values('가전') -- 1번 카테고리
Go
Insert Categories Values('컴퓨터') -- 2번 카테고리
Go
Insert Categories Values('서적')
Go
--상품 테이블 설계
Create Table dbo.Products (
ProductID Int Identity(1, 1) Not Null Primary Key, -- 상품 고유번호
ModelName VarChar(25) Not Null, -- 상품명
SellPrice Int Null, -- 가격
CategoryID Int Null --카테고리(1,2,3)
)
Go
--[!] 외래키 따로 지정 : 외부에서
Alter Table dbo.Products
Add Foreign Key(CategoryID) References Categories(CategoryID)
Go
-- 가전, 냉장고, 100
Insert Products Values('냉장고', 100, 1)
Go
-- 컴퓨터, 노트북, 200
Insert Products Values('노트북', 200, 2)
Go
Insert Products Values('데스크톱', 150, 2)
Go
--상품평(코멘트) 테이블 설계
Create Table dbo.Reviews
(
ReviewID Int Identity(1, 1) Primary Key, -- 일련번호
ProductID Int References Products(ProductID), -- 외래키
Comment VarChar(3850) -- 내용
)
Go
Insert Into Reviews Values(1, '냉장실에서 얼음이 얼어요...')
Insert Into Reviews values(2, '브랜드가 모닝글로리')
Select * From Categories
Select * From Products
-- 상품리스트 출력 : 카테고리명, 상품명, 판매가
--[1] SQL Server 전용 문법
Select CategoryName, ModelName, SellPrice
From Categories, Products
Where Categories.CategoryID = Products.CategoryID
Go
--[2] ANSI-SQL 공통 문법
Select CategoryName, ModelName, SellPrice
From Categories Join Products
On Categories.CategoryID = Products.CategoryID
Go
--[3] 상세 표시 : Join은 기본적으로 Inner Join이다.
Select
Categories.CategoryName,
Products.ModelName,
Products.SellPrice
From Categories Inner Join Products
On Categories.CategoryID = Products.CategoryID
Go
--[4] 축약 표시
Select c.CategoryName, p.ModelName, p.SellPrice
From Categories c, Products p
Where c.CategoryID = p.ProductID
Go
Select * From Products
Select * From Reviews
--상품명, 코멘트
-- 기본 조인(Inner Join)은 매치되는 결과만 가져온다. 데스크톱 누락
Select p.ModelName, r.Comment
From Products p Inner Join Reviews r
On p.ProductID = r.ProductID
Go
-- Left Outer Join : 왼쪽 테이블의 모든 목록은 출력
Select p.ModelName, r.Comment
From Products p LEFT OUTER Join Reviews r
On p.ProductID = r.ProductID
Go
-- 3개 테이블 조인
-- 카테고리명, 상품명, 판매가, 코멘트
Select c.CategoryName, p.ModelName, p.SellPrice, r.Comment
From
Categories
c Join Products p
On c.CategoryID = p.CategoryID
Join Reviews r
On p.ProductID = r.ProductID
Go
Select c.CategoryName, p.ModelName, p.SellPrice, r.Comment
From Categories c, Products p, Reviews r
Where
c.CategoryID = p.CategoryID
And
p.ProductID = r.ProductID
Go
'.Net Project > WindowServer2008' 카테고리의 다른 글
09장 테이블 제약조건 (0) | 2009.09.14 |
---|---|
08장 집계함수 (0) | 2009.09.14 |
06장 연산자 (0) | 2009.09.14 |
05장 SubQuery(서브쿼리) (0) | 2009.09.14 |
4장 사용자 생성 (0) | 2009.09.14 |