1. Trang chủ
  2. » Công Nghệ Thông Tin

Beginning C# 2008 Databases From Novice to Professional phần 2 ppsx

52 327 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 52
Dung lượng 1,45 MB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

A relational database management system, or RDBMS, is a type of DBMS that stores information in the form of related tables.. Following are some other characteristics that differentiate s

Trang 1

Figure 2-10.SSMSE Object Explorer and Summary tabbed pane

in Figure 2-11 As you can see, SSMS has four system databases:

global information that is required for the SQL Server instance

• The model database works as a template for new databases to be created; in

other words, settings of the model database will be applied to all user-createddatabases

• The msdb database is used by SQL Server Agent for scheduling jobs and alerts.

• The tempdb database holds temporary tables and other temporary database

objects, either generated automatically by SQL Server or created explicitly byyou The temporary database is re-created each time the SQL Server instance

is started, so objects in it do not persist after SQL Server is shut down

C H A P T E R 2 ■ G E T T I N G TO K N O W YO U R TO O L S 23

Trang 2

Figure 2-11.System databases

bring up a new SQL edit window, as shown in Figure 2-12 As mentioned in ter 1, AdventureWorks is a new sample database introduced for the first time withSQL Server 2005

name from sysobjects where xtype=‘U’ and click the Execute button The table

names will appear in the Results tab (see Figure 2-12) If you navigate to the sages tab, you will see the message “70 row(s) affected,” which means that theAdventureWorks database consists of 70 tables

the table names residing inside Northwind, type the query select name from objects where xtype=‘U’ and click the Execute button A listing of tables in the

sys-database will appear in the Results tab (see Figure 2-13) If you navigate to theMessages tab, you will see the message “13 row(s) affected,” which means thatthe Northwind database consists of 13 tables

Studio Express

C H A P T E R 2 ■ G E T T I N G TO K N O W YO U R TO O L S

24

Trang 3

Figure 2-12.Tables in the AdventureWorks database

Figure 2-13.Tables in the Northwind database

C H A P T E R 2 ■ G E T T I N G TO K N O W YO U R TO O L S 25

Trang 5

Getting to Know

Relational Databases

to give you a brief introduction to the important concepts of the PC database world

before diving into the examples

In this chapter, we’ll cover the following:

• What is a database?

• Choosing between a spreadsheet and a database

• Why use a database?

• Benefits of using a relational database management system

• Comparing desktop and server RDBMS systems

• The database life cycle

In very simple terms, a database is a collection of structured information Databases are

designed specifically to manage large bodies of information, and they store data in an

27

C H A P T E R 3

Trang 6

organized and structured manner that makes it easy for users to manage and retrievethat data when required.

A database management system (DBMS) is a software program that enables users to

create and maintain databases A DBMS also allows users to write queries for an ual database to perform required actions like retrieving data, modifying data, deletingdata, and so forth

individ-DBMSs support tables (a.k.a relations or entities) to store data in rows (a.k.a records

or tuples) and columns (a.k.a fields or attributes), similar to how data appears in a

spreadsheet application

A relational database management system, or RDBMS, is a type of DBMS that stores information in the form of related tables RDBMS is based on the relational model.

Choosing Between a Spreadsheet and a Database

If databases are much like spreadsheets, why do people still use database applications? Adatabase is designed to perform the following actions in an easier and more productivemanner than a spreadsheet application would require:

• Retrieve all records that match particular criteria

• Update or modify a complete set of records at one time

• Extract values from records distributed among multiple tables

Why Use a Database?

Following are some of the reasons we use databases:

Compactness: Databases help in maintaining large amounts of data, and thus

com-pletely replace voluminous paper files

• Speed: Searches for a particular piece of data or information in a database are

much faster than sorting through piles of paper

• Less drudgery: It is a dull work to maintain files by hand; using a database

com-pletely eliminates such maintenance

Currency: Database systems can easily be updated and so provide accurate

infor-mation all the time and on demand

C H A P T E R 3 ■ G E T T I N G TO K N O W R E L AT I O N A L D ATA B A S E S

28

Trang 7

Benefits of Using a Relational Database

Management System

RDBMSs offer various benefits by controlling the following:

Redundancy: RDBMSs prevent having multiple duplicate copies of the same data,

which takes up disk space unnecessarily

• Inconsistency: Each redundant set of data may no longer agree with other sets of

the same data When an RDBMS removes redundancy, inconsistency cannot occur

Data integrity: Data values stored in the database must satisfy certain types of

