17.06.2013 Views

Beginning Microsoft SQL Server 2008 ... - S3 Tech Training

Beginning Microsoft SQL Server 2008 ... - S3 Tech Training

Beginning Microsoft SQL Server 2008 ... - S3 Tech Training

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 5: Creating and Altering Tables<br />

Actually, this is about as simple as <strong>SQL</strong> statements get. We could drop both of our tables at the same<br />

time if we wanted:<br />

USE Accounting<br />

DROP TABLE Customers, Employees<br />

And this deletes them both.<br />

Be very careful with this command. There is no, “Are you sure?” kind of question<br />

that goes with this — it just assumes you know what you’re doing and deletes the<br />

object(s) in question.<br />

The syntax is very much the same for dropping the entire database. Now let’s drop the Accounting<br />

database:<br />

USE master<br />

DROP DATABASE Accounting<br />

You should see the following in the Results pane:<br />

Command(s) completed successfully.<br />

You may run into a situation where you get an error that says that the database cannot be deleted<br />

because it is in use. If this happens, check a couple of things:<br />

❑ Make sure that the database that you have as current in the Management Studio is something<br />

other than the database you’re trying to drop (that is, make sure you’re not using the database<br />

as you’re trying to drop it).<br />

❑ Ensure you don’t have any other connections open (using the Management Studio or sp_who)<br />

that are showing the database you’re trying to drop as the current database.<br />

I usually solve the first one just as I did in the code example — I switch to using the master database.<br />

The second you have to check manually — I usually close other sessions down entirely just to be sure.<br />

Using the GUI T ool<br />

144<br />

We’ve just spent a lot of time pounding in perfect syntax for creating a database and a couple of tables —<br />

that’s enough of that for a while. Let’s take a look at the graphical tool in the Management Studio that<br />

allows us to build and relate tables. From this point on, we’ll not only be dealing with code, but with the<br />

tool that can generate much of that code for us.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!