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

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

5<br />

Creating and<br />

Altering Tables<br />

Every time I teach the T-<strong>SQL</strong> code for creating databases, tables, keys, and constraints, I am asked<br />

the same question, “Can’t you just do this in the GUI tool?” The answer is an unequivocal “Yes!”<br />

Therefore, the next question usually follows quite shortly behind, “Then why are we spending all<br />

this time learning stuff I’ll never use?” The answer is just as unequivocal — you will use the regular<br />

syntax on a quasi-regular basis. The reality is you probably won’t actually write the code from scratch<br />

that often, but you’ll verify and edit it on the majority of all larger database projects you work on —<br />

that means that you had better know how it works.<br />

In this chapter, we will be studying the syntax for creating your own tables. We will also take a<br />

look at how to make use of the <strong>SQL</strong> Management Studio to help us with this (after we know how<br />

to do it for ourselves).<br />

However, before we get too deep in the actual statements that create tables and other objects, we<br />

need to digress far enough to deal with the convention for a fully qualified object name, and, to a<br />

lesser extent, object ownership.<br />

Object Names in <strong>SQL</strong> Ser ver<br />

In all the queries that we’ve been performing so far in this book, you’ve seen simple naming at work.<br />

I’ve had you switch the active database in the Query Analyzer before running any queries and that<br />

has helped your queries to work. How? Well, <strong>SQL</strong> <strong>Server</strong> looks at only a very narrow scope when<br />

trying to identify and locate the objects you name in your queries and other statements. For example,<br />

we’ve been providing only the names of tables without any additional information, but there are<br />

actually four levels in the naming convention for any <strong>SQL</strong> <strong>Server</strong> table (and any other <strong>SQL</strong> <strong>Server</strong><br />

object for that matter). A fully qualified name is as follows:<br />

[<strong>Server</strong>Name.[DatabaseName.[SchemaName.]]]ObjectName

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

Saved successfully!

Ooh no, something went wrong!