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

The Real MTCS SQL Server 2008 Exam 70/432 Prep Kit- P55 docx

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 (135.74 KB, 5 trang )

252 Chapter7•MaintainingYourDatabase
2 Peter
1 Valentine
4 valentine
SELECT * FROM TeamMembers ORDER BY MemberName COLLATE Latin1_General_CS_AS
Results:
MemberID MemberName

5 Matthew
3 Matthéw
2 Peter
4 valentine
1 Valentine
SELECT * FROM TeamMembers WHERE MemberName = 'Matthew'
Results:
MemberID MemberName

3 Matthéw
5 Matthew
SELECT * FROM TeamMembers WHERE MemberName = 'Matthew' COLLATE
Latin1_General_BIN
Results:
MemberID MemberName

5 Matthew
DROP DATABASE ExampleDB2
GO
Configuring & Implementing…
Selecting the Appropriate Collation
Given that your choice of collation has a profound effect on query results,
how do you choose the correct collation? To answer this question, first


consider your multilingual requirements. Will you store data in other
languages? Will you integrate with other SQL Server systems that run
under a different language? If the answer to these is ‘no’, you should
Continued
MaintainingYourDatabase•Chapter7 253
Collation
Considerations for Backup and Restore
SQL Server allows you to restore databases to a different server. If a server is
rendered inoperable by a disaster, you can immediately start restoring important
databases from backup onto an existing server. The server that hosted the database
before the disaster is referred to as the source server, while the server you plan to
restore the database to is referred to as the target server. The collation of the source
server may not be the same as the collation of the target server.
When a database is restored to a target server that has a different collation from
the source server, the database will always retain its original collation settings. For
example, you have a SQL Server with the server-level default collation set to
French_CI_AI. You create a database on this server without specifying a collation.
The database will inherit the French_CI_AI collation. You take a backup of this
database and restore it to another server that has the collation of Latin1_General_
CS_AS. The database will retain its original collation of French_CI_AI. If you need
to sort or compare data in this restored database using rules of the Latin1_General_
CS_AS target server collation, you must use the COLLATE Latin1_General_CS_AS
clause in your queries.
As a more permanent solution, you may need to use the ALTER TABLE
statement to change the collation of the column. However, ALTER TABLE
cannot be used if a constraint, computed column, index, or any manually created
statistics reference the column you are altering.
keep the default Windows collation suggested by the SQL Server Setup
program. You can choose collation options like case sensitive or binary-code
page, if you require case sensitive sorts and comparisons.

If you are upgrading from SQL Server 6.5 or SQL Server 7, your
collation will be set to the SQL collation of the system you are upgrading
from, and no collation-related choices will be offered. If you need to
replicate or synchronize data with SQL Server 6.5 or SQL Server 7 systems,
you should choose the SQL collation of the target system.
Finally, if you are going to be working with data from many languages,
choose the most used or the most compatible language for the server-level
default collation. You can then create language-specific databases and
columns with collations that differ from the server default.
254 Chapter7•MaintainingYourDatabase
EXERCISE 7.1
US I n g Co l l a t I o n S
In this exercise, we will practice working with the COLLATE clause.
Before you begin, you must have the following software installed on
your computer:
SQL Server 2008 – a free trial is available for download

AdventureWorks sample database

We will be querying the Person.Contact table in the AdventureWorks
database using various collations.
1. Open SQL Server Management Studio. To do this click Start | All
Programs | Microsoft SQL Server 2008 | SQL Server Management
Studio.
2. Create a new query against the AdventureWorks database.
3. Use the SELECT…INTO statement to retrieve a distinct list of first
names into a temporary table named “#Names”.
SELECT DISTINCT FirstName
INTO #Names
FROM AdventureWorks.Person.Contact

