Tải bản đầy đủ (.pdf) (10 trang)

CollaborateChapter 7..Knowledge ByteIn this section, you will learn about: Detaching and ppt

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (189.46 KB, 10 trang )

Collaborate
Chapter 7

Querying, Managing, and Administering Databases Using SQL Server 2005 7.3
¤NIIT
In this section, you will learn about:
 Detaching and attaching a database
 Restore operation
Read the following topics in the section Managing Databases of Chapter 2 of the book
Administering Databases using SQL Server 2005:
 Detaching a Database
 Attaching a Database
The process of retrieving data from a backup and applying transaction log to the data is
called as restoring. When you create a differential backup, it records all the transactions
applied to the database. Using the differential backup you can roll forward the transaction
as a part of the restoration process.
The information about the uncommitted transactions is also a part of the backup. This
information is useful to roll back uncommitted transactions and bring the database in a
transactionally consistent and usable state. The process of rolling forward uncommitted
transactions, if any, and bringing the database online is known as recovery. Before you
learn about the phases involved in the restore operation, you should know the following
terms used in the restore operation:
 Roll forward set: In a restore process, the logged changes are applied to the data in a
database to retrieve the data on time. This process is known as rolling forward and
the set of data restored is known as roll forward set. A roll forward set is created by
restoring one or more full backups, such as a database or partial backup or a set of
file backups. If you restore only filegroups, files and pages of a database then your
roll forward set will include only these items. Roll forward set will include all the
files of a backup if the user has not mentioned the specific restore items.
 Restore sequences: Each restore scenario is implemented by using one or more
restore steps (operations), called a restore sequence. Each operation corresponds to


an individual Transact-SQL RESTORE statement. A restore sequence moves
affected data through one or more of the phases of restore.
Knowledge Byte
Detaching and Attaching a Database
Restore Operation
7.4 Querying, Managing, and Administering Databases Using SQL Server 2005
¤NIIT
The Phases of Restore
A restore operation is a multiphase process. The possible phases of a restore process are
data copy phase, redo phase, and undo phase.
Data Copy Phase
The data copy phase is the first phase of any restore process. In this phase the contents of
the database, files and pages are initialized for restore. Full or differential backup
operations initiate this phase by restoring a database, a file, or a page. In the data copy
phase the content of the affected database, files, or pages are reset with the data from the
last full or differential backups. The content which is reset is from the time that they were
captured by those backups. The oldest file or page in the roll forward set determines the
starting point for the next phase.
Redo Phase
The redo phase includes the process of redoing logged changes to the data in the roll
forward set to bring the data forward in time. The SQL Server Database Engine processes
log backups as they are restored. It starts with the log that is contained in full backups for
rolling forward. Restore avoids unnecessary roll forward. The process of roll forward is
not required, if data was read-only when it was backed up and has remained read-only.
Undo Phase
The common problem faced after the redo phase is transactionally inconsistent data. In
the redo phase all the log transactions are rolled forward. In such a situation, a database
typically contains changes made by transactions that are uncommitted at the recovery
point. The recovery point is the point till which the set of data to be recovered is specified
by the user.

The undo process opens the transaction log to identify the uncommitted transactions. The
uncommitted transactions are rolled back, unless they hold locks that prevent other
transactions from viewing transactionally inconsistent data. If the data is already
transactionally consistent at the start of the recovery process, the undo phase is skipped.
After the database is transactionally consistent, recovery brings the database online.
Querying, Managing, and Administering Databases Using SQL Server 2005 7.5
¤NIIT
This section contains:
 Best practices
 Tips and tricks
 FAQs
The following best practices can be considered while managing databases and database
objects using SQL Server 2005:
 While creating your own database objects, ensure that you do not create the database
objects in the system databases like master database. This is because the master
database contains system-level information used by the instance of SQL Server, such
as logon information and configuration option settings.
 While moving master database and log files to another location, ensure that you also
move the Resource database to the same location. If you do not do so, you might face
problems while restoring the master database.
 You cannot take a backup of Resource database using the backup techniques
available in SQL Server 2005. You can backup and restore the Resource database
manually. While restoring the Resource database you must be careful not to
overwrite the current Resource database with an out-of-date or potentially insecure
version of Resource database.
 The size of tempdb can affect the performance of a system. If the size of the tempdb
is less than the requirement of the server, it would have an impact on the
performance of the server. This is because if the users will perform transaction on the
databases, the server would be requiring free space in tempdb. If the space is not
available in tempdb, the size of the temdb is increased. This will happen with every

transaction and it will effect the performance of the server. To avoid this, a good
practice is to increase the size of tempdb.
 It is not a good idea to shrink a database if the database has frequent updations. In
these cases, shrinking a database is a wasted operation. This is because most
databases require some free space for regular day-to-day operations. If you shrink a
database repeatedly and notice that the database size grows again, this indicates that
the space that was shrunk is required for regular operations. In these cases,
repeatedly shrinking the database is a wasted operation.
From the Expert’s Desk
Best Practices
7.6 Querying, Managing, and Administering Databases Using SQL Server 2005
¤NIIT
The following tips and tricks will help you to effectively manage database and database
objects using SQL Server 2005:
 Backup the master database after performing the following operations:
