SQL Server 강의실

시삽: 레드플러스 님 
게시판 이동:
 제목 : 12.1. 뷰(View) 사용 예제 : 카테고리 테이블
글번호: 37
작성자: 레드플러스
작성일: 2005/12/27 오전 10:15:00
조회수: 7711
--카테고리 응용 프로그램 설계

--[0] 테이블 설계
Create Table dbo.Categories
(
   CategoryID Int Identity(1, 1) Primary Key,
   CategoryName VarChar(50),
   SuperCategory Int Null,
   Align SmallInt Default(0)
)
Go

--[1] 입력
Insert Categories Values('컴퓨터', NULL, 0)
Insert Categories Values('노트북', 1, 1)
Insert Categories Values('핸드폰', NULL, 2)
Insert Categories Values('신규', 3, 3)

--[2] 출력
Select CategoryName
From Categories
Order By Align Asc

--[3] 상세
Select * From Categories Where CategoryID = 1

--[4] 수정
Update Categories
Set
    CategoryName = '콤퓨타'
Where CategoryID = 1

--[5] 삭제
Delete Categories Where CategoryID = 2

--[6] 검색
Select * From Categories
Where
    CategoryName Like '%퓨%'
    And
    SuperCategory is NULL --널(NULL)값 비교

--[!] 테이블 삭제
Drop Table dbo.Categories
Go

--[7] 뷰(View) 생성 : Select문 전용
--[a] 대분류 데이터를 출력하는 구문을 줄여서 출력
Select CategoryID, CategoryName
From Categories
Where SuperCategory Is NULL
--Order By CategoryName Asc

--[b] 위 구문을 줄여주는 뷰(View) 생성
Create View dbo.GetTopCategory
As
    Select CategoryID, CategoryName
    From Categories
    Where SuperCategory Is NULL
--    Order By CategoryName Asc
Go

--[c] 뷰(가상 테이블) 사용 : 약간 줄어들죠???
Select * From GetTopCategory
Order By CategoryName Asc

--[d] 뷰(가상 테이블) 수정 : 암호화
sp_helptext GetTopCategory --뷰 구문 보기

Alter View dbo.GetTopCategory
With Encryption -- 개체 암호화 옵션
As
    Select CategoryID, CategoryName
    From Categories
    Where SuperCategory Is NULL
--    Order By CategoryName Asc
Go

sp_helptext GetTopCategory --안 보임

--[e] 뷰 삭제
Drop View dbo.GetTopCategory
Go
 
이전 글   다음 글 삭제 수정 답변 글쓰기 리스트

(댓글을 남기려면 로그인이 필요합니다.)

관련 아티클 리스트
  제       목 파일 작성자 작성일 조회
이전글 13. 조인 및 집계 함수 - 레드플러스 2003-12-05 7764
  12. 뷰(View) - 레드플러스 2003-12-05 7773
현재글 12.1. 뷰(View) 사용 예제 : 카테고리 테이블 - 레드플러스 2005-12-27 7711
다음글 11. 테이블 제약 조건 - 레드플러스 2003-12-05 7672
 
손님 사용자 Anonymous (손님)
로그인 Home