This session includes the following topics: Functions to customize the result set in SQL Server 2005 Best practices Tips and Tricks Slide 1 Installing Windows XP Professional U
Trang 1Instructor Inputs S e
Trang 3This session includes the following topics:
Functions to customize the result set in SQL Server 2005
Best practices
Tips and Tricks
Slide 1
Installing Windows XP Professional Using Attended Installation
Slide 1 of 21
Ver 1.0
Querying, Managing, and Administering Databases Using SQL Server 2005
Collaborate Session 2
Objectives
In this session, you will learn about:
Functions to customize the result set in SQL Server 2005 Best Practices while using procedures and functions in SQL Server 2005
Tips and Tricks that will help you use functions in SQL Server
2005 FAQs related to general data types and functions in SQL Server 2005
Trang 42.4 Instructor Inputs ¤NIIT
Slide 2 of 21
Ver 1.0 Collaborate Session 2
Knowledge Byte
SQL Server 2005 provides many in-built functions to Customize the Result Set:
String functions:
Are used to manipulate the string values in the result set
Date Functions:
Are used to manipulate datetime values
Are used either to perform arithmetic operations on date values
or parse the date values
Ranking Functions:
Are used to generate sequential numbers for each row or to give
a rank based on specific criteria
Slide 3
Installing Windows XP Professional Using Attended Installation
Slide 3 of 21
Ver 1.0
Querying, Managing, and Administering Databases Using SQL Server 2005
Collaborate Session 2
Knowledge Byte (Contd.)
System Functions:
Are used to query the system tables System tables are a set of tables that are used by the SQL Server to store information about users, databases, tables, and security
Are used to access the SQL Server databases or user-related information.
Trang 5Slide 4 of 21
Ver 1.0 Collaborate Session 2
Best Practices
Never start the name of any stored procedure you create, with the SP prefix This is because all system stored procedures start with the SP prefix
Avoid using the INSERT statement when performing bulk inserts into your database
To reference a table from another database in the same server, prefix the table with the database name Use the following syntax to refer a table from another database in the same server:
SELECT * from [<database name>].[dbo].[< table name>]
Slide 5
Installing Windows XP Professional Using Attended Installation Querying, Managing, and Administering Databases Using SQL Server 2005
Tips and Tricks
Use SPACE() function for concatenating values in a query
Use VARCHAR(max) data type with LEFT(), RIGHT(), SUBSTRING() functions because these functions do not support ntext and text data types
Analyze all your query plans by using the SQL Query Analyzer to ensure that they are performing at optimum speed
Trang 62.6 Instructor Inputs ¤NIIT
Slide 6 of 21
Ver 1.0 Collaborate Session 2
Tips and Tricks (Contd.)
Avoid the use of nullable columns:
The use of the nullable column consumes an extra byte on each column used and while querying data, there is much more overhead with nullable columns
Slide 7
Installing Windows XP Professional Using Attended Installation
Slide 7 of 21
Ver 1.0
Querying, Managing, and Administering Databases Using SQL Server 2005
Collaborate Session 2
What is the difference between the DELETE and TRUNCATE commands?
The DELETE command removes the rows from a table based
on the condition provided with the WHERE clause The TRUNCATE command removes all the rows from a table and does not give an option to selectively delete records
FAQs
Trang 7Slide 8 of 21
Ver 1.0 Collaborate Session 2
How can you figure out if a string is completely in uppercase?
You can find if a string is completely in uppercase by using the ASCII string function
FAQs (Contd.)
Slide 9
Installing Windows XP Professional Using Attended Installation Querying, Managing, and Administering Databases Using SQL Server 2005
Can you define any length for a VARCHAR data type, such
as VARCHAR(10000)?
When the length is specified in declaring a VARCHAR variable
or column, the maximum length allowed is 8000 characters If the length is greater than 8000 characters, you have to use the MAX specifier as the length
FAQs (Contd.)
Trang 82.8 Instructor Inputs ¤NIIT
Slide 10 of 21
Ver 1.0 Collaborate Session 2
If there are VARCHAR(MAX), NVARCHAR(MAX) and VARBINARY(MAX) data types, are there CHAR(MAX), NCHAR(MAX) and BINARY(MAX) data types also?
No, there is no CHAR(MAX), NCHAR(MAX), or BINARY(MAX) data types The main reason why the MAX specifier is not included for these data types is because these data types are fixed-length data types If the MAX specifier is included for these data types, it will be a big waste of disk space as each column will consume 2 GB of memory even if only a short string value is assigned to the column
FAQs (Contd.)
Slide 11
Installing Windows XP Professional Using Attended Installation
Slide 11 of 21
Ver 1.0
Querying, Managing, and Administering Databases Using SQL Server 2005
Collaborate Session 2
How can you turn on IDENTITY on an existing column?
To change an existing column as an IDENTITY column, perform the following steps:
Modify table in object browser.
Change column property to IDENTITY “yes”.
Save the change.
FAQs (Contd.)
Trang 9Slide 12 of 21
Ver 1.0 Collaborate Session 2
Challenge
Identify the output of the following command:
Select left(‘SQL Server’,3)
Ser SQL ver
QL S
Answer:
SQL
Slide 13
Installing Windows XP Professional Using Attended Installation Querying, Managing, and Administering Databases Using SQL Server 2005
Challenge (Contd.)
Which function is used to return the length of a string?
length lenString StrLen len
Answer:
len
Trang 102.10 Instructor Inputs ¤NIIT
Slide 14 of 21
Ver 1.0 Collaborate Session 2
Challenge (Contd.)
Which string is used to replace a character with another character?
charindex Space stuff patindex
Answer:
stuff
Slide 15
Installing Windows XP Professional Using Attended Installation
Slide 15 of 21
Ver 1.0
Querying, Managing, and Administering Databases Using SQL Server 2005
Collaborate Session 2
Challenge (Contd.)
Which method returns the current date and time?
getdate date gettime time
Answer:
getdate
Trang 11Slide 16 of 21
Ver 1.0 Collaborate Session 2
Challenge (Contd.)
What is the return type of rand function?
numeric int float smallint
Answer:
float
Slide 17
Installing Windows XP Professional Using Attended Installation Querying, Managing, and Administering Databases Using SQL Server 2005
Challenge (Contd.)
Which join returns the Cartesian Product of the rows of two tables?
EQUI JOIN INNER JOIN CROSS JOIN OUTER JOIN
Answer:
CROSS JOIN
Trang 122.12 Instructor Inputs ¤NIIT
Slide 18 of 21
Ver 1.0 Collaborate Session 2
Challenge (Contd.)
What does >ALL mean in a SELECT statement that is using
a subquery?
It means greater than the maximum value in the list
It means any of the values in the list
It means greater than the minimum value in the list
It means lesser than the minimum value in the list
Answer:
It means greater than the maximum value in the list
Slide 19
Installing Windows XP Professional Using Attended Installation
Slide 19 of 21
Ver 1.0
Querying, Managing, and Administering Databases Using SQL Server 2005
Collaborate Session 2
Challenge (Contd.)
Which system database contains all system objects?
Resource database master database temdb database model database
Answer:
Resource database
Trang 13Slide 20 of 21
Ver 1.0 Collaborate Session 2
Challenge (Contd.)
Introduction to Hacking/Chapter 1
What should be the size of a transaction log file?
25-40 percent of the database size 25-30 percent of the database size 25-35 percent of the database size 22-40 percent of the database size
Answer:
25-40 percent of the database size
Slide 21
Installing Windows XP Professional Using Attended Installation Querying, Managing, and Administering Databases Using SQL Server 2005
Challenge (Contd.)
Which keyword with the CHECK constraint ensures that the values entered are from a list of constant expressions?
BETWEEN IN ALL LIKE
Answer:
IN
Trang 142.14 Instructor Inputs ¤NIIT
2 d len
3 c stuff
4 a getdate
5 c float
Trang 151 Which join should be used to retrieve records with values satisfying the join
condition in the common column?
a Outer Join
b Inner Join
c Cross Join
d Equi Join
2 Which database contains critical data that controls SQL Server operations?
a model
b msdb
c tempdb
d master
3 What should be the size of the transaction log file?
a 25-35 percent
b 25-30 percent
c 25-40 percent
d 20-40 percent
4 Which key constraint is used to maintain referential integrity?
a Foreign key constraint
b Primary key constraint
c Unique key constraint
d Check constraint
5 Which stored procedure is used to bind a rule?
a sp_bind
b sp_bindrule
c sp_rulebind
d sp_unbindrule
Solutions
Trang 162.16 Instructor Inputs ¤NIIT
The major requirement of any organization or an enterprise is to store complete and consistent data Data integrity is the concept of maintaining consistency and completeness
of data Data integrity is enforced to ensure that the data in a database is accurate,
consistent, and reliable
Discuss the broadly classified categories of data integrity In addition, discuss the
different constraints which can be applied to a table to enforce data integrity
Instructor Inputs
You can conduct the discussion as follows:
Move the discussion toward different categories of data integrity
Continue the discussion by mentioning the different types of constraints
Relate each constraint with the data integrity category
Solution
Data integrity is enforced to ensure that the data in a database is accurate, consistent, and reliable It is broadly classified into the following categories:
called the primary key
can be enforced by restricting the type of data, the range of values, and the format of the data.
the corresponding primary key.
belong to the entity, domain, and referential integrity categories
When creating tables, the SQL Server allows you to maintain integrity by:
Applying constraints
Applying rules
Using user-defined types
Constraints can be divided into the following types: