--备份数据库
BACKUP DATABASE db_name TO DISK = N'D:\db_name.bak' WITH NOFORMAT, NOINIT, NAME = N'db_name-完整 数据库 备份', SKIP, NOREWIND, NOUNLOAD,
COMPRESSION|NO_COMPRESSION, STATS = 10 ---完整 数据库 备份
--COMPRESSION 使用压缩备份数据库
--NO_COMPRESSION 不使用压缩备份
--不选 COMPRESSION|NO_COMPRESSION, 代表使用默认服务器设置备份
---差异备份
BACKUP DATABASE db_name TO DISK = N'D:\db_name.bak' WITH DIFFERENTIAL , NOFORMAT, NOINIT, NAME = N'db_name-差异 数据库 备份', SKIP, NOREWIND, NOUNLOAD, COMPRESSION, STATS = 10
GO
---事务日志备份
BACKUP LOG db_name TO DISK = N'D:\db_name.bak' WITH NOFORMAT, NOINIT, NAME = N'db_name-事务日志 备份', SKIP, NOREWIND, NOUNLOAD, COMPRESSION, STATS = 10
GO
--还原数据库(覆盖现有库,执行未提交回滚)
RESTORE DATABASE db_name FROM DISK = N'D:\db_name.bak' WITH FILE = 1, MOVE N'db_name' TO N'e:\db\db_name.mdf', MOVE N'db_name_log' TO N'e:\db\db_name_log.ldf', NOUNLOAD, REPLACE, STATS = 10
GO
--还原数据库(覆盖现有库,不执行未提交回滚)
RESTORE DATABASE db_name FROM DISK = N'D:\db_name.bak' WITH FILE = 1, MOVE N'db_name' TO N'e:\db\db_name.mdf', MOVE N'db_name_log' TO N'e:\db\db_name_log.ldf', NORECOVERY, NOUNLOAD, REPLACE, STATS = 10
GO
----还原数据库(覆盖现有库,使数据库为只读模式,撤销未提交事务,并将撤销的未提交事务保存到指定路径,以备恢复逆操作)
RESTORE DATABASE db_name FROM DISK = N'D:\db_name.bak' WITH FILE = 1, MOVE N'db_name' TO N'e:\db\db_name.mdf', MOVE N'db_name_log' TO N'e:\db\db_name_log.ldf', STANDBY = N'e:\ROLLBACK_UNDO_db_name.BAK', NOUNLOAD, REPLACE, STATS = 10
GO
--分离数据库
USE master
GO
ALTER DATABASE db_test SET SINGLE_USER WITH ROLLBACK IMMEDIATE --先将DB设置为单用户并回滚当前的所有操作,即删除所有链接
GO
USE master
GO
EXEC master.dbo.sp_detach_db @dbname = N'db_test' ---分离DB
GO
--附加被分离的DB(如果是用图形话工具执行附加,只需要选中主文件即可)
USE master
GO
CREATE DATABASE db_test ON
( FILENAME = N'E:\db\mytest.mdf' ),
( FILENAME = N'E:\db\mytest_log.ldf' ),
( FILENAME = N'f:\db\fileone.ndf' ),
( FILENAME = N'E:\db\filethrs.ndf' )
FOR ATTACH
GO
GO
EXEC master.dbo.sp_detach_db @dbname = N'db_name' --,@skipchecks = 'false'--更新统计信息
GO
---使得数据库脱机和联机
ALTER DATABASE db_name SET ONLINE---联机
ALTER DATABASE db_name SET OFFLINE--脱机
---修改数据库只读、可写属性
USE [master]
GO
ALTER DATABASE [db_name] SET READ_ONLY WITH NO_WAIT ---只读
GO
ALTER DATABASE [db_name] SET READ_WRITE WITH NO_WAIT --可写
GO
--收缩数据库
USE db_name
go
DBCC SHRINKDATABASE(N'db_name', 50 )---收缩后文件中的最大可用空间百分比。db_name数据库名
--可以不指定(默认模式)
DBCC SHRINKDATABASE(N'db_name')
GO
---收缩文件
DBCC SHRINKFILE (N'db_name' , 0, TRUNCATEONLY)---收缩数据文件,释放未使用空间 db_name数据文件名
GO
DBCC SHRINKFILE (N'db_name' , 50) ---将文件收缩到指定大小(MB)
GO
DBCC SHRINKFILE (N'db_name_log' , 0, TRUNCATEONLY)---收缩日志文件,释放未使用空间 db_name_log日志文件名
GO
DBCC SHRINKFILE (N'db_name_log' , 150)---将日志文件收缩到指定大小(MB)
GO
---删除指定的数据文件
ALTER DATABASE db_name REMOVE FILE dbfile ---dbfile 是指逻辑文件名非文件组名
GO
--修改指定文件组中指定逻辑文件的初始化大小.并指定文件自动增长及文件的增长量和文件最大值
ALTER DATABASE db_name MODIFY FILE ( NAME = N'dbfile', SIZE = 51200KB,MAXSIZE = 460800KB ,FILEGROWTH = 10240KB )
--将数据改为简单恢复模式
USE master
GO
ALTER DATABASE db_name SET RECOVERY SIMPLE WITH NO_WAIT
go
--将数据库改为完整恢复模式
ALTER DATABASE db_name SET RECOVERY FULL WITH NO_WAIT
GO
--将数据库改为大容量日志恢复模式
ALTER DATABASE db_name SET RECOVERY BULK_LOGGED WITH NO_WAIT
GO
EXECUTE master.dbo.xp_delete_file 0,N'D:\db_name.bak',N'bak',N'2011-05-08T09:36:49' ---删除两周内的数据