SQL Server MDF file - sql

I am working on a website that has access data from a database (sql server). It will also be adding, updating deleting records in the database. It seems like there is an MDF file that gets created containing the database schema and all the records I guess? Once development is complete and I want to move this database to a real server then all I need to do is move this MDF file to the real server and that is it? is it that simple? or not really?
Also, I hope the MDF file is not a read-only file and can be updated/modified or is it?

Yes, the MDF data file (and the associated *.LDF transaction log file) contains all the database objects that make up your system.
And yes, you can definitely detach a database (an .MDF/.LDF file pair) from your (dev) SQL Server, copy it onto another "real" Production SQL Server, and re-attach it there, without loss of data or information.
Yes, the MDF file is modifyable - through the SQL Server commands and methods. You should never tamper with the file yourself, directly (flipping bits). Use SQL Server and its official interfaces (T-SQL, ADO.NET etc.) to work with your data.

Yes you can do that. Right click the database and click "detach". Make sure you click the drop connections box and click ok (if you don't do this some errors could occur). You can now move the mdf and ldf files.
However, there is a better way imho. Instead, create a full backup of the database, copy the backup file, and restore from the backup on the new server. Now you have two copies of the database, a development version and a production version.

You will need to take .MDF file as well as .LDF file which will have your logs. You have to take both mdf and ldf files and attach on the real server.

Related

Restoring a database from .bak file on another machine

I've not done much SQL and am still pretty new to this, so please excuse what's probably a basic question.
I've been asked to look into creating an SQL job to backup our databases, store the .baks on another machine and then to restore them to a second server. I've been doing a bit of research and playing with SSMS and have back-ed up the database to my personal machine by setting up a share and running a backup job to the share location. I'm now trying to create a new database (on the same server I back-ed up from) by restoring the .bak file (but giving the database I'm trying to create a new name and what-not) but am unable to specify restoring it from the share, like I did when backing it up/I can't find how to specify other network locations and am just browsing the server's C drive when I try to locate the file.
For now, I'm just using the built-in wizards to try and achieve this (open SSMS -> Connect to server -> right click DataBases -> Restore DataBases and then select From Device and browse to find the file).
This isn't the final process, just me trying to get to grips with how this works. As I said, the idea is to ultimately have a scheduled job to backup the DB from server1 to a .bak on, say, my personal machine and then to restore that to a DB on server2 (different network, different city) and, probably, with a series of SQL commands rather than using the wizard every time (there are a few DBs that'll, ultimately, need backing up).
My apologies for the, possibly, quite drawn out and convoluted question - essentially, all I need to know is can I/how can I restore a DB in SSMS from a .bak on a different machine?
Many thanks
You could use something like the following script. It restores a database from the filesystem, and it overwrites the existing database with the name of "MyDB", moving the files to new locations of your choice in the process.
RESTORE DATABASE
MyDB
FROM DISK = '\\MyShare\MyBackup.bak'
WITH
MOVE 'DataFile' TO 'D:\myNewDBLocation\DataFile.mdf',
MOVE 'LogFile' TO 'E:\\myNewDBLocation\LogFile.ldf'
, REPLACE
You can find out the name of the llogical files (in the above, those are called DataFile and LogFile by running the following:
RESTORE FILELISTONLY
FROM DISK = '\\MyShare\MyBackup.bak'
Additional information about various options and parameters:
RESTORE (Transact-SQL)

How to setup database in SQL Server by an unknown file?

Probably this is the lamest question ><
I am developing an ASP.net web app with SQL Server database. When the client asked me to develop the system, he provided a file for the database.
I thought the file was a dumpfile (if you familiar with oracle or mysql, you can replicate database with dumpfile, which is basically bunch of queries inside a text file).
To my horror, plus with my noobity in SQL Server, I found out that it was NOT a dumpfile. The file is rename to .bak which is not really the correct extension for SQL Server. I tried to rename it to .mdf, but no luck.
What should I do? The client is out for vacation on some island and cannot be contacted at the moment?
This is backup file, you need to restore database from backup.
How to: Restore a Database Backup (SQL Server Management Studio)
This is SQL Server backup database file. You can restore it by following these steps: http://msdn.microsoft.com/en-us/library/ms177429.aspx
Alex Aza has the right answer. But remember to create users for the database, before you restore the provided .bak file. A backup file only hold a user id -- the complete user definition is held in the server instance. Thus, it is better to create the users needed before restoring a backup from another server instance which is the case in your project.

Why is SQL Server backup so much larger than the DB files?

I have a SQL Server DB that is about 13Gb with a 1.6Gb log file. When I back it up, however, it creates a 50Gb .bak file! Why is that?
I have read some commentary here about transaction logs, but my DB runs on my laptop and there is no DB activity during the time of the backup. (It only takes 5 minutes).
My backup type is Full, my recovery type is Full, and I am doing Copy-Only backups. Any ideas? Much appreciated!
I am using SQL 2008 R2, Developer Edition.
It might be because the backup routine is appending the new backup to the same file?
There is an option in the Backup dialog in Sql Management Studio to Append or Overwrite all existing backup sets. If you have Append if will add the backup to the same .bak file and the .bak file will be larger for every backup.
In Transact Sql it is the INIT and NOINIT option which determine if the bak file is overwritten or appended.
In my case, I found that the 500MB database also had a 560MB full text catalogue attached which gets included in the backup file. I thought I'd add this despite another answer having been accepted in case anyone else is still stumped :)
In my case, heavy use of filestream objects made for the largest part of the backup file.

SQL Server database recovery

I simply copied my .mdf/.ldf files to another file and suddenly my Microsoft SQL 2008 database dies. I can't open these files on another system either. Can anyone explain what's happened? Any way to recover this application's database now?
Did you stop the service or detach the files first?
You are not supposed to copy files when the server is online.
if you want to copy when the server is online, you can right click on management studio , -> detach->
then copy the files to another instance , and choose attach on new server. remember you need to know the location of the files, before you detach, also you should be knowing, also the logins need to transfer.
Have you tried moving the database files back to where they were? (Specifically, stop SQL server, move files, start SQL Server).
Whenever I do this kind of work, I make copies of the files, and rename the original ones (from, say, MyData.mdf to xMyData.mdf). Makes it easier to back out when my fatfingers take over.

How to copy a SQL 2005 database to another computer?

I'm trying to rebuild a web server in a virtual pc. Installed required software and Microsoft SQL Server 2005. I've had full backups of my databases but it is not possible to restore from those backups in this new installation. By the way, backups are fine, i can restore from them on original server.
In summary, how can I copy whole database to another computer? How can I backup a database and restore is fully on another computer?
Regards,
Burak
PS: The database I'm trying to restore is called "Some_db" and it does not exist on new server. I also tried to create a new one with defaults and restore on it but that gave an error on new db. I don't know any details of the database.
You have 3 Choises:
1) Restore DB as you tried
This failed for you for some reason
Here a tutorial: http://msdn.microsoft.com/en-us/library/ms177429.aspx
2) Detach DB and Atach it to the new server
This is easy: Rightclick on the DB -> Detach.
Rightclick on the new Server -> Attach
Tutorial: http://www.databasedesign-resource.com/moving-the-database.html
3) Create Create-Scripts of the table schema and use insert into statements for the data.
For this, there are a bunch of tools, my favourit is "Redgate SQL Compare".
It creates you all nessecary scripts.
The link: http://www.red-gate.com/products/SQL_Compare/index.htm
Backup file
Copy .bak file to other server
Restore .bak file.
Works every time for me. You have to make sure the new instance of SQL is of a sufficient level to be able to host the database. You also have to recreate the users on the new server and remove and readd them to each database.
The alternative to the backup and restore approach is to detach the db, copy the mdf and ldf files over and then attach them on the new server
Or take offline, copy files, attach database, bring online..
open your current query browser window and run the query .The restore will happen automatically
BACKUP DATABASE AdventureWorks
TO DISK = 'C:\Backup\AdventureWorks.bak'
GO
for more details
This will create a .bak file after that copy the .bak to server.
restore the data base by right click on DataBase .Select restore database .Give the database name and location .And restore it .