Tuesday, August 23, 2011

How to Move SQL server user Database?

-->
Microsoft SQL server has two types of database, one is system database and another is user database. A system database is used by the SQL server for its own maintenance and management while a user database is used by the database administrator for storing user data. In this article, we will discuss about to “How to move SQL server user database within same instance”.

A database administrator can move data and log files of user database to a new location by specifying the new location in a filename clause of ALTER database statement. To move a SQL server user database follow the below steps:

  1. Set the database off-line through this command
ALTER DATABASE database_name SET OFFLINE;
  1. Move the files to a designation location
Run this command for each move file.
ALTER DATABASE database_name MODIFY FILE ( NAME = logical_name, FILENAME = ' new_path\os_file_name' );
  1. Now set the database online through this command
ALTER DATABASE database_name SET ONLINE;
  1. Verify the changes
SELECT name, physical_name AS CurrentLocation, state_desc
FROM sys.master_files
WHERE database_id = DB_ID(N'');

Note: The above method is applied to moving database within the same instance of SQL server database. To move database from one instance to another, you can try backup and restore method.

Example: Suppose we have a log file sqlserverdatabase and want to move in to a new location.

Use master;
Go
SELECT name, physical_name AS CurrentLocation, state_desc
FROM sys.master_files
WHERE database_id = DB_ID(N'sqlserverdatabase')
AND type_desc = N'LOG';
Go
ALTER DATABASE sqlserverdatabase SET OFFLINE;
GO
ALTER DATABASE sqlserverdatabase MODIFY FILE ( NAME = sqlserverdatabase_log, FILENAME = 'C:\NewLoc\sqlserverdatabase_Log.ldf');
Go
ALTER DATABASE sqlserverdatabase SET ONLINE;
GO
SELECT name, physical_name AS CurrentLocation, state_desc
FROM sys.master_files
WHERE database_id = DB_ID(N'sqlserverdatabase');
AND type_desc = N'LOG';

Friday, August 19, 2011

Restore SQL server Database in case of Corruption

MS SQL server database offers a high performance backup and restore facility to its database users for maintaining the backup of SQL server database and restore in case of corruption. SQL server database may be corrupt due to severely reasons like media failure, user errors, power failure, metadata structure corruption, virus attack and many more. High performance backup and restore facility enables database administrator to handle all above corruption issues in SQL server database. A well planed backup and restore strategy helps database administrator to protect their data in case of above mentioned failures. The point comes to describe backup.

Backup: A copied data that can be used in restoring the corrupted data is called backup.

How to make a backup of SQL server database? : A database administration can take a backup of SQL server database with the help of SQL server management studio. Below image is the screen-shot to make a backup in SQL server database.




Restore SQL server Database in case of Corruption?: A database administrator can restore corrupt SQL server database with the help of good backup but he/she will be also able to restore corrupt SQL database without any backup. In this article we will discuss about both two methods for restoring corrupt SQL server database.

Restore with Good Backup: A database administrator can use “backup & restore” method, if he has maintained a good backup for his database. Two machines are required in this method. One is source machine where corrupt database reside and another is designation machine where corrupted database will be restored. 

Note: Make sure following point before restoring the database
  • The designation machine has sufficient space to restore.
  • Directory structure on designation machine is must to exist.
  • Don't have same file name on the designation machine.
Restore without Backup: A database administrator is still able to restore corrupt SQL server data files without any good backup. In this situation, you should take help from any Microsoft gold certified SQL server recovery software. Stellar Phoenix SQL recovery software is a Microsoft gold certified partner that repairs corrupt SQL server data files and its objects. It is the most recommended software by the database expert to repair corrupt SQL server database.