4. Sort the data using the Latin1_General_CS_AI case sensitive colla-
tion. In order to see how case sensitivity affects query results,
union the table to itself by casting join columns upper and lower
case. Take note of the sort order (lower case before upper case).
SELECT UPPER(CAST(FirstName AS nvarchar(50)) COLLATE
Latin1_General_CS_AI) AS FirstName
FROM #Names
UNION ALL
Ex a m Wa r n i n g
You are likely to be asked about the impact of collations on backup and
restore. Ensure that you understand how collations are affected by the
restore operation, i.e., the restored database will retain its original
collation. Ensure that you understand how to use the COLLATE clause
with the SELECT statement and have practiced using it.
MaintainingYourDatabase•Chapter7 255
SELECT LOWER(CAST(FirstName AS nvarchar(50)) COLLATE
Latin1_General_CS_AI) AS FirstName
FROM #Names
ORDER BY FirstName
5. Perform a self-join using a case sensitive collation. In order to see
how case sensitivity affects joining, we cast one side of the join all
in upper case. Will any rows be returned here?
SELECT a.FirstName, UPPER(b.FirstName)
FROM #Names AS a
INNER JOIN #Names AS b
Matching using case sensitive comparison, but ignoring accent
ON CAST(a.FirstName AS nvarchar(50)) COLLATE Latin1_General_CS_AI =
UPPER(CAST(b.FirstName AS nvarchar(50))) COLLATE
Latin1_General_CS_AI
Sorting by a case sensitive collation

ORDER BY CAST(a.FirstName AS nvarchar(50)) COLLATE
Latin1_General_CS_AI
6. Sort the data using an accent sensitive collation. In order to see
how accent sensitivity affects sorting, use another self-join to find
all those cases where the only difference between the two names
is in accented characters.
SELECT a.FirstName
FROM #Names AS a
INNER JOIN #Names AS b
We match ignoring case and accent
ON CAST(a.FirstName AS nvarchar(50)) COLLATE Latin1_General_CI_AI =
CAST(b.FirstName AS nvarchar(50)) COLLATE Latin1_General_CI_AI
We find rows that do not match on accent.
AND CAST(a.FirstName AS nvarchar(50)) COLLATE Latin1_General_
CI_AS <>
CAST(b.FirstName AS nvarchar(50)) COLLATE Latin1_General_CI_AS
We sort by a case sensitive collation
ORDER BY CAST(a.FirstName AS nvarchar(50)) COLLATE
Latin1_General_CI_AS
7. Using a similar self-join as in the previous step, view name
matches side by side. Force the matches with accents to one side
256 Chapter7•MaintainingYourDatabase
of the join using an OR operator. Review the results of this state-
ment and how it is affected by collation.
SELECT a.FirstName,
b.FirstName
FROM #Names AS a
INNER JOIN #Names AS b
We match ignoring case and accent
ON CAST(a.FirstName AS nvarchar(50)) COLLATE Latin1_General_CI_AI =

CAST(b.FirstName AS nvarchar(50)) COLLATE Latin1_General_CI_AI
We find rows that do not match on accent.
AND CAST(a.FirstName AS nvarchar(50)) COLLATE Latin1_General_CI_AS >
CAST(b.FirstName AS nvarchar(50)) COLLATE Latin1_General_CI_AS
DROP TABLE #Names
Maintaining Data Files
Database objects, like tables and indexes, are physically stored in data files sometimes
across multiple filegroups. In production databases, you must perform ongoing mainte-
nance on these data files, as well as optimize database performance and disk space used.
The key techniques to optimize disk space usage are data compression, sparse columns,
and the shrinking of database and log files using the Database Console Commands
(DBCC) DBCC SHRINKFILE option. Performance is optimized by the creation
and ongoing maintenance of indexes. In this section, you will learn how to use data
compression, maintain indexes, and use the DBCC commands to validate and fix
errors in your databases.
Implementing Data Compression
You can enable data compression to reduce the amount of disk space that your
database uses. Data compression is a new feature of SQL Server 2008 and can be
enabled at two levels: row compression and page compression. The decision to enable data
compression should not be taken lightly as it is very likely to reduce the perfor-
mance of your database applications. The performance reduction is caused by the
additional work your server must do to compress the data. Consequently, the deci-
sion to enable data compression is a trade-off between disk space and performance.
Data compression is only available in SQL Server 2008 Enterprise Edition or SQL
Server 2008 Developer Edition. Data compression can be applied to tables and indexes.

×