---- 1.使用默认配置创建数据库
use master
go
if not exists (select name from sys.databases where name = 'test') ----检查是否存在名为test
create database test ----创建test数据库
go
---- 2.查看数据库信息
exec sp_helpdb 'test'
---- 3.使用文件�x���建�����
create database test
on primary
(
name = 'test', ----����斓倪��名�Q
filename = 'd:\test.mdf', ----物理名�Q
size = 10MB, ----文件大小
maxsize = 50MB, ----文件最大值
filegrowth = 10% ----文件大小增量
),
filegroup FG2 --default ----��建文件�M(如果指定default,默�J��建的�ο蟊4娴竭@�e)
(
name = 'test1', ----����斓倪��名�Q
filename = 'd:\test1.ndf', ----物理名�Q
size = 10MB, ----文件大小
maxsize = 50MB, ----文件最大值
filegrowth = 10%
)
log on
(
name = 'test_log',
filename = 'f:\test.ldf',
size = 10MB,
maxsize = 50MB,
filegrowth = 12%
)
go
----�榱颂岣����斓淖x��性能,最好�⑷照I文件和���文件放置在不同�P符下。
---- 4.�O置����煊�粼L��
alter database test
set single_user ----multi_user|restricted_user
with rollback immediate --no_wait
---- 5.重命名�����
alter database test
modify name = test1
exec sp_renamedb 'test1','test'
---- 6.分�x、附加、�h除�����
use master
go
exec sp_detach_db 'test','false' ----分�x����欤��z查�y�更新
create database test ----附加�����
on
(
filename = 'd:\test.mdf'
)
for attach ----��事物日�I文件不可用�r,指定attach_rebuild_log重建事物日�I文件
drop database test ----�h除�����
----7.修改允�S外部�L��的�����
create database test ----��建�����r�⒂没蚪�用外部�L���x�
...
WITH {DB_CHAINING {ON|OFF}
|TRUSTWORTHY{ON|OFF}}
ALTER DATABASE TEST ----�ΜF有的����爝M行修改
{
SET DB_CHAINING {ON|OFF}
|TRUSTWORTHY{ON|OFF}
}
exec sp_configure 'show advanced option',0
reconfigure
----8.配置����旎�湍J�
ALTER DATABASE TEST
SET RECOVERY FULL ---- BULK_LOGGED | SIMPLE
SELECT RECOVERY_MODEL_DESC FROM SYS.databases WHERE name = 'TEST' ----查看����斓幕�湍J�
---- 9.配置���C
ALTER DATABASE TEST
SET PAGE_VERIFY CHECK_SUM ----TORN_PAGE_DETECTION | NONE
SELECT PAGE_VERIFY_OPTION_DESC FROM SYS.databases WHERE name = 'TEST'
---- 10.修改����斓��B
ALTER DATABASE TEST
SET ONLINE ----OFFLINE | EMERENCY
---- 11.添加、�h除���和日�I文件
ALTER DATABASE TEST ----添加���文件
ADD FILE
(
NAME = 'TEST2',
FILENAME = 'D:\TEST2.NDF',
SIZE = 10MB,
MAXSIZE = 60MB
)
TO FILEGROUP FG2
ALTER DATABASE TEST ----添加日�I文件
LOG FILE
(
NAME = 'TEST2_LOG',
FILENAME = 'D:\TEST2_LOG.LDF',
SIZE = 5MB
)
DBCC SHRINKFILE (TEST2 , EMPTYFILE) ----�囊��h除的文件中移走既有的���
ALTER DATABASE TEST ---- �����熘�h除文件
REMOVE FILE TEST2
ALTER DATABASE TEST ----重新分配���或日�I文件
MODIFY FILE
(
NAME = 'TEST',
FILENAME = 'D:\TEST.MDF' ----要重新分配的物理文件的��名�Q
)
ALTER DATABASE TEST ----添加文件�M
ADD FILEGROUP FG3
ALTER DATABASE TEST ----�O置默�J文件�M
MODIFY FILEGROUP FG3 DEFAULT
ALTER DATABASE TEST ----�h除文件�M
REMOVE FILEGROUP FG3
ALTER DATABASE TEST ----修改����斓淖x���嘞�
SET READ_ONLY ----READ_WRITE
ALTER DATABASE TEST ----修改文件�M的�x���嘞�
MODIFY FIELGROUP FG3 READ_ONLY ----READ_WRITE
use test
exec sp_spaceused ----查看����炜臻g使用情�r
DBCC SQLPERF (LOGSPACE)
WITH NO_INFOMSGS ----�@示整����例的日�I信息
---- 11.收�s����旌���文件
DBCC SHRINKDATABASE ('TEST' , 10) ---NOTRUNCATE | TRUNCATEONLY
DBCC SHRINKFILE ('TEST_LOG' , 5)