重建log 文件
第一步:
先备份整个数据库以备不测
第二步:
备份结束后,在Query Analyzer中执行如下的语句:
exec sp_detach_db yourDBName,true --卸除这个DB在MSSQL中的注册信息
第三步:
到日志的物理文件所在的目录中去删除该日志文件或者将该日志文件移出该目录
第四步:
在Query Analyzer中执行如下的语句:
exec sp_attach_single_file_db yourDBName,'d:/mssql7/data/yourDBName_data.mdf'
reference :
Transact-SQL Reference |
sp_detach_db
Detaches a database from a server and, optionally, runs UPDATE STATISTICS on all tables before detaching.
Syntax
sp_detach_db [ @dbname = ] 'dbname'
[ , [ @skipchecks = ] 'skipchecks' ]
Arguments
[@dbname =] 'dbname'
Is the name of the database to be detached. dbname is sysname, with a default value of NULL.
[@skipchecks =] 'skipchecks'
skipchecks is nvarchar(10), with a default value of NULL. If true, UPDATE STATISTICS is skipped. If false, UPDATE STATISTICS is run. This option is useful for databases that are to be moved to read-only media.
Return Code Values
0 (success) or 1 (failure)
Result Sets
None
Remarks
The detached files remain and can be reattached using sp_attach_db or sp_attach_single_file_db. The files can also be moved to another server and attached.
Permissions
Only members of the sysadmin fixed server role can execute sp_detach_db.
Examples
This example detaches the pubs database with skipchecks set to true.
EXEC sp_detach_db 'pubs', 'true'
sp_attach_single_file_db
Attaches a database having only one data file to the current server.
Syntax
sp_attach_single_file_db [ @dbname = ] 'dbname'
, [ @physname = ] 'physical_name'
Arguments
[@dbname =] 'dbname'
Is the name of the database to be attached to the server. dbname is sysname, with a default of NULL.
[@physname =] 'phsyical_name'
Is the physical name, including path, of the database file. physical_name is nvarchar(260), with a default of NULL.
Return Code Values
0 (success) or 1 (failure)
Result Sets
None
Remarks
When sp_attach_single_file_db attaches the database to the server, it builds a new log file and performs additional cleanup work to remove replication from the newly attached database.
Used sp_attach_single_file_db only on databases that were previously detached from the server using an explicit sp_detach_db operation.
Permissions
Only members of the sysadmin and dbcreator fixed server roles can execute this procedure.
Examples
This example detaches pubs and then attaches one file from pubs to the current server.
EXEC sp_detach_db @dbname = 'pubs'
EXEC sp_attach_single_file_db @dbname = 'pubs',
@physname = 'c:/Program Files/Microsoft SQL Server/MSSQL/Data/pubs.mdf'
/
压缩
数据库在使用一段时间后,时常会出现因数据删除而造成数据库中空闲空间太多的情况,这时就需要减少分配给数据库文件和事务日志文件的磁盘空间,以免浪费磁盘空间。当数据库中没有数据时,可以修改数据库文件属性直接改变其占用空间,但当数据库中有数据时,这样做会破坏数据库中的数据,因此需要使用压缩的方式来缩减数据库空间。可以在数据库属性选项中选择“Auto shrink”选项,让系统自动压缩数据库,也可以用人工的方法来压缩。人工压缩数据库有以下两种方式:
1、用Enterprise Manager 压缩数据库
在Enterprise Manager 中在所要压缩的数据库上单击右键,从快捷菜单中的“所有任务(All Tasks)”中选择“Shrink Database(压缩数据库)”选项,就会出现如图6-10 所示的对话框。可以在图6-10 所示的对话框中选择数据库的压缩方式,也可以选择使用压缩计划或压缩单个文件。
单击图6-10 中的“Files”按钮,会出现如图6-11 所示的压缩数据库文件对话框,可以针对每个数据库文件进行不同的压缩设置。
单击图6-10 中的“Change” 按钮,会出现如图6-12 所示的压缩计划编辑对话框,可以指定压缩计划的执行方式。单击图6-12 中的“Change” 按钮,会出现如图6-13 所示的循环工作计划编辑对话框,可以编辑计划执行的周期或时间点。设置完成后单击“OK” 按钮就开始压缩数据库,在压缩结束后会显示一个压缩情况信息框。
2、用Transact-SQL 命令压缩数据库
可以使用DBCC SHRINKDATABASE 和DBCC SHRINKFILE 命令来压缩数据库。其中DBCC SHRINKDATABASE 命令对数据库进行压缩,DBCC SHRINKFILE 命令对数据库中指定的文件进行压缩。
(1) DBCC SHRINKDATABASE
DBCC SHRINKDATABASE 命令语法如下:
DBCC SHRINKDATABASE (database_name [, target_percent]
[, {NOTRUNCATE | TRUNCATEONLY}] )
各参数说明如下:
- target_percent 指定将数据库压缩后,未使用的空间占数据库大小的百分之几。如果指定的百分比过大,超过了压缩前未使用空间所占的比例,则数据库不会被压缩。并且压缩后的数据库不能比数据库初始设定的容量小。
- NOTRUECATE
将数据库缩减后剩余的空间保留在数据库,中不返还给操作系统。如果不选择此选项,则剩余的空间返还给操作系统。 - TRUNCATEONLY
将数据库缩减后剩余的空间返还给操作系统。使用此命令时SQL Server 将文件缩减到最后一个文件分配,区域但不移动任何数据文件。选择此项后,target_percent 选项就无效了。
例6-14: 压缩数据库mytest 的未使用空间为数据库大小的20%。
dbcc shrinkdatabase (mytest, 20)
运行结果如下:
DBCC execution completed. If DBCC printed error messages, contact your system administrator.
(2) DBCC SHRINKFILE
DBCC SHRINKFILE 命令压缩当前数据库中的文件。其语法如下:
DBCC SHRINKFILE ( {file_name | file_id }
{ [, target_size] |
[, {EMPTYFILE | NOTRUNCATE | TRUNCATEONLY}] } )
各参数说明如下:
- file_id
指定要压缩的文件的鉴别号(Identification number, 即ID)。文件的ID 号可以通过 FILE_ID()函数或如本章前面所讲述的Sp_helpdb 系统存储过程来得到。 - target_size
指定文件压缩后的大小。以MB 为单位。如果不指定此选项,SQL Server 就会尽最大可能地缩减文件。 - EMPTYFILE
指明此文件不再使用,将移动所有在此文件中的数据到同一文件组中的其它文件中去。执行带此参数的命令后,此文件就可以用ALTER DATABASE 命令来删除了。
其余参数NOTRUNCATE 和TRUNCATEONLY 与DBCC SHRINKDATABASE 命令中的含义相同。
例6-15: 压缩数据库mydb 中的数据库文件mydb_data2 的大小到1MB。 use mydb dbcc shrinkfile (mydb_data2, 1)
sp_detach_db
Detaches a database from a server and, optionally, runs UPDATE STATISTICS on all tables before detaching.
Syntax
sp_detach_db [ @dbname = ] 'dbname'
[ , [ @skipchecks = ] 'skipchecks' ]
Arguments
[@dbname =] 'dbname'
Is the name of the database to be detached. dbname is sysname, with a default value of NULL.
[@skipchecks =] 'skipchecks'
skipchecks is nvarchar(10), with a default value of NULL. If true, UPDATE STATISTICS is skipped. If false, UPDATE STATISTICS is run. This option is useful for databases that are to be moved to read-only media.
Return Code Values
0 (success) or 1 (failure)
Result Sets
None
Remarks
The detached files remain and can be reattached using sp_attach_db or sp_attach_single_file_db. The files can also be moved to another server and attached.
Permissions
Only members of the sysadmin fixed server role can execute sp_detach_db.
Examples
This example detaches the pubs database with skipchecks set to true.
EXEC sp_detach_db 'pubs', 'true'
sp_attach_single_file_db
Attaches a database having only one data file to the current server.
Syntax
sp_attach_single_file_db [ @dbname = ] 'dbname'
, [ @physname = ] 'physical_name'
Arguments
[@dbname =] 'dbname'
Is the name of the database to be attached to the server. dbname is sysname, with a default of NULL.
[@physname =] 'phsyical_name'
Is the physical name, including path, of the database file. physical_name is nvarchar(260), with a default of NULL.
Return Code Values
0 (success) or 1 (failure)
Result Sets
None
Remarks
When sp_attach_single_file_db attaches the database to the server, it builds a new log file and performs additional cleanup work to remove replication from the newly attached database.
Used sp_attach_single_file_db only on databases that were previously detached from the server using an explicit sp_detach_db operation.
Permissions
Only members of the sysadmin and dbcreator fixed server roles can execute this procedure.
Examples
This example detaches pubs and then attaches one file from pubs to the current server.
EXEC sp_detach_db @dbname = 'pubs'
EXEC sp_attach_single_file_db @dbname = 'pubs',
@physname = 'c:/Program Files/Microsoft SQL Server/MSSQL/Data/pubs.mdf'
/
压缩
数据库在使用一段时间后,时常会出现因数据删除而造成数据库中空闲空间太多的情况,这时就需要减少分配给数据库文件和事务日志文件的磁盘空间,以免浪费磁盘空间。当数据库中没有数据时,可以修改数据库文件属性直接改变其占用空间,但当数据库中有数据时,这样做会破坏数据库中的数据,因此需要使用压缩的方式来缩减数据库空间。可以在数据库属性选项中选择“Auto shrink”选项,让系统自动压缩数据库,也可以用人工的方法来压缩。人工压缩数据库有以下两种方式:
1、用Enterprise Manager 压缩数据库
在Enterprise Manager 中在所要压缩的数据库上单击右键,从快捷菜单中的“所有任务(All Tasks)”中选择“Shrink Database(压缩数据库)”选项,就会出现如图6-10 所示的对话框。可以在图6-10 所示的对话框中选择数据库的压缩方式,也可以选择使用压缩计划或压缩单个文件。
单击图6-10 中的“Files”按钮,会出现如图6-11 所示的压缩数据库文件对话框,可以针对每个数据库文件进行不同的压缩设置。
单击图6-10 中的“Change” 按钮,会出现如图6-12 所示的压缩计划编辑对话框,可以指定压缩计划的执行方式。单击图6-12 中的“Change” 按钮,会出现如图6-13 所示的循环工作计划编辑对话框,可以编辑计划执行的周期或时间点。设置完成后单击“OK” 按钮就开始压缩数据库,在压缩结束后会显示一个压缩情况信息框。
2、用Transact-SQL 命令压缩数据库
可以使用DBCC SHRINKDATABASE 和DBCC SHRINKFILE 命令来压缩数据库。其中DBCC SHRINKDATABASE 命令对数据库进行压缩,DBCC SHRINKFILE 命令对数据库中指定的文件进行压缩。
(1) DBCC SHRINKDATABASE
DBCC SHRINKDATABASE 命令语法如下:
DBCC SHRINKDATABASE (database_name [, target_percent]
[, {NOTRUNCATE | TRUNCATEONLY}] )
各参数说明如下:
- target_percent 指定将数据库压缩后,未使用的空间占数据库大小的百分之几。如果指定的百分比过大,超过了压缩前未使用空间所占的比例,则数据库不会被压缩。并且压缩后的数据库不能比数据库初始设定的容量小。
- NOTRUECATE
将数据库缩减后剩余的空间保留在数据库,中不返还给操作系统。如果不选择此选项,则剩余的空间返还给操作系统。 - TRUNCATEONLY
将数据库缩减后剩余的空间返还给操作系统。使用此命令时SQL Server 将文件缩减到最后一个文件分配,区域但不移动任何数据文件。选择此项后,target_percent 选项就无效了。
例6-14: 压缩数据库mytest 的未使用空间为数据库大小的20%。
dbcc shrinkdatabase (mytest, 20)
运行结果如下:
DBCC execution completed. If DBCC printed error messages, contact your system administrator.
(2) DBCC SHRINKFILE
DBCC SHRINKFILE 命令压缩当前数据库中的文件。其语法如下:
DBCC SHRINKFILE ( {file_name | file_id }
{ [, target_size] |
[, {EMPTYFILE | NOTRUNCATE | TRUNCATEONLY}] } )
各参数说明如下:
- file_id
指定要压缩的文件的鉴别号(Identification number, 即ID)。文件的ID 号可以通过 FILE_ID()函数或如本章前面所讲述的Sp_helpdb 系统存储过程来得到。 - target_size
指定文件压缩后的大小。以MB 为单位。如果不指定此选项,SQL Server 就会尽最大可能地缩减文件。 - EMPTYFILE
指明此文件不再使用,将移动所有在此文件中的数据到同一文件组中的其它文件中去。执行带此参数的命令后,此文件就可以用ALTER DATABASE 命令来删除了。
其余参数NOTRUNCATE 和TRUNCATEONLY 与DBCC SHRINKDATABASE 命令中的含义相同。
例6-15: 压缩数据库mydb 中的数据库文件mydb_data2 的大小到1MB。 use mydb dbcc shrinkfile (mydb_data2, 1)