Table of Contents

Database Maintenance

A quick reference for common database tasks like compacting, repairing and creating a backup for databases and setting up SQL Server.

Notes on Compacting: Both MDB and SQL Server databases can automatically grow much more than needed due to fragmented data. For this reason it is recommended to regularly compact the databases. Compacting obviously can also make backups and copies much smaller.

MDB

SQL Server

For all of the following SQL Server tasks, the commands can be run in several ways:


Tasks:

ALTER DATABASE <dbname> SET single_user with rollback immediate 
DBCC checkdb(<dbname>,REPAIR_ALLOW_DATA_LOSS)
USE <dbname>
GO
EXEC sp_MSforeachtable @command1="print '?' DBCC DBREINDEX ('?', ' ', 80)"
GO
SELECT OBJECT_NAME(ps.object_id) [TableName], i.name [IndexName], ps.Index_type_desc [IndexType], CONVERT(TINYINT,ps.avg_fragmentation_in_percent) [AvgFrag%], CONVERT(TINYINT,ps.avg_page_space_used_in_percent) [AvgSpaceUsed%], ps.record_count [RecordCount], ps.fragment_count [FragmentCount] 
FROM   sys.dm_db_index_physical_stats(db_id(db_name()),NULL,NULL,NULL,'DETAILED') ps INNER JOIN sys.indexes i  ON ps.object_id = i.object_id 
     AND ps.index_id = i.index_id INNER JOIN sys.tables t ON ps.object_id = t.object_id 
WHERE  ps.index_id > 0
ORDER BY [AvgFrag%] DESC,[TableName],[IndexName]
DBCC SHRINKDATABASE(<dbname>,2);
USE <dbname>
GO
EXEC sp_MSforeachtable @command1="print '?' DBCC DBREINDEX ('?', ' ', 80)"
GO
DBCC UPDATEUSAGE (<dbname>) WITH NO_INFOMSGS;
USE master select text,wait_time,blocking_session_id AS "Block", percent_complete, * from sys.dm_exec_requests CROSS APPLY sys.dm_exec_sql_text(sql_handle) AS s2 order by start_time asc


SQL Express Connectivity

When installing SQL Server Express, by default it may not be configured for connectivity from different clients or from remote machines. The following may need to be configured in order to allow ClayCentral to connect and here are also some tips for handling some tasks without SQL Management Studio.