how to backup SQL database (tables, mappings, indices, etc.) - sql

I am installing a service pack on our shopping cart. They recommend backing up the SQL database before installing. I know we have backups to tape drives done by our hosting company, but I want one I know the exact time stamp for and can access quickly if I need to reload it because of a goof during the upgrade. (I don't want to have the store down for any longer than needed.)
How do you recommend backing up a SQL database for easy reloading for someone who is used to just writing queries and stored procedures? (I'd like to get everything - mappings & indices, etc - because I wouldn't know what all of them are or how to recreate them.)
I access the database via Remote Desktop and can link my hard drive and DVD drives, if that helps. It's MSSQL 2008.
Thank you so much.
Best wishes,
Andrea

BACKUP DATABASE databasename TO DISK='C:\somefile.bak' WITH COPY_ONLY, INIT, FORMAT, CHECKSUM
Obviosly replace databasename and the target C:\somefile.bak as appropriate. Remember, the file and path is on the server; connecting remotely won't change where backup file is stored--in other words it won't be on your local machine.
You can omit the WITH options if you want. Drop INIT if you don't want the target .bak file overwritten. COPY_ONLY isn't a big deal either way in your case. CHECKSUM is just for validating the data before it gets backed up, and may not matter if you don't have CHECKSUMs turned on for the database--though by default starting in MS SQL 2005 new databases were.
The MS documentation for BACKUP and RESTORE isn't too difficult to understand in its basic forms. You can also use the Management Studio Tasks->Back Up or Tasks->Restore GUI if you have access to it.

Related

How to unlock a Database.mdf file so can share with friends?

Does anyone know how to unlock a SQL Server .mdf file so can share with friends?
Because the file that my friend share with me for our group assignment happen to be locked up and that I can't use the data within and only can view it.
Actually I want to share my database with my partner in this group assignment. We use a similar database. However, due to reason of time meet up with our instructor, we both need to have our own holding of a database for presentation at different time.
Also the reason, I want to learn to unlock it is because, we may need to try in on different hardware as well. Is in by Exporting? Like what is shown in this link video? https://www.youtube.com/watch?v=c2XmaCFk-kk
Firstly, to be able to use .mdf/.ldf files in common operations like Copy/Paste you need:
1. Under Sql Server Management Studio right click on the DB:
2. Then under Detach window check 'Delete Connexions' and click Ok:
You're free to do whatever you want with your .mdf/.ldf files
You probably shouldn't be sharing the MDF files, but if that's what you want to do you will indeed have to detach the database and transfer MDF and LDF files before attaching it on the other side.
It's not completely clear what you want to accomplish, but I think your best option is to take a backup on your machine, transfer the backup to the other machine and restore it there, that way the database doesn't have to go offline.
See this link for a how-to on the backup/restore method.
See this link for instructions on the detach/attach method
Share SQL connection, not the MDF file. Use SQL Surface to enable network access to your DB and define user logins.

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 can I move data from one SQL Server to other?

I want to copy all my database to remote database server. I have access to server using SQL server management studio.
How can I do this? I have created script with data of old database and tried to run it on new server but it is taking too much time and returns different errors like duplicate key etc.
If this is a one time deal, you should download Redgate's SQL tools. They can handle this easily and are free to test for 14 days I believe. (http://www.red-gate.com) You might even find they are worth the purchase as I have. The tools you would use are SQL Compare (to copy structure) and SQL DAta Compare to actually copy the data.
Assuming that you can restore databases to your remote SQL server, you can use SQL Server backup and restore? They're available in SQL Server Management Studio. See http://msdn.microsoft.com/en-us/library/ms187510.aspx. It's very easy to use.
Other than that, you can try copying the database MDF and LDF files from your local database onto your remote database filesystem, then attaching to the MDF file on your remote database.
Those are probably the fastest ways that you can copy and entire database to a remote location that I can think of.
Try Database publishing wizard by Microsoft. Good enough for no very big data amounts.

Open databases from SQL Server like you do it with MySQL with PhpMyAdmin

i have a backup from a data base (SQL Server), i never worked with this type (always with MySQL and PhpMyAdmin to see the data). How can i see the data who is inside the backup? there is a Microsoft program to do that? like access.
Using SQL Server Management Studio, you use Restore feature. The process is laid out in the MSDN documentation.
Obviously, you restore to another SQL Server database instance. The version of the instance can be newer than the version the backup came from - the compatibility level will just be set accordingly.
depends whether you want to pay for the solution ;)
If you have the disk space and don't want to pay then you have to restore the backup.
If you dont have the disk space, and/or don't mind paying and/or you only want to restore a certain subset of the data you can use quest software's litespeed http://www.quest.com/litespeed-for-sql-server

