Thursday, 30 April 2015

Know About .MDF and .NDF In SQL Server

In SQL Server Records (Data) and log information is not stored in one file. There are three types of individual files used by SQL server to store its information. These files are Primary data file and secondary data file. These two individual files are used by one SQL server. These different files have their different file extensions. Below is a brief description of these two files.

Primary data files (.Mdf file)

In a nutshell, .Mdf, also known as a master data file of SQL server and is the Primary database file where the data stored physically. It is a very important file of SQL server. Because it stores very crucial information of the database like tables, views, stored procedure, triggers… etc. and it also links to other database files. In another way we can say, a Primary data file is a file which contains the schema with data. It is necessary to have one primary data file in every database.

When we create a database through create command, it creates a data file with .Mdf extension. This extension is recommended for Primary data file.

Secondary data files (.Ndf file)

The Ndf file of the database is also known as a secondary data file. These are optional and user defined data files and also store users' data. It is not important to have a secondary data file. If a database uses maximum size for a single file, then we can use secondary data file. Therefore, some databases not required any secondary data file. A simpler way, we can say that a secondary data file is an extra file by which you can continue to grow your database.

While it is not necessary to have a secondary data file, but you can have more than one secondary data file for a single database.The recommended file extension for a secondary data file is .Ndf.

Note:Though the required file extensions for these two files is .Mdf and .Ndf. The SQL server Never enforces you to use these file extensions.

These two files (Primary and secondary) are located and stored in the Primary file of the database and in the master database. Whenever a SQL server database engine wants to use file location of those two files, it retrieves the data from the master database.

Sometimes complete database inaccessibility may cause of corruption in any of these data files.

.Mdf and .Ndf File Corruption issues on SQL server

The SQL Server database files (MDF and NDF) involve important data so they should be well maintained. It should be frequently retrieved, managed and stored, so the integrity maintenance of the data is a very important term in SQL server database. If it is not managed properly, it can be cause of corruption

There are many corruption issues arising in SQL Server. Some of the common error messages that could appear while accessing corrupt MDF file are mentioned below.

Error messages:

  • Table error: Object ID 0, index ID 0, and page ID (1:105). The PageId in the page header = (0:0)
  • Buffer provided to read column value is too small
  • Conflict, occurred in database'db_name', table 'table_name', column 'column_name'
  • Memory or buffer error, space provided to read column is too small

In such circumstances, you should use the DBCC CHECKDB command-line tool to repair data corruption.

Run following command to check the physical consistency of the database:

'DBCC CHECKDB ('neo') WITH PHYSICAL_ONLY; GO'

The two manual repair options used with DBCC CHECKDB are:

REPAIR_ALLOW_DATA_LOSS
This option will usually try to repair all reported errors and might also cause some data loss. This option only uses as the last resort
REPAIR_REBUILD
This option does not elevate the data loss. For example, if a non-clustered index is damaged, you can be easily repaired the index by rebuilding it.

If the methods discussed above fail to resolve the problem, use third party tool SysTools SQL Recovery software.

Wednesday, 15 April 2015

Tips To Fix MS SQL Server Master Database Corruption

There are two types of databases that are supported by SQL Server: System database and User database. The system databases are used by the SQL Server, SQL Server services and functions while the user database are used to store data of user’s choice, the data for which SQL Server is being set up.
There are five types of system database: master, msdb, model, resource, and tempdb. Here, we are going to discuss the Master database of SQL Server, its importance, and options available to troubleshoot if it gets corrupted.

SQL Server Master Database: An Introduction

Master database contains the primary configuration details of SQL Server. The system database tables along with the Master DB is known as system tables that records the server parameters as well as detailed information about every user and database.

The master database gets stored in physical file called master.mdf file and its corresponding transaction logs gets saved into masterlog.ldf file. Generally, this database is stored at default location and is small in size. For this reason, sharing the drive with the SQL Server instance will not cause any performance issues. If the master database gets corrupted, system database will not start along with the user database.

How to detect that Master DB is Corrupt?

Before we actually move to the topic as how to recover a corrupt master database, we will have an idea as how to discover that the DB is actually corrupt. Here, we break a master DB to exemplify its consequences.

Let us have a hypothetical scenario that the enterprise confronted a power surge and when the server was rebooted, it failed to start with error registered in the event log.

How to Fix Corrupt Master Database?

Here we are going to discuss three options to resolve MS SQL Server master database corruption issue. Any of them is adopted according to convenience and availability of resources for their proper execution.

#1: Rebuild the master Database

To repair the system database of including master DB, run SQL Server setup.exe. Open Command Prompt and move to \servers folder. The syntax for rebuilding the database in SQL 2005, the syntax used is:
Start /wait setup.exe /qn INSTANCENAME=<InstanceName> REINSTALL=SQL_Engine REBUILDDATABASE=1 sapwd=<NewStrongPassword>

Note: The parameter “qn” will suppress the dialog boxes and errors while processing the command.
Since the model, msdb, and master database reside on same disk, it is likely that a disk failure may lead to loss of all three system databases, The process mentioned above will help to rebuild all three DB together.

#2: Restore Database from Backup

To restore master database, it is necessary to have full backup of the master.mdf and master.ldf file available. Before starting up the restoration process, it is important to start SQL in single user mode. For this, follow the steps mentioned below:


1. Open “SQL Server Configuration Manager” and then click on “SQL Server 2005 Services”.
2. Now, select the SQL Server instance, right-click on it and choose “Properties”.


3. On the window that gets opened, click on “Advanced” tab. For Startup Parameters box, prefix the parameter “-m;” before already existing parameters.


Make sure that the added parameter is removed one the required task is done. Once this is done, you can connect to SQL Server using “sqlcmd” and run following command to restore the database:

RESTORE DATABASE master FROM DISK = ‘C:\BackupLocation\master.bak’ WITH REPLACE;GO


#3: Recover Master Database .mdf File

Another option that can be adopted to start up SQL Server in case of master database corruption is MDF Recovery tool that helps to restore SQL Server Master Database into healthy form. There are solutions that give the benefit of recovering system databases (including master.mdf) database. The best part about these software applications is they give a solution repair system database for any SQL Server which means even if you upgrade or degrade SQL edition, they will be with you as helpful DPR plan.

Rebuild, Recover, or Restore: Which is the Best Option? 

In our day to day life, we use our mobile phone that stores so many contacts in it. This saves us from memorizing them and of course do not demand to write all these contacts. If the cell phone is lost, you can get back to normalcy only when you get a new phone. That means you have to manually add all the contacts to the phone because the contact details are lost with the old phone.

A master database plays the same role in SQL Server as contacts in the mobile phone. If you lose access to it, all information needed to start up the day-to-day functioning will be lost. Rebuilding master database is like getting a new phone where you have to manually add information about user-accounts (login details), the permissions assigned to them, the previous configuration details and much more. On the contrary, if the database is restored using backup, it is always positive point DB will be received in the same state as it was before.

Conclusion:

The master database stores the all the metadata about the SQL database be it configuration details, login details, file location, information about pointers and much more. With the master database in corrupt or inconsistent state, it is not possible to start SQL Server and run any query. The above mentioned solutions are tried-and-tested for their successful master database recovery and can be adopted.