z Creating, modifying, or dropping a database
z Changing server and database configuration values
z Modifying or adding logon accounts
 After restoring a backup of mssqlsystemresource.mdf, it is a good practice to reapply
any subsequent updates.
 After you add or delete files in a database, create a database backup immediately.
Also keep in mind not to create a transaction log backup unless a full database
backup has been created.
 If you have to create multiple secondary files in your database, create them in a
separate filegroup. Also make the second filegroup as default. In this way, the
primary filegroup will contain only system tables and objects.
 Do not put the Resource database in either compressed or encrypted NTFS file
system folders. Doing so will hinder performance and prevent upgrades.
 Can you change the owner of the master database?
You cannot change the owner of master database as it is owned by dbo.

 Can master database be dropped?
No, you cannot drop master database as it is a system database.
 While creating objects in a database if user does not specify the filegroup, which
filegroup are objects assigned to?
The objects are by default assigned to default filegroup. The primary filegroup is the
default filegroup.
 What are the three types of files used to store a database? Which of these files are
must for a database?
The three types of files to store a database are primary files, secondary files, and
transaction files. A database must have a primary file and atleast one transaction log
file.
 Can transaction log files be a part of any filegroup?
No, transaction log file are never a part of any filegroup.
Tips and Tricks
FAQs
Querying, Managing, and Administering Databases Using SQL Server 2005 7.7
¤NIIT
1. Which of the following statement is NOT correct about detaching a database?
a. It’s a logical removal of database from the server.
b. A detached database can be attached to a SQL Server in the same computer.
c. Using the SQL Server Management Studio you can detach a database.
d. A detached database cannot be attached to SQL Server of another computer.
2. Which T-SQL procedure is used to detach a database?
a.
sp_detach_db
b.
sp_detach
c. sp_db_detach
d.
sp_detachdb

3. Which of the following statement will detach the AdventureWorks and execute the
UPDATE STATISTIC process?
a.
sp_detach_db 'AdventureWorks', 'TRUE'
b.
sp_detach_db 'AdventureWorks', ‘NULL’
c. sp_detach 'AdventureWorks', 'TRUE'
d.
sp_detach 'AdventureWorks', 'NULL'
4. In which condition CREATE DATABASE with FOR ATTACH clause is used?
a. When the database is detached using sp_detach_db statement
b. When the database is detached using SQL Server Management Studio
c. When the number of data files to be attached is more than 16
d. When the database needs to be attached to a server on a different computer
5. Which phase of restore process is skipped if the data is already transactionally
consistent at the start of the recovery process?
a. Redo Phase
b. Data Copy Phase
c. Undo Phase
d. Roll Forward Phase
Challenge
7.8 Querying, Managing, and Administering Databases Using SQL Server 2005
¤NIIT
1. Which of the following statement is true about log files?
a. These file are used to recover a database
b. These files have an extension of .ndf
c. A database can have only one log file
d. The minimum size of log files is 256 MB
2. Which of the following commands will remove the filegroup fg1 from the database
db1?

a.
ALTER DATABASE db1 REMOVE FILE fg1
b. ALTER DATABASE db1 REMOVE fg1
c.
ALTER DATABASE db1 REMOVE FILEGROUP fg1
d. ALTER FILEGROUP fg1 REMOVE FILEGROUP fg1 FROM db1
3. Which of the following commands will show the information for the emp_sts
statistics on the Event table?
a.
DBCC SHOW_STATISTICS ('db1.Event', emp_sts)
b.
DBCC SHOW_CONTIG ('db1.Event', emp_sts)
c. sp_helpstats ('db1.Event', emp_sts)
d.
sp_helpstats 'db1.Event', ‘emp_sts’
4. Which of the following command checks for the consistency of disk space allocation
structures for a particular database?
a. DBCC CHECKDBALLOC
b. DBCC CHECKALLOC
c. DBCC CHECKTABLE
d. DBCC CHECKMEM
5. Which of the following is NOT a schedule type available in the New Job Schedule
dialog box?
a. Recurring
b. First time
c. Start automatically when SQL Server Agent starts
d. Start whenever CPU become idle
6. Which of the following principal allows log on to an instance of the SQL Server?
a. Windows
b. Database

c. Server
d. System
Home Assignment
Querying, Managing, and Administering Databases Using SQL Server 2005 7.9
¤NIIT
7. Which scope of SQL Server level securable includes the tables, views, procedures
and functions?
a. Sever scope
b. Database scope
c. Schema scope
d. Windows scope
8. Which parameter of CREATE LOGIN statement specifies the GUID of the new SQL
Server login?
a. GUID
b. SID
c. PASSWORD
d. HASHED
9. Which of the following fixed database role allows adding, modifying, and dropping
database objects?
a. db_accessadmin
b. db_owner
c. db_ddladmin
d. db_securityadmin
10. Which of the following is a database level permission?
a. ALTER ANY DATABASE
b. VIEW ANY DATABASE
c. VIEW DATABASE STATE
d. BACKUP DATABASE
7.10 Querying, Managing, and Administering Databases Using SQL Server 2005
¤NIIT

×