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.

Column_name Type Computed Length Prec Scale Nullable<br />

HireDate smalldatetime no 4 no<br />

TerminationDate smalldatetime no 4 yes<br />

ManagerEmpID int no 4 10 0 no<br />

Department varchar no 25 no<br />

PreviousEmployer varchar no 30 yes<br />

DateOfBirth datetime no 8 yes<br />

LastRaiseDate datetime no 8 no<br />

As you can see, all of our columns have been added. The thing to note, however, is that they all went to<br />

the end of the column list. There is no way to add a column to a specific location in <strong>SQL</strong> <strong>Server</strong>. If you<br />

want to move a column to the middle, you need to create a completely new table (with a different name),<br />

copy the data over to the new table, DROP the existing table, and then rename the new one.<br />

This issue of moving columns around can get very sticky indeed. Even some of the tools that are supposed<br />

to automate this often have problems with it. Why? Well, any foreign key constraints you have<br />

that reference this table must first be dropped before you are allowed to delete the current version of the<br />

table. That means that you have to drop all your foreign keys, make the changes, and then add all your<br />

foreign keys back. It doesn’t end there, however; any indexes you have defined on the old table are automatically<br />

dropped when you drop the existing table — that means that you must remember to re-create<br />

your indexes as part of the build script to create your new version of the table — yuck!<br />

But wait! There’s more! While we haven’t really looked at views yet, I feel compelled to make a reference<br />

here to what happens to your views when you add a column. You should be aware that, even if your<br />

view is built using a SELECT * as its base statement, your new column will not appear in your view<br />

until you rebuild the view. Column names in views are resolved at the time the view is created for performance<br />

reasons. That means any views that have already been created when you add your columns<br />

have already resolved using the previous column list — you must either DROP and recreate the view or<br />

use an ALTER VIEW statement to rebuild it.<br />

The DROP Statement<br />

Performing a DROP is the same as deleting whatever object(s) you reference in your DROP statement. It’s<br />

very quick and easy, and the syntax is exactly the same for all of the major <strong>SQL</strong> <strong>Server</strong> objects (tables,<br />

views, sprocs, triggers, and so on). It goes like this:<br />

DROP [, ...n]<br />

Chapter 5: Creating and Altering Tables<br />

143

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

Saved successfully!

Ooh no, something went wrong!