Friday, 6 September 2013

Tools & Tech – Add / Delete a SQL database

Let's carry on our SQL Server training. Now that we have SQL Server and SQL Server Management Studio (SSMS) installed on our system, we need to add a database and some tables to work with. That can either be done via the user interface or by running a set of SQL queries. Though the aim of this exercise is to practice writing SQL queries for our testing, we’ll look at both ways as it will be quicker to get set up.

Adding a Database
Log-into your SQL server via SSMS and open the Object Explorer window (‘View > Object Explorer’ or hit F8). Here’s the steps to follow:

  •       Right click on ‘Databases’ then
  •     Select ‘New Database…’ to open the New Database window
  •     In ‘Database name’ enter the name of your database, something like TestDB001
  •      Click [OK]
That's it, as we’re not changing any settings for the DB. If you now expand ‘Databases‘ in  the Object Explorer window you’ll see your new DB has been created.

To do this using a SQL statement is even easier. On the menu bar hit ‘New Query’ to open the Query window. In the query window type:

create database TestDB002 

and hit F5 to run the query.

If you don’t see your new database, right click on ‘Databases’ in the Object Explorer window and in the menu that appears click ‘Refresh’.

Deleting a Database
If you want to remove the database you just created it’s straight forward via the GUI. Just right click on the name of your database in the Object Explorer window and in the menu that appears, click ‘Delete’. This opens a Delete Object window that will have your database listed. Simply click [OK] and your database will be deleted.

To do this via a SQL query, go to the SQL Query window we opened earlier and type the following:

drop database TestDB002

That will then remove the database named TestDB002.

For a full walkthrough have a watch of the short video - linked to above!