SQL Server基礎SQL腳本之分區表、分區方案

--建立文件組
create database alex2
on primary
(=,=,=,=,=),
Filegroup old
(=,=,=,=,=),
Filegroup first
(=,=,=,=,=),
Filegroup second
(=,=,=,=,=),
Filegroup third
(=,=,=,=,=%),
Filegroup fourth
(=,=,=,=,=%)
log on
(=,=,=,=,=)
go

--使用數據庫
use alex2
go

--建立分區函數
--按照必定的條件劃分數據
--range left   (--,--] (--,--] 分界點的值歸左邊
--range right  (--,--) [--,--)    分界點的值歸右邊
create partition (datetime)
as range right values(,,,)


    --建立分區方案
--將分區函數區分的範圍和文件組對應起來
create partition scheme RateChngDate_Scheme
as partition to (Old,First,Second,Third,Fourth)

--建立分區表
create table EmpPayHistPart
(
    EmployeeID int,
    RateChangeDate datetime,
    Rate money,
    PayFrequency tinyint,
    ModifiedDate datetime
)on RateChngDate_Scheme(RateChangeDate)


--添加數據
insert into EmpPayHistPart values(,,,,)
insert into EmpPayHistPart values(,,,,)
insert into EmpPayHistPart values(,,,,)
insert into EmpPayHistPart values(,,,,)
insert into EmpPayHistPart values(,,,,)
--檢索分區
select * from EmpPayHistPart where $partition.RateChngDate(RateChangeDate)=select * from EmpPayHistPart where $partition.RateChngDate(RateChangeDate)=select * from EmpPayHistPart where $partition.RateChngDate(RateChangeDate)=select * from EmpPayHistPart where $partition.RateChngDate(RateChangeDate)=select * from EmpPayHistPart where $partition.RateChngDate(RateChangeDate)=select * from EmpPayHistPart where $partition.RateChngDate(RateChangeDate)=select * from EmpPayHistPart where $partition.RateChngDate(RateChangeDate)=select * from EmpPayHistPart


---------------分隔分區
--修改數據庫添加文件組
alter database alex2
add    filegroup Fifth
--修改數據庫向文件組中添加文件
alter database alex2
add (=,=,=,=,=)
to filegroup Fifth
go
--修改分區方案 讓下一個分區使用Fifth文件組
alter partition scheme RateChngDate_Scheme
next used Fifth
--修改分區函數 加入一個臨界點2002--alter partition ()
split range ()
go

---------------------合併分區
--將2008--以後的數據和前一個分區數據合併
--原來是2004--到2008--,--到之後
--如今是2004--到之後
alter partition ()
merge range()

-------------------建立表保存分區
--修改數據庫添加文件組Sixth
alter database alex2
add    filegroup Sixth
--修改數據庫添加文件到文件組Sixth
alter database alex2
add (=,=,=,=,=)
to filegroup Sixth
go
--修改分區方案讓下一個分區對應文件組Sixth
alter partition scheme RateChngDate_Scheme
next used Sixth
--分隔數據分區
alter partition ()
split range ()
go
--建立表來保存分區數據
create table New_EmpPayHistPart
(
    EmployeeID int,
    RateChangeDate datetime,
    Rate money,
    PayFrequency tinyint,
    ModifiedDate datetime
)on Sixth
--將分區6中的數據移動到新表中
alter table EmpPayHistPart
partition to New_EmpPayHistPart
相關文章
相關標籤/搜索