con-sistency constraints (We’ll discuss this benefit in more detail in the section

“Understanding Data Integrity” later in this chapter.)

• Data atomicity: In event of a failure, data is restored to the consistent state it

existed in prior to the failure For example, fund transfer activity must be atomic

(We cover the fund transfer activity and atomicity in more detail in Chapter 8.)

Access anomalies: RDBMSs prevent more than one user from updating the same

data simultaneously; such concurrent updates may result in inconsistent data

• Data security: Not every user of the database system should be able to access all

the data Security refers to the protection of data against any unauthorized access

Transaction processing: A transaction is a sequence of database operations that

represents a logical unit of work In RDBMSs, a transaction either commits all thechanges or rolls back all the actions performed till the point at which failureoccurred

Recovery: Recovery features ensure that data is reorganized into a consistent state

after a transaction fails

Storage management: RDBMSs provide a mechanism for data storage

manage-ment The internal schema defines how data should be stored

Comparing Desktop and Server RDBMS Systems

In the industry today, we mainly work with two types of databases: desktop databases

and server databases Here, we’ll give you a brief look at each of them

C H A P T E R 3 ■ G E T T I N G TO K N O W R E L AT I O N A L D ATA B A S E S 29

Trang 8

Desktop Databases

Desktop databases are designed to serve a limited number of users and run on desktopPCs, and they offer a less-expansive solution wherever a database is required Chancesare you have worked with a desktop database program—Microsoft SQL Server Express,Microsoft Access, Microsoft FoxPro, FileMaker Pro, Paradox, and Lotus represent a widerange of desktop database solutions

Desktop databases differ from server databases in the following ways:

• Less expensive: Most desktop solutions are available for just a few hundred dollars.

In fact, if you own a licensed version of Microsoft Office Professional, you’realready a licensed owner of Microsoft Access, which is one of the most commonlyand widely used desktop database programs around

User friendly: Desktop databases are quite user friendly and easy to work with,

as they do not require complex SQL queries to perform database operations(although some desktop databases also support SQL syntax if you would like tocode) Desktop databases generally offer an easy-to-use graphical user interface

Server Databases

Server databases are specifically designed to serve multiple users at a time and offer tures that allow you to manage large amounts of data very efficiently by serving multipleuser requests simultaneously Well-known examples of server databases include

fea-Microsoft SQL Server, Oracle, Sybase, and DB2

Following are some other characteristics that differentiate server databases fromtheir desktop counterparts:

Flexibility: Server databases are designed to be very flexible to support multiple

platforms, respond to requests coming from multiple database users, and performany database management task with optimum speed

Availability: Server databases are intended for enterprises, and so they need to be

available 24/7 To be available all the time, server databases come with some availability features, such as mirroring and log shipping

high-• Performance: Server databases usually have huge hardware support, and so servers

running these databases have large amounts of RAM and multiple CPUs, and this

is why server databases support rich infrastructure and give optimum ance

perform-• Scalability: This property allows a server database to expand its ability to process

and store records even if it has grown tremendously

C H A P T E R 3 ■ G E T T I N G TO K N O W R E L AT I O N A L D ATA B A S E S

30

Trang 9

The Database Life Cycle

The database life cycle defines the complete process from conception to

implementa-tion The entire development and implementation process of this cycle can be divided

into small phases; only after the completion of each phase can you move on to the next

phase, and this is the way you build your database block by block

Before getting into the development of any system, you need to have strong a cycle model to follow The model must have all the phases defined in proper sequence,

life-which will help the development team to build the system with fewer problems and full

functionality as expected

The database life cycle consists of the following stages, from the basic stepsinvolved in designing a global schema of the database to database implementation

and maintenance:

• Requirement analysis: Requirements need to be determined before you can begin

design and implementation The requirements can be gathered by interviewingboth the producer and the user of the data; this process helps in creating a formalrequirement specification

• Logical design: After requirement gathering, data and relationships need to be

defined using a conceptual data modeling technique such as an entity relationship(ER) diagram

• Physical design: Once the logical design is in place, the next step is to produce the

physical structure for the database The physical design phase involves table ation and selection of indexes

• Database implementation: Once the design is completed, the database can be

cre-ated through implementation of formal schema using the data definition language(DDL) of the RDBMS

Data modification: Data modification language (DML) can be used to query and

update the database as well as set up indexes and establish constraints such as erential integrity

ref-• Database monitoring: As the database begins operation, monitoring indicates

