惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

S
Secure Thoughts
Security Latest
Security Latest
Simon Willison's Weblog
Simon Willison's Weblog
O
OpenAI News
GbyAI
GbyAI
L
LINUX DO - 最新话题
A
Arctic Wolf
T
Tor Project blog
G
GRAHAM CLULEY
I
InfoQ
博客园_首页
IT之家
IT之家
The Register - Security
The Register - Security
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
P
Proofpoint News Feed
The GitHub Blog
The GitHub Blog
Blog — PlanetScale
Blog — PlanetScale
N
Netflix TechBlog - Medium
K
Kaspersky official blog
博客园 - 三生石上(FineUI控件)
S
SegmentFault 最新的问题
U
Unit 42
PCI Perspectives
PCI Perspectives
量子位
P
Palo Alto Networks Blog
S
Securelist
T
Troy Hunt's Blog
博客园 - 【当耐特】
Recorded Future
Recorded Future
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
S
Security Affairs
Engineering at Meta
Engineering at Meta
T
The Blog of Author Tim Ferriss
博客园 - 聂微东
罗磊的独立博客
N
News and Events Feed by Topic
人人都是产品经理
人人都是产品经理
B
Blog RSS Feed
NISL@THU
NISL@THU
C
Cisco Blogs
T
Threatpost
有赞技术团队
有赞技术团队
Forbes - Security
Forbes - Security
Hugging Face - Blog
Hugging Face - Blog
Last Week in AI
Last Week in AI
T
The Exploit Database - CXSecurity.com
Cloudbric
Cloudbric
Cyberwarzone
Cyberwarzone
Google DeepMind News
Google DeepMind News
C
Cyber Attacks, Cyber Crime and Cyber Security

博客园 - Edison Zhu

Zhuanzai: change Asp.net Themes dynamicly (Setting An ASP.NET Theme in the PreInit Event Handler) zhuanzai: AJAX: How to create a "Processing" modal window using UpdateProgress and ModalPopup ASP.net AJAX controls - Edison Zhu 转:Manage Web.config from XML File by using configSource attribute ASP.Net 2.0 [转载] 微软SQL Server事务隔离级别实例简介 [转载] SQL SERVER – 2005 – Database Table Partitioning Tutorial – How to Horizontal Partition Database Table [转载] SQL SERVER – 2005 – Introduction to Partitioning 转载:.NET Programming Standards and Naming Conventions [引]:聚集索引与非聚集索引 Trouble Shooting: can not access Excel file using excel control C#中小数点后保留两位小数,四舍五入的函数及使用方法 关于锁 IE7下关闭窗口不弹出提示窗口方法 [转]ASP.NET如何在客户端调用服务端代码 [转]谈谈Cookie存取和IE页面缓存的问题 Operate File [转] 客户端的JavaScript脚本中获取服务器端控件的值 及ID BULK INSERT Temporary Tables [转]调用.NET XML Web Services返回数据集合
[转载] Moving table to a different filegroup in SQL 2005
Edison Zhu · 2009-09-30 · via 博客园 - Edison Zhu

source article: http://decipherinfosys.wordpress.com/2007/08/14/moving-tables-to-a-different-filegroup-in-sql-2005/

In SQL Server 2005, a new clause ‘MOVE TO’ was added to the ALTER TABLE command to allow for moving a table to a different filegroup. MOVE TO clause is used along with DROP CONSTRAINT clause in the ALTER TABLE syntax. When a primary key constraint or a constraint which created a clustered index is dropped, leaf level data rows of the clustered index are placed in non clustered table. In SQL Server 2005, when clustered index is dropped (By dropping constraint that created clustered index), one can move table to new filegroup or partition scheme in same transaction by using this new ‘MOVE TO’ option. Let us see this with the help of an example. For our test purpose, we will create test database, couple of filegroups and a table and will populate it with some data.

USE master
GO
CREATE DATABASE TEST
GO

ALTER DATABASE TEST ADD FILEGROUP TEST_DATA_1
GO
ALTER DATABASE TEST ADD FILEGROUP TEST_DATA_2
GO

ALTER DATABASE TEST
ADD FILE
( NAME = TEST1,
FILENAME = ‘C:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\DATA\TEST_1.ndf’,
SIZE = 1MB,
MAXSIZE = 10MB,
FILEGROWTH = 1MB)
TO FILEGROUP TEST_DATA_1
GO

ALTER DATABASE TEST
ADD FILE
( NAME = TEST2,
FILENAME = ‘C:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\DATA\TEST_2.ndf’,
SIZE = 1MB,
MAXSIZE = 10MB,
FILEGROWTH = 1MB)
TO FILEGROUP Test_DATA_2
GO

USE TEST
GO

CREATE TABLE TAB1
(
TAB1_ID INT IDENTITY(1,1),
TAB1_NAME VARCHAR(100),
CONSTRAINT PK_TAB1 PRIMARY KEY(TAB1_ID)
) ON TEST_DATA_1 –- Filegroup we created.
GO

INSERT INTO TAB1(TAB1_NAME)
SELECT Table_Name
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_TYPE = ‘BASE TABLE’
GO

Execution of sp_help system stored procedure for table TAB1 will indicate that filegroup for table is TEST_DATA_1.

sp_help TAB1

Selected output of above command is

Data_located_on_filegroup
—————————
TEST_DATA_1

index_name index_description
———- ——————————————————-
PK_TAB1 clustered, unique, primary key located on TEST_DATA_1

constraint_type constraint_name
————————– —————-
PRIMARY KEY (clustered) PK_TAB1

Above result indicates that PK_TAB1 constraint is used to create the clustered index on the table TAB1. Please note that whenever a primary key constraint is defined, and clustered index does not exist on the table, SQL Server will create clustered index for the primary key constraint. It is not the same for UNIQUE constraint. Unique constraint/ index will always be non-clustered unless other wise it is specified during creation of constraint or index. We had covered this before in the differences between a primary key and a unique constraint blog post.

In order to move the table to a different file group, we need to use the drop constraint command along with the MOVE TO option as shown below. Once the table is moved to a new file group, we can re-create the primary key constraint.

ALTER TABLE TAB1 DROP CONSTRAINT PK_TAB1 WITH (MOVE TO TEST_DATA_2)
GO
ALTER TABLE TAB1 ADD CONSTRAINT PK_TAB1 PRIMARY KEY(TAB1_ID)
GO

After executing the above command, table TAB1 will now reside on filegroup TEST_DATA_2. If you re-execute sp_help stored procedure, it will show the filegroup change for table TAB1.

sp_help TAB1

Data_located_on_filegroup
—————————
TEST_DATA_2

We need to keep in mind the following restrictions when the ‘MOVE TO’ option is used:

• MOVE TO is not valid for indexed views or non-clustered indexes.
• The partition scheme or filegroup must already exist.
• If MOVE TO is not specified, the table will be located in the same partition scheme or filegroup as was defined for the clustered index.

This becomes very handy when we want to move some large tables to their own filegroup for performance reasons or for backing up individual tables via filegroup backup. We can also keep this into consideration when we need to clone table definition and data.