테이블 작성
--Drop table dbo.zip
Create Table dbo.Zip
(
ZipCode NVarChar(8) Not Null, --우편번호
Si NVarChar(150) Null, --시도
Gu NVarChar(150) Null, --구군
Dong NVarChar(255) Null, --동면
PostEtc NVarChar(255) Null, --번지
)
Go
--6가지 예시문 작성
--[1] 입력
Insert Zip Values('404-230','인천시','서구','가정동','113번지')
Insert Zip Values('404-230','광명시','서구','가정동','113번지')
Insert Zip Values('404-230','서울시','서구','광명동','113번지')
Go
--[2] 출력
Select *From Zip Order By ZipCode Asc
Go
--[3] 상세
Select *From Zip Where Dong ='가정동'
Go
Select *From Zip Where Dong ='광명동'
Go
--[4] 수정
Begin Tran
Update Zip
Set PostEtc = '456번지' Where ZipCode = '404-203'
--RollBack Tran
Commit Tran
--[5] 삭제
Begin Tran
Delete zip
Where 1 = 1
--RollBack Tran
Commit Tran
--[6] 검색
Select *From Zip
Where Dong Like '%가정%'
Go
--[7] View 뷰 생성
Create View dbo.Onlysi
As
Select Distinct Si From Zip --시 정보 중복을 제거해서 출력
Go
--출력
Select *From Onlysi
Go
----------------------------------------------------------------------
--[1] 입력
Drop Procedure dbo.AddZip
Create Procedure dbo.AddZip
@ZipCode NVarChar(8),
@Si NVarChar(150),
@Gu NVarChar(150),
@Dong NVarChar(255),
@PostEtc NVarChar(255)
As
Insert Zip Values('404-230','인천시','서구','가정동','113번지')
Insert Zip Values('404-230','광명시','서구','가정동','113번지')
Insert Zip Values('404-230','서울시','서구','광명동','113번지')
Go
--[2] 출력
Drop Procedure dbo.GetZip
Create Procedure dbo.GetZip
As
Select *From Zip Order By ZipCode Asc
Go
--[3] 상세
--Drop Procedure GetZipByDong
Drop Procedure dbo.GetZipByDong
Create Procedure dbo.GetZipByDong
@Dong NVarChar(255)
As
Select *From Zip Where Dong =@Dong
Go
--[4] 수정
Drop Proc dbo.UpdateZip
Create Proc dbo.UpdateZip
@PostEtc NVarChar(255),
@ZipCode NVarChar(8)
As
Begin Tran
Update Zip
Set PostEtc = @PostEtc Where ZipCode = @ZipCode
--RollBack Tran
Commit Tran
Go
--[5] 삭제
Drop Proc dbo.DeleteZip
Create Proc dbo.DeleteZip
As
Delete zip Where 1 = 1
--RollBack Tran
Commit Tran
Go
--[6] 검색
Drop Proc dbo.SearchZip
Create Proc dbo.SearchZip
@SearchQuery VarChar(50)
As
--Declare @SearchQuery VarChar(50)
--Set @SearchQuery = '가정'
Declare @sql VarChar(500)
Set @sql = 'Select *From Zip Where Dong Like ''' + @SearchQuery +
'%''
'
Print @sql
Exec(@sql)
Go
--인덱스 생성 : 테이블에 딱한개(클러스터드인덱스), 넌클러스터드인덱스(254)
Create Clustered Index idxZip On Zip(Dong)
Go |