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.

Reviewing the Defaults<br />

So let’s look one last time at how the defaults work at each level of the naming hierarchy from right to left:<br />

❑ Object Name: There isn’t a default — you must supply an object name.<br />

❑ Ownership: You can leave this off, in which case it will resolve first using the current user’s<br />

name, and then, if the object name in question doesn’t exist with the current user as owner, then<br />

it will try the dbo as the owner.<br />

❑ Database Name: This can also be left off unless you are providing a <strong>Server</strong> Name — in which<br />

case you must provide the Database Name for <strong>SQL</strong> <strong>Server</strong>s (other server types vary depending<br />

on the specific kind of server).<br />

❑ <strong>Server</strong> Name: You can provide the name of a linked server here, but most of the time you’ll just<br />

leave this off, which will cause <strong>SQL</strong> <strong>Server</strong> to default to the server you are logged into.<br />

If you want to skip the schema name, but still provide information regarding the database or server, then<br />

you must still provide the extra “.” for the position where the owner would be. For example, if we are logged<br />

in using the AdventureWorks<strong>2008</strong> database on our local server, but want to refer to the Sales.Customer<br />

table in the AdventureWorks<strong>2008</strong> database on a linked server called MyOther<strong>Server</strong>, then we could<br />

refer to that table by using MyOther<strong>Server</strong>.AdventureWorks<strong>2008</strong>..Customer. Since we didn’t provide<br />

a specific schema name, it will assume that either the default schema for the user ID that is used to<br />

log on to the linked server or the dbo (in that order) is the schema of the object you want (in this case,<br />

Customer). Since the Customer table is not part of the dbo schema, the user would need to have a default<br />

schema of Sales or they would get an error that the object was not found. In general, I recommend explicitly<br />

naming the schema of the object you want to reference.<br />

The CREATE Statement<br />

In the Bible, God said, “Let there be light!” And there was light! Unfortunately, creating things isn’t quite<br />

as simple for us mere mortals. We need to provide a well-defined syntax in order to create the objects in<br />

our database. To do that, we make use of the CREATE statement.<br />

Let’s look at the full structure of a CREATE statement, starting with the utmost in generality. You’ll find<br />

that all the CREATE statements start out the same, and then get into the specifics. The first part of the CREATE<br />

statement will always look like:<br />

CREATE <br />

This will be followed by the details that vary by the nature of the object that you’re creating.<br />

CREATE DATABASE<br />

For this part of things, we’ll need to create a database called Accounting that we will also use when we<br />

start to create tables. The most basic syntax for the CREATE DATABASE statement looks like this:<br />

CREATE DATABASE <br />

Chapter 5: Creating and Altering Tables<br />

117

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

Saved successfully!

Ooh no, something went wrong!