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 />

Now, let’s say that there is also a user with a login name of Fred. Fred is the database owner (as opposed<br />

to just any member of db_owner). If Fred creates a table called MyTable using an identical CREATE<br />

statement to that used by MySchema, the two-part table name will be dbo.MyTable. In addition, as dbo<br />

also happens to be the default owner, any user could just refer to the table as MyTable.<br />

It’s worth pointing out that members of the sysadmin role (including the sa login) always alias to the dbo.<br />

That is, no matter who actually owns the database, a member of sysadmin will always have full access<br />

as if it were the dbo, and any objects created by a member of sysadmin will, unless explicitly defined<br />

otherwise, show ownership belonging to the dbo. In contrast, objects created by members of the db_owner<br />

database role do not default to dbo as the default schema — they will be assigned to whatever that particular<br />

user has set as the default schema (it could be anything). Weird but true!<br />

In chats I had with a few old friends at <strong>Microsoft</strong>, they seemed to be somewhat on the schema bandwagon<br />

and happy for the changes. I too am happy for the changes, but mostly because they make the use of<br />

schemas easier, not because schemas are a feature that I think everyone should rush to use.<br />

The addition of schemas adds complexity to your database no matter what you do. While they can address<br />

organizational problems in your design, those problems can usually be dealt with in other ways that<br />

produce a much more user-friendly database. In addition, schemas, while an ANSI/ISO-compliant notion,<br />

are not supported in the same way across every major RDBMS product. This means using schemas is<br />

going to have an impact on you if you’re trying to write code that can support multiple platforms.<br />

The Database Name<br />

The next item in the fully qualified naming convention is the database name. Sometimes you want to<br />

retrieve data from a database other than the default, or current, database. Indeed, you may actually want<br />

to JOIN data from across databases. A database-qualified name gives you that ability. For example, if you<br />

were logged in with AdventureWorks<strong>2008</strong> as your current database, and you wanted to refer to the Orders<br />

table in the Accounting database we’ll be building later in the chapter, then you could refer to it by<br />

Accounting.dbo.Orders. Since dbo is the default schema, you could also use Accounting..Orders.<br />

If a schema named MySchema owns a table named MyTable in MyDatabase, then you could refer to that<br />

table as MyDatabase.MySchema.MyTable. Remember that the current database (as determined by the<br />

USE command or in the drop-down box if you’re using the <strong>SQL</strong> <strong>Server</strong> Management Studio) is always<br />

the default, so, if you want data from only the current database, then you do not need to include the<br />

database name in your fully qualified name.<br />

Naming by <strong>Server</strong><br />

116<br />

In addition to naming other databases on the server you’re connected to, you can also “link” to another<br />

server. Linked servers give you the capability to perform a JOIN across multiple servers — even different<br />

types of servers (<strong>SQL</strong> <strong>Server</strong>, Oracle, DB2, Access — just about anything with an OLE DB provider).<br />

We’ll see a bit more about linked servers later in the book, but for now, just realize that there is one more<br />

level in our naming hierarchy, that it lets you access different servers, and that it works pretty much like<br />

the database and ownership levels work.<br />

Now, let’s just add to our previous example. If we want to retrieve information from a server we have<br />

created a link with called My<strong>Server</strong>, a database called MyDatabase, and a table called MyTable owned<br />

by MySchema, then the fully qualified name would be My<strong>Server</strong>.MyDatabase.MySchema.MyTable.

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

Saved successfully!

Ooh no, something went wrong!