What is the use of mdf and ldf in sql? - sql-server

I am very new to databases. My friend has shared the following files
company_db.sql
company.mdf
company.ldf
Now I executed the sql file & on successful execution of the command, all the tables, relations are installed on my local machine.
Now definition wise I know what are the .mdf and .ldf files, but I am confused that
What is the use of .mdf and .ldf files in my scenario?
What should I do with these files?

What is the use of mdf and ldf in sql?
Nothing. SQL - the language - has no use of those. SQL Server - the product has, and they are file extensions. Be more exact with how you communicate.
Now definition wise what is mdf and ldf file,
The documentation - which at one point is osmething you may consider reading before asking pointy questions - will tell you that are data and log files.
What is the use of mdf and ldf files?
Let me clarify that - what do you think a database does? Hint: it stored data. For that it needs some place to store bit, which is why all databases interact somehow with storage. SQL Server uses normal files for that (some others use special partitions without a computer visible file system).
MDF (and NDF) store the data, LDF store changes. Why this is needed is a little on the "learn what a database is" layer and has to do with disaster and performance scenarios.

MDF stands for "Master Database File". While you are creating any database that time two extension will create automatically.
The .MDF contains all the main information about the database that are part of SQL Server. Between .MDF file you will get table, function, stored procedure, views and etc.
LDF stands for "Log Database File". The .LDF stores changes related to inserts, deletion, updates, addition, etc. Transaction logs kept in the server help in identifying activities related to unauthorized changes as well as where an error is originating. Log information can sometimes come handy in fixing errors, recovering important data, and identifying anomalies.

Related

How do I see inside of a SQL Server database file

I'm curious what inside of a SQL Server database (a .mdf file) since it has been 4 gb and how do I open it to find something interesting in it?
Extracted from data-recovery-solutions:
MDF files are the primary database files of SQL database server and
they are saved as .MDF extension. Being the primary database, MDF
files store all the user database and other database objects like
tables, keys, indices, views, functions, columns, triggers etc. Also,
these files include all the startup info of the SQL Server database.
In an MS SQL Server, each database contains one primary MDF file.
It is a binary kind of file which can’t be opened normally. It is the
main database file that can be attached to a running SQL Server to
access the database using SQL transaction Statements. But, these MDF
files are prone to corruption. Even a smaller corruption in MDF files
makes your entire database contents inaccessible.
See here for a MDF viewer

how to copy a database in SQL server management studios

I have a database in SQL server management studio named wardobsliveisle. I want to create another database in SQL server management studio that is the exact same copy of wardobsliveisle but I want to call it demosql. How do I do this?
You have database wardobsliveisle in SSMS and now you want to create an other database demosql from wardobsliveisle. The process given below creates a copy of wardobsliveisle with new name demosql. This new database has now separate .ldf and .mdf files and it is absolutely a separate copy of your original database.
Follow the steps
Right click on Databases in your Object Explorer in SSMS.
Select Restore Files and Filegroups..., a wizard opens
Type a new database name demosql in To Database box and select source
database (Not a backup file) from From Database dropdown.
Click OK button
It will take a few minutes or less.
Taken from MSDN:
General Steps for Using Backup and Restore to Copy a Database
When you use backup and restore to copy a database to another instance of SQL Server, the source and destination computers can be any platform on which SQL Server runs.
The general steps are:
Back up the source database, which can reside on an instance of SQL Server 2005 or later. The computer on which this instance of SQL Server is running is the source computer.
On the computer to which you want to copy the database (the destination computer), connect to the instance of SQL Server on which you plan to restore the database. If needed, on the destination server instance, create the same backup devices as used to the backup of the source databases.
Restore the backup of the source database on the destination computer. Restoring the database automatically creates all of the database files.
The following topics address additional considerations that may affect this process.
Before You Restore Database Files
Restoring a database automatically creates the database files that are needed by the restoring database. By default, the files that are created by SQL Server during the restoration process use the same names and paths as the backup files from the original database on the source computer.
Optionally, when restoring the database, you can specify the device mapping, file names, or path for the restoring database. This might be necessary in the following situations:
The directory structure or drive mapping used by the database on the original computer not exist on the other computer. For example, perhaps the backup contains a file that would be restored to drive E by default, but the destination computer lacks a drive E.
The target location might have insufficient space.
You are reusing a database name that exists on the restore destination and any of its files is named the same as a database file in the backup set, one of the following occurs:
If the existing database file can be overwritten, it will be overwritten (this would not affect a file that belongs to a different database name).
If the existing file cannot be overwritten, a restore error would occur.
To avoid errors and unintended consequences, before the restore operation, you can use the backupfile history table to find out the database and log files in the backup you plan to restore.
Moving the Database Files
If the files within the database backup cannot be restored onto the destination computer because of the reasons mentioned earlier, it is necessary to move the files to a new location while they are being restored. For example:
You want to restore a database from backups created in the default location of the earlier version.
It may be necessary to restore some of the database files in the backup to a different drive because of capacity considerations. This is likely to be a common occurrence because most computers within an organization do not have the same number and size of disk drives or identical software configurations.
It may be necessary to create a copy of an existing database on the same computer for testing purposes. In this case, the database files for the original database already exist, so different file names need to be specified when the database copy is created during the restore operation.
For more information, see "To restore files and filegroups to a new location," later in this topic.
Changing the Database Name
The name of the database can be changed as it is restored to the destination computer, without having to restore the database first and then change the name manually. For example, it may be necessary to change the database name from Sales to SalesCopy to indicate that this is a copy of a database.
The database name that is explicitly supplied when you restore a database is used automatically as the new database name. Because the database name does not already exist, a new one is created by using the files in the backup.

how to backup of specific tables from database sql server and save .bak file in computer using vb.net code

I want to backup some specific data of SQL database by using vb.net code. In fact i want to save this file as .BAK file and then i want to restore it.
I couldn't find anything. all the solutions in the web was about full backup of database and there was not any method to backup some tables via code.
SQL Server does not permit backing up a single table. Full stop. No provision is made for this whatsoever.
It does permit restoring a single table from a complete backup, but this does not seem to be what you want.

Is it possible to read the database name from a mdf file that is not attached to a sql server

I have a batch file I am running to shrink a large number of detached database log files. I first attach the database with a "default_database_name", then shrink the database, then detach it again and move onto the next database.
This accomplishes shrinking all my log files, however, then all my databases end up having the same logical database name. I am trying to avoid this.
Any ideas?
You can see this information, but you will have to use a hex editor to see it, for the databases I tested with I opened the MDF with a hex editor (HxD was the one I used) and the database name was visible at around offset 00000E20-00000E30:

sql server 2005 attach database broken

I need to reproduce a bug, and a guy from the other team has sent me a .mdf and .ldf files from his sql server 2005 instance. When I attach the database, all I get is empty tables, even though file is 2 mb large. The db contains 2 tables that have, among other thing, a varbinary(max) field. At the same time another database, which has no varbinaries in tables, is attached ok and data are in place. What could be possible reason why data became inaccessible?
Perhaps he sent you a database with empty tables.
at 2MB, the file is probably legitimately empty. There is no blob field exclusions that I know of with SQL backups.
Make sure the attached files have the correct security for SQL Server to access them and also that you have the authority to read the attached DB within SQL Server itself.

Resources