블로그 이미지
Magic_kit
study 관련자료를 한곳으로 자기 개발 목적으로 재태크 재무 관리 목적으로 일상생활의 팁을 공유 하기 위하여 블로그를 개설 하였습니다.

calendar

1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28

Category

Recent Post

Recent Comment

Archive

2009. 9. 14. 23:11 .Net Project/WindowServer2008
반응형

-- 조인(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
posted by Magic_kit