Modifying SQL Database on Shared Hosting

I have a live database on a shared hosting server. I am making some major changes to my site's code and I would like to fix some stupid mistakes I made in initially designing the database. These changes involve altering the size of a large number of fields, and enforcing referential integrity between tables properly. I would like to make the changes on both my local test server and the remote server if possible.
I should note that while I'm fairly comfortable with writing complex queries to handle data, I have very little experience modifying database structure without a graphical interface.
I can access the remote database in the visual studio database explorer but I can not use that for anything other than data manipulation. I installed Sql Management Studio express last night and after 40+ crashes I gave up - I couldn't even patch the damn thing.
The remote server is SQL 2005 / The MyLittleAdmin web interface is available.
So my question is what is the best way to accomplish these changes. Is there a graphical interface I can use on the remote server? If not is there an easy way to copy the database to my local machine, fix it, and re upload? Finally if none of the above are viable does anyone have links to a decent info on fixing referential integrity via query?
Sorry for the somewhat general question - I feel like I am making this far harder than it should be but after searching / trying all night i haven't gotten anywhere. Thanks in advance for the help. I really appreciate it.
...Also does anyone have a time machine I can borrow- I need to go kick my past self's ass for this.
Usually hosting providers allow you to backup and restore your database, so the easiest way to accomplish the move is to backup your live DB, download the backup file, restore it locally, do all the changes, do a backup of the local db, upload it, then restore it in the live service. Your site should be placed on an administrative shutdown during this time so it does not continue to update the data while you're doing this operations. You have to make sure your local SQL instance is exactly at the same build version (##version) like the hosting provider, otherwise your local SQL may upgrade the database structure and you'll be unable to restore it back on the hosting provider (or you'll be unable to restore in on your local server if your version is earlier than the host's). The MSDN BOL has a detailed guid on how to Copy Databases using Backup/Restore.
An alternative to backup/restore is to detach/attach the database, but I do not recommend this because you need to move both the MDF and the LDF in sync, and they're also larger in size than a backup.
This assumes you can do all the schema changes on your local copy in a wizardly manner, ie. fast and correct. Of course, that is not easy. The recommended way is to prepare in time a script that applies all the transformations needed to reach the new schema. There are tools like SQL Diff, SQL Compare, SQL Delta and other that can generate such a script. Also Visual Studio Database Edition can do this.
How I would do this would be like this:
Ensure I have exactly the same schema on my dev machine as on the live host. If not sure, I can take a backup of the live server and restore it localy. This would be my reference, v1. schema.
Keep the backup of v1. for reference
Start developing a script that changes the schema to my target. Sometimes I need to refresh my memory on script syntax myself, and what I do is I go to the SQL Server Management Studio wizards for the operation I want to do, select all the options in the UI and then select the 'show script options', that will show me exactly the script SSMS is running to accomplish my desired change.
For each change I add to the script, I can test it by restoring the v1. reference backup I have from step 1 and running the script.
Keep iterating on the script, adding one change at a time, until all the needed schema changes are done. After each change, I can test it again like in step 4.
Yourscript should do not only DDL changes to the schema, but also any DML changes needed (modifying reference data, changing values, moving columns between tabels etc).
When the script is ready, I can download a newer backup, apply the script, and upload the updated backup and restore it on the live host. Alternatively you can simply run the script on the live host (after of course you backed it up in case something goes horribly wrong).
In my projects I always rely on scripts to deploy and upgrade the database. In fact I use the database extended properties to store a 'version' of my application deployed schema and in my code I simply roll forward all the scripts that bring the schema to my last version. I have an article on my blog describing this technique: Version Control and your Database.