Chapter 3 Databases and Database Files summary

SQL Server database as having the following properties and features:

It's is a collection of many objects, such as tables,views,stored procedures, and constraints.

The technical limit is 2^31-1 objects. The number of objects typically ranges from hundreds to ten of thousands.

It's owned by single SQL Server login account.

It maintains its own set of user accounts, roles,schemas, and security.

It has its own set of system tables to hold the database catalog.

It is the primary unit of recovery and maintains logical consistency among objects within it.

It has its own transaction log and manges its own transactions.

It can span multiple disk drives and operating system files.

It can range in size from 2MB to a technical limit of 524272 terabytes.

It can grow and shrink, either automatically or manually.

It can have specific properties enabled or disabled.

And here is what a SQL Server database is not:

It is not synonymous with an entire SQL Server instance.

It is not a single SQL Server table.

It is not a specific operating system file.

System Databases

SQL Server 2008 installation always includes four databases: master,model,tempdb, and msdb.

It also contains a fifth, "hidden" database that you never see using any of the normal SQL commands that list all your databases.This database is referred to as the resource database,but its actual name is mssqlsystemresource.

Master 

The master database is composed of system tables that keep track of the server installation as a whole and all other databases that are subsequently created.Although every database has a set of system catalogs that maintain information about objects that the database contains, the master database has system catalogs that keep information about disk space,file allocations and usage,system-wide configuration settings.endpoints,login accounts, databases on the current instance, and the existence of other servers running SQL Server.

The master database is critical to your system, so always keep a current backup copy of it.Operations such as creating another database,changing configuration values, and modifying login accounts all make modifications to master,so you should always back up master after performing such actions.

model

The model database is simply a template database.Every time you create a new database, SQL Server makes a copy of model to form the basis of the new database. you can also change most properties of the model database by using the ALTER DATABASE command.

tempdb

The tempdb database is used as a workspace. It is unique among SQL Server databases because it's re-created ---not recovered----every time SQL Server is restarted.

The Resource Databases

the mssqlsystemresource database is a hidden database and is usually refered to as the resource database,Executable system objects, such as system stored procedures and functions, are stored here.Microsoft created this database to allow very fast and safe upgrades.

msdb

The msdb database is used by the SQL Server Agent service and other companion services which perform scheduled activities such as  backups and replication tasks, and the Service Broker, which provides queuing and reliable messaging for SQL server.In addition to backups,objects in msdb support jobs,alerts,log shipping,polices,database mail, and recovery of damaged pages. When you arfe not acitvely performing these activities on this datbase,you can generally ignore msdb.

Sample Databases

AdventureWorks

AdventureWorks actually comprises a family of sample databases that was created by the Microsoft User Education group as an example of what a "real" database might look like.  The family includes : AdventureWork2008,AdventureWorksDW2008,and AdventureWorksLT2008,

pubs

The pubs database is a sample database that was used extensively in earlier versions of SQL Server.

Northwind

The Northwind database is a sample database that was originally developed for use with Microsoft Office Access.

Database Files

A database file is nothing more than an operationg system file.

SQL Server 2008 allows the following three types of database files:

Primary data files

Every database has one primary data file that keeps track of all the rest of the files in the database, in addtion to storing data. By convention, a primary data file has then extension .mdf.

Secondary data files

A database can have zero or more secondary data files. By convention, a secondary data file has the extension .ndf.

Log files

Every database has at least one log file that contains the information necessary to recover all transactions in a database.

Each database file has five properties that can be specified when you create the file: a logical filename,a physical filename, an initial size, a maximum size, and a growth increment.(Filestream data files have only the logical and physical name properties.)

Creating a Database

The easiest way to create a database is to use object explorer in management studio, which provides a graphical front end to the T-SQL commands that actually create the database and set its properties.

when you create a new database,SQL Server copies the model database.If you have an object that you want created in every subsequent user database, you should create that object in model first. you can also use model to set default database options in all subsequently created databases. The model database includes 53 objects ----- 45 system tables, 6 objects used for SQL Server Query Notifications and Service Broker, 1 table used for helping to manage filestream data, and 1 table for helping to manage change tracking. You can see these objects by selecting from the system table called sys.objects.

Expanding or Shrinking a Database

Database can be expanded and shrunk automatically or manually. The mechanism for automatic expansion is completely different from the mechanism for automatic shrinkage.Manual expansion is also handled differently from manual shrinkage.

Automatic File Expansion

Expansion can happen automatically to any one of the database's files when that particular file becomes full.The file property FILEGROWTH determines how that automatic expansion happens.

Manual File Expansion

You can expand a database file manually by using the ALTER DATABASE command with the MODIFY FILE option to change the SIZE property of one or more of the files. When you alter a database, the new size of a file must be larger than the current size. To decrease the size of a file,you use the DBCC SHRINKFILE command, whcih I'll tell you about shortly.

Fast File Initialization 

SQL Server 2008 data files (but not log files) can be initialized instantaneously. This allows for fast execution of the file creation and growth.

Automatic Shrinkage

The database property autoshrink allows a database to shrink automatically. The effect is the same as doing a DBCC SHRINKDATABASE(dbname,25). This option leaves 25% free space in a database after the shrink, and any free space beyond that is returned to the operationg system.

Important Automatic shrinking is never recommanded. In fact ,Microsoft has announced that the autoshrink option will be removed in a future version of SQL Server and you should avoid using it.

Manual Shrinkage

You can shrink a database manually using one of the following DBCC commands:

DBCC SHRINKFILE ({file_name | file_id } [, target_size] [, {EMPTYFILE | NOTRUNCATE | TRUNCATEONLY} ]  )

DBCC SHRINKDATABASE(database_name [, target_percent][, {NOTRUNCATE | TRUNCATEONLY} ]  )

DBCC SHRINKFILE

DBCC SHRINKFILE allows you to shrink files in the current database. For example, for a 15-MB data file, a DBCC SHRINKFILE with a target_size of 12 causes all used pages in the last 3 MB of the file to be reallocated into any free slots in the first 12MB of the file. DBCC SHRINKFILE doesn't shrink a file past the size needed to store the data.

DBCC SHRINKDATABASE

DBCC SHRINKDATABASE shrinks all files in a database but does not allow any file to be shunk smaller than its minimum size. The minimum size of a database file is the initial size of the file(specified when the database was created) or the size to which the file has been explicitly extended or reduced, using either the ALTER DATABASE OR DBCC SHRINKFILE command.

Using Database Filegroups 

you can group data files for a datbase into filegroups for allocation and administration purposes.The filegroup containing the primary data file is called the primary filegroup.

Don't confuse the primary filegroups and the primary file. Here are the differences:

The primary file is always the first file listed when you create a databse, and it typically has the file extension .mdf. The one special feature of the primary file is that is has pointers into a table in the master database that contains information about all the files belonging to the database.

The primary filegroup is always the filegroup that contains the primary file. This filegroup contains the primary data file and any files not put into another specific filegroup. All pages from system tables are always allocated from files in the primary filegroup.

The Default Filegroup

One filegroup always has the property of DEFAULT. Note that DEFAULT is a property of a filegroup, not a name. Only one filegroup in each database can be default filegroup.By default, the primary filegroup is also the default filegroup. A database owner can change which filegroup is the default by using the ALTER DATABASE command.

发布了39 篇原创文章 · 获赞 4 · 访问量 3473

猜你喜欢

转载自blog.csdn.net/u011868279/article/details/102084937
今日推荐