Collaborate Chapter 1 Querying, Managing, and Administering Databases Using SQL Server 2005 1.3 ¤NIIT In this section you will learn about: .NET Framework SQL Server 2005 tools Grouping data by using the UNPIVOT operator Read the following topic in the Introduction to SQL Server 2005 section of Chapter 1 of the book Querying and Managing Data Using SQL Server 2005: The .NET Framework Read the following section of Chapter 1 of the book Querying and Managing Data Using SQL Server 2005: Identifying the SQL Server 2005 Tools The UNPIVOT operator allows database users to normalize the data that has earlier been pivoted. The UNPIVOT operator transforms the multiple column values of a record into multiple records with the same values in a single column. For example, a table that stores applicants’ records has one row for each applicant. The table stores the applicant’s name, qualification and grade. After applying the UNPIVOT operator on the table, the table contains a different row for each qualification that the applicant has completed. Knowledge Byte .NET Framework W orking with SQL Server 2005 Tools Grouping Data by Using the UNPIVOT Operator ¤NIIT 1.4 Querying, Managing, and Administering Databases Using SQL Server 2005 The following table shows the structure of the Applicant table before using the UNPIVOT operator. Column Name Applicant Name Matriculation Higher Secondary Graduation Anderson A A B Samuel A B A Data Records Sandra B B B Applicant Table Before Using the UNPIVOT Operator The following table shows the structure of the Applicant table after using the UNPIVOT operator. Column Name Applicant Name Qualification Grade Anderson Matriculation A Anderson Higher Secondary A Anderson Graduation B Samuel Matriculation A Samuel Higher Secondary B Samuel Graduation A Sandra Matriculation B Sandra Higher Secondary B Data Records Sandra Graduation B Applicant Table After Using the UNPIVOT Operator Querying, Managing, and Administering Databases Using SQL Server 2005 1.5 ¤NIIT The syntax for using the UNPIVOT operator is: SELECT ApplicantName, newTable.Qualification, newTable.Grade FROM tblPivot UNPIVOT (Qualification FOR Grade IN (Matriculation, Higher Secondary, Graduation)) as tblUnPivot ¤NIIT 1.6 Querying, Managing, and Administering Databases Using SQL Server 2005 This section contains: Best practices Tips and tricks FAQs The following best practices can be considered while using the query statements in SQL Server 2005: When you write stored procedures, triggers, and user-defined functions, you need to decide whether to use traditional Transact-SQL or a programming language that is compatible with the .NET Framework, such as Visual Basic .NET or C#. Transact- SQL is best for situations in which the code primarily performs data access with little or no procedural logic. Programming languages that are compatible with the .NET Framework are best-suited for computationally-intensive functions and procedures that feature complex logic or for situations where you want to take advantage of the .NET Framework class library. To rotate the data of a table you can use the PIVOT and UNPIVOT operators, instead of using complex JOIN statements. SQL Server 2005 provides three new data types for storing long columns: VARCHAR(MAX), NVARCHAR(MAX) and VARBINARY(MAX). These new data types are easier to use than the older large object data types, such as TEXT, NTEXT, and IMAGE. When building a new application that needs to store a column that possibly might exceed 8000 bytes, you should consider using these new large value data types. The total characters displayed in the Query Editor Results window are limited to 256 characters. If the result exceeds the limit of 256 characters, it gets truncated. In this case, you can use the Options tab in Management Studio to set the maximum column size under the Results section. The result sets that you return from your database should be kept as small as possible. This greatly improves the performance and makes the database much more scalable. From the Expert’s Desk Best Practices Querying, Managing, and Administering Databases Using SQL Server 2005 1.7 ¤NIIT The following tips and tricks will help you perform effective query by using SQL Server 2005: When writing calculation expressions such as “expr1 * expr2”, ensure that the expression sweeping the largest area/volume is on the left side. For instance, write “Sales * ExchangeRate” instead of “ExchangeRate * Sales”, and “Sales * 1.15” instead of “1.15 * Sales”. Here Sales value will be larger than the exchange rate. Consider replacing simple “Measure1 + Measure2” calculations with computed columns in the SQL data source. Avoid using Select * in your query design. Instead, ensure that you use the proper column names in the query. Using the proper column names decreases network traffic, puts less load on the database, and therfore, improves performance. What are the two new components introduced with SQL Server 2005? The two new components introduced with SQL are: z SQL Server Management Studio z SQL Server Business Intelligence Development Studio Can we use the PIVOT and UNPIVOT operators in SQL Server 2000? No, SQL Server 2000 does not support the PIVOT and UNPIVOT operators. To achieve the same functionality as that of the PIVOT and UNPIVOT operators, you can use JOINS in SQL Server 2000. Which framework is supported by SQL Server 2005? SQL Server 2005 supports Framework V2.0. Which application architecture does SQL Server 2005 support? SQL Server 2005 supports n-tier architecture. Which service of SQL Server 2005 allows you to gather and integrate data from various disparate data sources available in an organization? Integration services of SQL Server 2005 allow you to gather and integrate data from various disparate data sources available in an organization. Tips and Tricks FAQs ¤NIIT 1.8 Querying, Managing, and Administering Databases Using SQL Server 2005 1. Which of the following is not a component of the .NET Framework? a. Development tools and languages b. Base class library c. Service Broker d. Common Language Runtime 2. Which of the following component of the .NET Framework is used to create the interface for the Windows forms? a. Base class library b. Service Broker c. Development tools and languages d. Common Language Runtime 3. Which component of the .NET Framework provides an environment for the application to run? a. Development tools and languages b. Base class library c. Security Management d. Common Language Runtime 4. Which of the following is not a feature provided by the common language runtime? a. Service-oriented architecture b. Language interoperability c. Automatic memory management d. Platform independence 5. Which of the following is not a main component of the SQL Server Management Studio interface? a. Solution Explorer b. Query Editor c. Object Explorer d. SQL Server Client Agent Challenge Querying, Managing, and Administering Databases Using SQL Server 2005 1.9 ¤NIIT 1. What is the maximum length of NVARCHAR data type? a. 8000 characters b. 6000 characters c. 4000 characters d. 400 characters 2. Which of the following data type does not store integer value? a. tinyint b. int c. smallint d. decimal 3. Which operator is used to change the precedence of the arithmetic operators in SQL queries? a. \ b. [ ] c. ( ) d. { } 4. Which of the following query retrieves the record of the students who are studying in a class higher than class 10? a. SELECT * FROM Student WHERE NOT class<=10 b. SELECT * FROM Student WHERE class=10 c. SELECT * FROM Student WHERE class>=10 d. SELECT * FROM Student WHERE NOT class>=10 5. Which of the following query retrieves the names of the cities that start with letter M and whose fourth letter is B? a. SELECT City FROM City_List WHERE City LIKE ‘M_ B_%’ b. SELECT City FROM City_List WHERE City LIKE ‘M_ _ B[]’ c. SELECT City FROM City_List WHERE City LIKE ‘M_ _ B%’ d. SELECT City FROM City_List WHERE City LIKE ‘M% B%’ 6. Which SQL Server 2005 service provides data mining solutions that are built on data integrated in the data warehouse? a. Analysis services b. Reporting services c. Database engine d. Integration services Home Assignment ¤NIIT 1.10 Querying, Managing, and Administering Databases Using SQL Server 2005 7. Which type of statement in SQL Server allows you to control the data access in the database? a. DML b. DCL c. DDL d. DQL 8. Which data type is used to store fixed length character data? a. text b. varchar(n) c. char(n) d. nchar 9. Which clause allows you to group data in a SELECT statement? a. WHERE b. HAVING c. ORDER BY d. COMPUTE BY 10. Which group operator is used to transform a set of columns into values? a. COMPUTE b. COMPUTE BY c. GROUP BY d. PIVOT . Collaborate Chapter 1 Querying, Managing, and Administering Databases Using SQL Server 2005 1. 3 ¤NIIT In this section you will learn about: .NET Framework SQL Server 2005 tools . Introduction to SQL Server 2005 section of Chapter 1 of the book Querying and Managing Data Using SQL Server 2005: The .NET Framework Read the following section of Chapter 1 of the book Querying. * 1. 15” instead of 1. 15 * Sales”. Here Sales value will be larger than the exchange rate. Consider replacing simple “Measure1 + Measure2” calculations with computed columns in the SQL