• Aucun résultat trouvé

Prebuilt Databases Within SQL Server

Dans le document SQL Server 2008 (Page 79-82)

Several databases are installed and displayed when SQL Server is first installed. This section explores each of these databases, so that you’ll know what each does and feel comfortable when you come across them outside of this book.

Let’s first look at the most important database in SQL Server: the master database. We’ll then cover the tempdb, model, msdb, and AdventureWorks/AdventureWorksDW databases.

master

master is the most important database in SQL Server, so I must start off with a warning:

Directly alter this database at your own peril!

There should be no reason to go into any of the system views within this database and alter the records or column information directly. There are system functions that allow a constructive alter-ation of any of the data in an orderly fashion, and these are the only approaches you should use to alter the master database.

54 C H A P T E R 3 ■ D A T A B A S E D E S I G N A N D C R E A T I O N

The master database is at the heart of SQL Server, and if it should become corrupted, there is a very good chance that SQL Server will not work correctly. The master database contains the following crucial information:

• All logins, or roles, that the user IDs belong to

• Every system configuration setting (e.g., data sorting information, security implementation, default language)

• The names of and information about the databases within the server

• The location of databases

• How SQL Server is initialized

• Specific system tables holding the following information (this list is not exhaustive):

• How the cache is used

• Which character sets are available

• A list of the available languages

• System error and warning messages

• Special SQL Server objects called assemblies (tables within every database that deal with SQL Server objects and therefore are not specific to the master database)

The master database is the security guard of SQL Server, and it uses the preceding information to ensure that everything is kept in check.

Note

It is crucial that you take a regular backup of the master database. Ensure that doing so is part of your backup strategy. Backups are covered in more detail in Chapter 7.

tempdb

The tempdb database is—as its name suggests—a temporary database whose lifetime is the duration of a SQL Server session; once SQL Server stops, the tempdb database is lost. When SQL Server starts up again, the tempdb database is re-created, fresh and new, and ready for use. There is more to this process, but before we delve into that, you first need to know what the tempdb database is used for.

A database can hold data, and that data can be held in many tables. You use commands and functions to retrieve and manipulate that data. However, there may be times when you wish to temporarily store a certain set of data for processing at a later time—for example, when you pass data from one stored procedure to another that is going to run right after the first one. One option is to store that data within a table within the tempdb database. Such a table could be thought of as a

“permanent” temporary table, as it will exist until it is specifically deleted or until tempdb is re-created on a SQL Server restart. Another type of temporary table that can exist is a temporary table created within a stored procedure or query. This type of table will also be placed within the tempdb database, although its lifetime will only last until all users of the table finish. We’ll look at both of these types of tables in Chapter 11 when we examine advanced T-SQL. Both of these scenarios are fine, as long as the tempdb database is not refreshed. If it is, then your data will be gone, and you will need to rebuild it.

You may be thinking that this is not an ideal solution. After all, wouldn’t it be wonderful if temporary information could be stored somewhere outside of the database? Well, that’s not really where tempdb would be used. It really should be thought of only as transitional storage space.

Dewson_958-7C03.fm Page 54 Tuesday, July 1, 2008 5:16 PM

C H A P T E R 3 ■ D A T A B A S E D E S I G N A N D C R E A T I O N 55

Another reason tempdb is refreshed is that not only is it available for a developer to use, but also SQL Server itself uses tempdb. Actually, SQL Server uses tempdb all the time, and when you reinitialize SQL Server, it will want to know that any temporary work it was dealing with is cleaned out. After all, there could have been a problem with this temporary work that caused you to restart the service in the first place.

Being just like any other database, tempdb has size restrictions, and you must ensure that it is big enough to cope with your applications and any temporary information stored within it. As you read through the next sections, you will see that a database has a minimum and a maximum size. tempdb is no exception to this, and you should ensure that its settings provide for expansion so it can grow as required.

Caution

Because tempdb has a limited size, you must take care when you use it that it doesn’t get filled with records in tables from rogue procedures that indefinitely create tables with too many records. If this were to happen, not only would your process stop working, but also the whole server could stop functioning and therefore impact everyone on that server!

As indicated in the first paragraph of this section, there’s more to say about tempdb’s refresh process, which we’ll examine in the next section.

model

Whenever you create a database, as you’ll do shortly in this chapter, it has to be modeled on a predefined set of criteria. For example, if you want all your databases to have a specific initial size or to have a specific set of information, you would place this information into the model database, which acts as a template database for further databases. If you want all databases to have a specific table within them, for example, then you would put this table in the model database.

The model database is used as the basis of the tempdb database. Thus, you need to think ahead and take some care if you decide to alter the model database, as any changes will be mirrored within the tempdb database.

msdb

msdb is another crucial database within SQL Server, as it provides the necessary information to run jobs to SQL Server Agent.

SQL Server Agent is a Windows service in SQL Server that runs any scheduled jobs that you set up (e.g., jobs that contain backup processing). A job is a process defined in SQL Server that runs auto-matically without any manual intervention to start it.

As with tempdb and model, you should not directly amend this database, and there is no real need to do so. Many other processes use msdb. For example, when you create a backup or perform a restore, msdb is used to store information about these tasks.

AdventureWorks/AdventureWorksDW

AdventureWorks and AdventureWorksDW are the example databases found in SQL Server if you selected to install them during setup. These databases are based on a manufacturing company that produces bicycles. They exemplify the new features in SQL Server 2008 as well as demonstrate the features that arrived in SQL Server 2005, such as Reporting Services, CLR functionality, and many others, in a simple, easy-to-follow way.

56 C H A P T E R 3 ■ D A T A B A S E D E S I G N A N D C R E A T I O N

The following excerpt from the Microsoft documentation provides a concise overview of what the AdventureWorks databases are about:

Adventure Works Cycles, the fictitious company on which the AdventureWorks sample data-bases are based, is a large, multinational manufacturing company. The company manufactures and sells metal and composite bicycles to North American, European, and Asian commercial markets. While its base operation is located in Bothell, Washington with 290 employees, several regional sales teams are located throughout their market base.

The example databases are not meant for novice SQL Server developers, although you’ll have no problems with them after you learn the basics of SQL Server.

Now that you know what databases are in SQL Server, let’s start building one! We’ll start by deciding what type of database to create, depending on what we’ll use it for.

Dans le document SQL Server 2008 (Page 79-82)