whether performance requirements are being met; if they are not, modificationsshould be made to improve database performance Thus the database life cyclecontinues with monitoring, redesign, and modification

C H A P T E R 3 ■ G E T T I N G TO K N O W R E L AT I O N A L D ATA B A S E S 31

Trang 10

ness of data values contained in a particular column of a database table The term

relational database refers to the fact that different tables quite often contain related data.

For example, one sales rep in a company may take many orders, which were placed bymany customers The products ordered may come from different suppliers, and chancesare that each supplier can supply more than one product All of these relationships exist

in almost every database and can be classified as follows:

One-to-One (1:1) For each row in Table A, there is at most only one related row in Table B,and vice versa This relationship is typically used to separate data by frequency of use tooptimally organize data physically For example, one department can have only onedepartment head

One-to-Many (1:M) For each row in Table A, there can be zero or more related rows inTable B; but for each row in Table B, there is at most one row in Table A This is the mostcommon relationship An example of a one-to-many relationship of tables in Northwind

is shown in Figure 3-1 Note the Customers table has a CustomerID field as the primary key (indicated by the key symbol on the left), which has a relation with the CustomerID field of the Orders table; CustomerID is considered a foreign key in the Orders table The

link shown between the Customers and Orders tables indicates a one-to-many ship, as many orders can belong to one customer Here, Customers is referred to as the

relation-parent table, and Orders is the child table in the relationship.

C H A P T E R 3 ■ G E T T I N G TO K N O W R E L AT I O N A L D ATA B A S E S

32

Trang 11

Figure 3-1.A one-to-many relationship

Many-to-Many (M:M) For each row in Table A, there are zero or more related rows in

Table B, and vice versa Many-to-many relationships are not so easy to achieve, and

they require a special technique to implement them This relationship is actually

implemented in a one-many-one format, so it requires a third table (often referred to

as a junction table) to be introduced in between that serves as the path between the

related tables

This is a very common relationship An example from Northwind is shown in ure 3-2: an order can have many products and a product can belong to many orders

Fig-The Order Details table not only represents the M:M relationship, but also contains

data about each particular order-product combination

C H A P T E R 3 ■ G E T T I N G TO K N O W R E L AT I O N A L D ATA B A S E S 33

Trang 12

Figure 3-2.A many-to-many relationship

nothing to do with them Relational databases are (to varying extents) based on the relational model of data

invented by Dr Edgar F Codd at IBM in the 1970s Codd based his model on the mathematical

(set-theoretic) concept of a relation Relations are sets of tuples that can be manipulated with a well-defined and well-behaved set of mathematical operations—in fact, two sets: relational algebra and relational cal- culus You don’t have to know or understand the mathematics to work with relational databases, but if you

hear it said that a database is relational because it “relates data,” you’ll know that whoever said it doesn’tunderstand relational databases

Understanding Keys

The key, the whole key, and nothing but the key, so help me Codd.

Relationships are represented by data in tables To establish a relationship between twotables, you need to have data in one table that enables you to find related rows in another

C H A P T E R 3 ■ G E T T I N G TO K N O W R E L AT I O N A L D ATA B A S E S

34

Trang 13

table That’s where keys come in, and RDBMS mainly works with two types of keys, as

mentioned earlier: primary keys and foreign keys

A key is one or more columns of a relation that is used to identify a row

Primary Keys

A primary key is an attribute (column) or combination of attributes (columns) whose

val-ues uniquely identify records in an entity

Before you choose a primary key for an entity, an attribute must have the followingproperties:

• Each record of the entity must have a not-null value

• The value must be unique for each record entered into the entity

• The values must not change or become null during the life of each entity instance

• There can be only one primary key defined for an entity

Besides helping in uniquely identifying a record, the primary key also helps insearching records as an index automatically gets generated as you assign a primary key

to an attribute

An entity will have more than one attribute that can serve as a primary key Any key

candi-date keys are identified, choose one, and only one, primary key for each entity

Sometimes it requires more than one attribute to uniquely identify an entity A mary key that consists of more than one attribute is known as a composite key There can

pri-be only one primary key in an entity, but a composite key can have multiple attributes

(i.e., a primary key will be defined only once, but it can have up to 16 attributes) The

property, which allows insertion of an auto-incremented integer value into the table

when you insert a row into the table

Foreign Keys

A foreign key is an attribute that completes a relationship by identifying the parent entity

Foreign keys provide a method for maintaining integrity in the data (called referential

integrity) and for navigating between different instances of an entity Every relationship

in the model must be supported by a foreign key For example, in Figure 3-1 earlier, the

Customers and Orders tables have a primary key and foreign key relationship, where the

Orders table’s CustomerID field is the foreign key having a reference to the CustomerID

field, which is the primary key of the Customers table

C H A P T E R 3 ■ G E T T I N G TO K N O W R E L AT I O N A L D ATA B A S E S 35

Trang 14

Understanding Data Integrity

Data integrity means that data values in a database are correct and consistent There are two aspects to data integrity: entity integrity and referential integrity.

Entity Integrity

We mentioned previously in “Primary Keys” that no part of a primary key can be null.This is to guarantee that primary key values exist for all rows The requirement that pri-

mary key values exist and that they are unique is known as entity integrity (EI) The DBMS

primary key Any operation that creates a duplicate primary key or one containing nulls

is rejected That is, to establish entity integrity, you need to define primary keys so theDBMS can enforce their uniqueness

Referential Integrity

Once a relationship is defined between tables with foreign keys, the key data must be

(RI) RI requires that all foreign key values in a child table either match primary key

key constraint.

Normalization Concepts

Normalization is a technique for avoiding potential update anomalies, basically by

mini-mizing redundant data in a logical database design Normalized designs are in a sense

“better” designs because they (ideally) keep each data item in only one place ized database designs usually reduce update processing costs but can make queryprocessing more complicated These trade-offs must be carefully evaluated in terms ofthe required performance profile of a database Often, a database design needs to be

Normal-denormalized to adequately meet operational needs.

Normalizing a logical database design involves a set of formal processes to rate the data into multiple, related tables The result of each process is referred to as a

sepa-normal form Five sepa-normal forms have been identified in theory, but most of the time third normal form (3NF) is as far as you need to go in practice To be in 3NF, a relation

(the formal term for what SQL calls a table and the precise concept on which the ematical theory of normalization rests) must already be in second normal form (2NF),and 2NF requires a relation to be in first normal form (1NF) Let’s look briefly at whatthese normal forms mean

math-C H A P T E R 3 ■ G E T T I N G TO K N O W R E L AT I O N A L D ATA B A S E S

36

Trang 15

First Normal Form (1NF) In first normal form, all column values are scalar; in other words,

they have a single value that can’t be further decomposed in terms of the data model For

example, although individual characters of a string can be accessed through a procedure

that decomposes the string, only the full string is accessible by name in SQL, so, as far as

the data model is concerned, they aren’t part of the model Likewise, for a Managers table

with a manager column and a column containing a list of employees in Employees table

who work for a given manager, the manager and the list would be accessible by name,

but the individual employees in the list wouldn’t be All relations—and SQL tables—are

by definition in 1NF since the lowest level of accessibility (known as the table’s

gran-ularity) is the column level, and column values are scalars in SQL.

Second Normal Form (2NF) Second normal form requires that attributes (the formal

term for SQL columns) that aren’t parts of keys be functionally dependent on a key that

uniquely identifies them Functional dependence basically means that for a given key

value, only one value exists in a table for a column or set of columns For example, if a

table contained employees and their titles, and more than one employee could have

the same title (very likely), a key that uniquely identified employees wouldn’t uniquely

identify titles, so the titles wouldn’t be functionally dependent on a key of the table To

put the table into 2NF, you’d create a separate table for titles—with its own unique

key—and replace the title in the original table with a foreign key to the new table Note

how this reduces data redundancy The titles themselves now appear only once in the

database Only their keys appear in other tables, and key data isn’t considered

redun-dant (though, of course, it requires columns in other tables and data storage)

Third Normal Form (3NF) Third normal form extends the concept of functional

table are uniquely identified by the whole, not just part of, the primary key For example,

if you revised the hypothetical 1NF Managers-Employees table to have three columns

(ManagerName, EmployeeId, and EmployeeName) instead of two, and you defined the

composite primary key as ManagerName + EmployeeId, the table would be in 2NF (since

EmployeeName, the nonkey column, is dependent on the primary key), but it wouldn’t

be in 3NF (since EmployeeName is uniquely identified by part of the primary key defined

as column named EmployeeId) Creating a separate table for employees and removing

EmployeeName from Managers-Employees would put the table into 3NF Note that even

though this table is now normalized to 3NF, the database design is still not as normalized

as it should be Creating another table for managers using an ID shorter than the

man-ager’s name, though not required for normalization here, is definitely a better approach

and is probably advisable for a real-world database

C H A P T E R 3 ■ G E T T I N G TO K N O W R E L AT I O N A L D ATA B A S E S 37

Trang 16

Drawbacks of Normalization

Database design is an art more than a technology, and applying normalization wisely isalways important On the other hand, normalization inherently increases the number of

tables and therefore the number of operations (called joins) required to retrieve data.

Because data is not in one table, queries that have a complex join can slow things down.This can cost in the form of CPU usage: the more complex the queries, the more CPUtime is required

Denormalizing one or more tables, by intentionally providing redundant data to

reduce the number or complexity of joins to get quicker query response times, may benecessary With either normalization or denormalization, the goal is to control redun-dancy so that the database design adequately (and ideally, optimally) supports theactual use of the database

Summary

This chapter has described basic database concepts You also learned about desktopand server databases, the stages of the database life cycle, and the types of keys andhow they define relationships You also looked at normalization forms for designing abetter database

In the next chapter, you’ll start working with database queries

C H A P T E R 3 ■ G E T T I N G TO K N O W R E L AT I O N A L D ATA B A S E S

38

Trang 17

Writing Database Queries

uses T-SQL as its language, and it has a wide variety of functions and constructs for

querying Besides this, you will also be exploring new T-SQL features of SQL Server

2005 in this chapter You will see how to use SQL Server Management Studio Express

and the AdventureWorks and Northwind databases to submit queries

In this chapter, we’ll cover the following:

• Comparing QBE and SQL

• SQL Server Management Studio Express

• Beginning with queries

• Common table expressions

Trang 18

Comparing QBE and SQL

There are two main languages that have emerged for RDBMS—QBE and SQL

Query by Example (QBE) is an alternative, graphical-based, point-and-click way ofquerying a database QBE was invented by Moshé M Zloof at IBM Research during themid-1970s, in parallel to the development of SQL It differs from SQL in that it has agraphical user interface that allows users to write queries by creating example tables onthe screen QBE is especially suited for queries that are not too complex and can beexpressed in terms of a few tables

QBE was developed at IBM and is therefore an IBM trademark, but a number of othercompanies also deal with query interfaces like QBE Some systems, such as MicrosoftAccess, have been influenced by QBE and have partial support for form-based queries.Structured Query Language (SQL) is the standard relational database query lan-guage In the 1970s, a group at IBM’s San Jose Research Center (now the Almaden

Research Center) developed a database system named System R based upon Codd’s

English Query Language (SEQUEL) was designed Donald D Chamberlin and Raymond F.

Boyce at IBM were the authors of the SEQUEL language design The acronym SEQUELwas later condensed to SQL SQL was adopted as a standard by the American NationalStandards Institute (ANSI) in 1986 and then ratified by International Organization forStandardization (ISO) in 1987; this SQL standard was published as SQL 86 or SQL 1 Sincethen, the SQL standards have gone through many revisions After SQL 86, there was SQL

89 (which included a minor revision); SQL 92, also known as SQL 2 (which was a majorrevision); and then SQL 99, also known as SQL 3 (which added object-oriented featuresthat together represent the origination of the concept of ORDBMS, or object relationaldatabase management system)

Each database vendor offers its own implementation of SQL that conforms at somelevel to the standard but typically extends it T-SQL does just that, and some of the SQLused in this book may not work if you try it with a database server other than SQL Server

Tip Relational database terminology is often confusing For example, neither the meaning nor the nunciation of SQL is crystal clear IBM invented the language back in the 1970s and called it SEQUEL,changing it shortly thereafter to Structured Query Language SQL to avoid conflict with another vendor’sproduct SEQUEL and SQL were both pronounced “sequel.” When the ISO/ANSI standard was adopted, itreferred to the language simply as “database language SQL” and was silent on whether this was an acronymand how it should be pronounced Today, two pronunciations are used In the Microsoft and Oracle worlds(as well as many others), it’s pronounced “sequel.” In the DB2 and MySQL worlds (among others), it’s pro-nounced “ess cue ell.” We’ll follow the most reasonable practice We’re working in a Microsoft environment,

pro-so we’ll go with “sequel” as the pronunciation of SQL

C H A P T E R 4 ■ W R I T I N G D ATA B A S E Q U E R I E S

40

Trang 19

Beginning with Queries

A query is a technique to extract information from a database You need a query window

into which to type your query and run it so data can be retrieved from the database

Studio Express Refer back to “Using SQL Server Management Studio Express” in Chapter 2 for instructions

if you need to refresh your memory on how to connect to SSMSE

Try It Out: Running a Simple Query

select the AdventureWorks database

Figure 4-1, and then enter the following query:

Select * from Sales.SalesReason

Figure 4-1.Writing a query

C H A P T E R 4 ■ W R I T I N G D ATA B A S E Q U E R I E S 41

Trang 20

3. Click Execute (or press F5 or select Query ➤Execute), and you should see the put shown in the Results window as in Figure 4-2.

out-Figure 4-2.Query Results window

How It Works

columns from the specified table should be retrieved

Common Table Expressions

Common table expressions (CTEs) are new to SQL Server 2005 A CTE is a named

the CTE

The main advantage CTEs provide you is that the queries with derived tables becomesimpler, as traditional Transact-SQL constructs used to work with derived tables usuallyrequire a separate definition for the derived data (such as a temporary table) Using aCTE to define the derived table makes it easier to see the definition of the derived tablewith the code that uses it

C H A P T E R 4 ■ W R I T I N G D ATA B A S E Q U E R I E S

42

Trang 21

A CTE consists of three main elements:

• The column list (optional)

Try It Out: Creating a CTE

To create a CTE, enter the following query into SQL Server Management Studio Express

and execute it You should see the results shown in Figure 4-3

WITH TopSales (SalesPersonID,TerritoryID,NumberOfSales)

Figure 4-3.Using a common table expression

C H A P T E R 4 ■ W R I T I N G D ATA B A S E Q U E R I E S 43

Trang 22

How It Works

The CTE definition line in which you specify the CTE name and column list:

WITH TopSales (SalesPersonID,TerritoryID,NumberOfSales)

SELECT SalesPersonID,TerritoryID, Count(*)

map to the columns specified inside the CTE definition

By running the CTE, you will see the SalesPersonID, TerritoryID, and NumberOfSalesmade in that particular territory by a particular salesperson

GROUP BY Clause

The GROUP BYclause is used to organize output rows into groups The SELECTlist caninclude aggregate functions and produce summary values for each group Often you’llwant to generate reports from the database with summary figures for a particular column

or set of columns For example, you may want to find out the total quantity of each cardtype that expires in a specific year from the Sales.CreditCard table

Try It Out: Using the GROUP BY Clause

The Sales.CreditCard table contains the details of credit cards You need to total the cards

of a specific type that will be expiring in a particular year

Open a New Query window in SQL Server Management Studio Express Enter the lowing query and click Execute You should see the results shown in Figure 4-4

C H A P T E R 4 ■ W R I T I N G D ATA B A S E Q U E R I E S

44

Trang 23

Figure 4-4 Using GROUP BYto aggregate values

How It Works

listed in the CardType column of the CreditCard table

Select CardType, ExpYear,count(CardType) AS 'Total Cards'

from Sales.CreditCard

WHEREcondition ensures that the cards listed will be those that will expire in either 2006

Trang 24

PIVOT Operator

For example, suppose you want to query the Sales.CreditCard table in the Works database to determine the number of credit cards of a particular type that will beexpiring in specified year

but they are displayed only as part of the record and get repeated for each type of card

goal by producing a concise and easy-to-understand report format

Try It Out: Using the PIVOT Operator

The Sales.CreditCard table contains the details for customers’ credit cards You need tototal the cards of a specific type that will be expiring in a particular year

Open a New Query window in SQL Server Management Studio Express Enter the lowing query and click Execute You should see the results shown in Figure 4-5

Trang 25

Figure 4-5.Using the PIVOToperator to summarize data

How It Works

to appear in the result set

select CardType ,[2006] as Year2006,[2007] as Year2007

from

Trang 26

The ORDER BYclause will arrange the credit card names listed under CardType column

in the asscending order by the type of card

order by CardType

ROW_NUMBER() Function

unique, sequential number for each row of the returned result set

Try It Out: Using the ROW_NUMBER() Function

Studio Express Enter the following query and click Execute You should see the resultsshown in Figure 4-6

select SalesPersonID, Bonus,

ROW_NUMBER() over (order by SalesPersonID) as [RowCount]

from Sales.SalesPerson

Figure 4-6.Using the ROW_NUMBER()function

C H A P T E R 4 ■ W R I T I N G D ATA B A S E Q U E R I E S

48

Ngày đăng: 08/08/2014, 18:21

TỪ KHÓA LIÊN QUAN