Technical Script AIST3410 Database Management systems p 1 There are times when a SQL Server database needs to be preserved or restored outside of the database engine. In real life this occurs when we want to move a database or archive a database before deleting it. When using a VM for class on campus computers, we need to preserve a database copy outside the VM to restore when we begin a fresh VM (this doesn t happen if you ported the VM to your own computer since when you restart the VM in that case, you are picking up where you left off). SQL Server Management Studio provides a wizard to preserve an entire data base as a script. So you simply create the script, save the script outside the VM, and exit the VM. When you want to re install that database in another SQL Server instance, simply open and run the script. As a first step, be sure you have set up file sharing between your VM and the host computer. Use the VM menu and choose Settings Select the Options tab Shared Folders. Be sure the Always enabled radio button is selected and Map as a network drive is selected. Highlight the top folder and click the Properties button.
Technical Script AIST3410 Database Management systems p 2 Give the shared folder a name and browse to the host location where the share should save files. Click OK twice to get back to the VM. Preserving the Database In SQL Server Management Studio, right click on the database you want to preserve (it is pubs in the example), select Tasks Generate Scripts
Technical Script AIST3410 Database Management systems p 3 Click Next on the Wizard splash screen Be sure the Script Entire Database option is selected; click Next.
Technical Script AIST3410 Database Management systems p 4 You can save your script directly to a file but it can be more reassuring to see the script before saving it. So use Save scripts to a specific location and Save to a new query window but don t click Next yet click the Advanced button. To preserve the schema (i.e., database structure and any entered data), set the Types of data to script option in the General options group to schema and data. Then click Ok (to close options) and Next.
Technical Script AIST3410 Database Management systems p 5 Double check that you are ready to go by reviewing the summary; use Previous to go back and change things or use Next to create the script. Creating the script is non destructive it simply creates instructions so it is not a disaster if you find something was left out and have to re create the script. The wizard will do its work providing some feedback about what is going on. Click Finish when done.
Technical Script AIST3410 Database Management systems p 6 The script will be placed in the query window. You might look through it to build confidence it is complete (i.e., I would look at the end for data insert statements if my tables had data). With your focus in the script (i.e., click into the script area), use File Save or click the Save button. Put this script file where you can find it (e.g., on your ASU Z:\ drive by choosing the shared folders and double clicking the share name) and give it a name that will help you remember what database the script creates and when you created the script (the date is important since if you change the database and make another script you want to know the difference); PubsSep01.sql in the example.
Technical Script AIST3410 Database Management systems p 7 Restoring the Database First, be sure the script file created when preserving the database is available to the computer (e.g., the VM or other computer) where you want to restore a copy. This may mean setting up the VM share again or somehow copying the script. Use SQL Server Management Studio to connect to a SQL Server without your database (this will happen if you start a fresh VM on campus or in real life you might connect to a SQL Server instance that you are trying to move your database to). Use File Open File and find the database script. Press F5 or the Execute button and the script will run. Your database should be created within the SQL Server instance (you might need to right click on databases and choose refresh to see it).
Technical Script AIST3410 Database Management systems p 8 There are other ways to backup/restore or detach/attach databases in SQL Server. Making a script is the most portable approach and has a lot of merit because it causes the database engine to rebuild the database rather than simply accept a copy.