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

OReilly learning SQL on SQL server 2005 apr 2006 ISBN 0596102151

691 140 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 691
Dung lượng 4,15 MB

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

Nội dung

If you're new to databases, or need a SQL refresher, Learning SQL on SQL Server 2005 is an ideal step-by-step introduction to this database query tool, with everything you need for progr

Trang 1

By Sikha Saha Bagui, Richard Walsh Earp

Publisher: O'Reilly Pub Date: April 2006 Print ISBN-10: 0-596-10215-1 Print ISBN-13: 978-0-59-610215-9 Pages: 342

Table of Contents | Index

Anyone who interacts with today's modern databases needs to know SQL (Structured Query Language), the standard language for generating, manipulating, and retrieving database information In recent years, the dramatic rise in the popularity of relational databases and multi-user databases has fueled a healthy demand for application

developers and others who can write SQL code efficiently and correctly.

If you're new to databases, or need a SQL refresher, Learning SQL on SQL Server 2005 is

an ideal step-by-step introduction to this database query tool, with everything you need for programming SQL using Microsoft's SQL Server 2005-one of the most powerful and popular database engines used today Plenty of books explain database theory This guide lets you apply the theory as you learn SQL You don't need prior database knowledge, or even prior computer knowledge.

Based on a popular university-level course designed by authors Sikha Saha Bagui and

Richard Walsh Earp, Learning SQL on SQL Server 2005 starts with very simple SQL

concepts, and slowly builds into more complex query development Every topic, concept, and idea comes with examples of code and output, along with exercises to help you gain proficiency in SQL and SQL Server 2005 With this book, you'll learn:

Beginning SQL commands, such as how and where to type an SQL query, and how to create, populate, alter and delete tables

How to customize SQL Server 2005's settings and about SQL Server 2005's functions About joins, a common database mechanism for combining tables

Query development, the use of views and other derived structures, and simple set operations

Trang 2

Subqueries, aggregate functions and correlated subqueries, as well as indexes and constraints that can be added to tables in SQL Server 2005

Whether you're an undergraduate computer science or MIS student, a self-learner who has

access to the new Microsoft database, or work for your company's IT department, Learning

SQL on SQL Server 2005 will get you up to speed on SQL in no time.

Trang 3

By Sikha Saha Bagui, Richard Walsh Earp

Publisher: O'Reilly Pub Date: April 2006 Print ISBN-10: 0-596-10215-1 Print ISBN-13: 978-0-59-610215-9 Pages: 342

Trang 7

Colophon

Index

Trang 8

most titles (safari.oreilly.com) For more information, contactour corporate/institutional sales department: (800) 998-9938 or

Trang 9

GD Using Access by Bagui and Earp, pp 64-77, © 2003

Pearson Education, Inc Reproduced by permission of PearsonEducation, Inc Publishing as Pearson Addison Wesley All rightsreserved

Oracle® and all Oracle-based trademarks and logos are

trademarks or registered trademarks of Oracle Corporation, Inc

in the United States and other countries O'Reilly Media, Inc isindependent of Oracle Corporation

Many of the designations used by manufacturers and sellers todistinguish their products are claimed as trademarks Wherethose designations appear in this book, and O'Reilly Media, Inc.was aware of a trademark claim, the designations have been

Trang 10

While every precaution has been taken in the preparation of thisbook, the publisher and authors assume no responsibility forerrors or omissions, or for damages resulting from the use ofthe information contained herein

ISBN: 0-596-10215-1

[M]

Trang 11

Dedicated to my father, Santosh Saha, and mother, Ranu Saha

Trang 12

SQL Server is one of the most powerful database engines usedtoday Microsoft's latest release of SQL Server, SQL Server

2005, is a comprehensive database platform that provides

secure and reliable storage for both relational and structureddata, enabling one to build and manage high-performance dataapplications SQL Server 2005's close integration with MicrosoftVisual Studio, the Microsoft Office System, and a suite of newdevelopment tools set SQL Server 2005 apart from previousversions and from other database engines This system allowsdevelopers to build, debug, and operate applications faster thenever before

SQL Server 2005 can be installed on small machines using

Microsoft Windows as well as on large servers In recent years,the computer industry has seen a dramatic increase in the

popularity of relational databases and multiuser databases, andthe computer industry needs application developers and peoplewho can write SQL code efficiently and correctly for relationaland multiuser databases

Trang 13

This book is mainly intended to be a systematic guide to

learning SQL using SQL Server 2005 a relational and multiuserdatabase The book is aimed at students who wish to learn SQLusing Microsoft's SQL Server 2005 The book is expected to beused by schools and SQL training organizations as well as bydatabase and IT professionals who are actively working withSQL Server 2005

The book starts with very simple SQL concepts, and slowly

builds into more complex query development The purpose ofthis book is to present every topic, concept, and idea with

examples of code and output Exercises have also been included

to gain SQL proficiency using SQL Server The best approach tousing this book efficiently is to read through the book with SQLServer open and active As the book is read, it will be

advantageous for you to work with and understand the

examples

If the book is used for a beginning database course, the

exercises are presented to be done by the students over thecourse of one semester at a pace of one chapter per week Theexercises are found at the end of each chapter

Due to the dramatic increase in the popularity of relational andmultiuser databases, many schools and training organizationsare using SQL Server in their database courses to teach

Trang 14

SQL (Structured Query Language) is a standard language usedfor querying, updating, and managing relational databases, andlately SQL has become the de facto standard "language" foraccessing relational databases SQL is not really as much of alanguage as it is a database query tool In this book, we

SQL is a data manipulation language, or DML).

The book is targeted at SQL Server users on the Windows

operating system, but is easily adaptable to other platforms

Trang 15

A book like this can be used in an "Introduction to Databases"course or a second database course along with textbooks like

Fundamentals of Database Systems, 4th Edition, Addison

Wesley, 2003 (Elmasri and Navathe), and Database Processing,

Fundamentals, Design & Implementation, 9th Edition, Prentice

Hall, 2003 (David Kroenke) Students could learn the databasetheory from the texts, and apply the theory using this book

(using SQL Server) as they learn SQL

This book can also be used as a standalone text in a course onlearning SQL using SQL Server 2005 This book does not

assume any prior computer knowledge

This book consists of 11 chapters Chapter 1 introduces the

user to SQL Server 2005 In Chapter 1, you will learn how toopen SQL Server 2005 using SQL Server Management Studio,load the database, and view and perform simple table

manipulations Chapter 1 also introduces the user to the queryeditor; shows you how to view, save, and print queries and

output; and how to customize SQL Server 2005's settings

Chapter 2 introduces the user/learner to some basic SQL

commands in SQL Server Chapter 3 discusses creating,

populating, altering, and deleting tables; an example relationaldatabase is built on the idea of tabular data Chapter 4

Appendix A describes the Student_course database and other

Trang 16

B provides the actual script used to create the Student_course

database Glossaries defining terms and important functions areprovided, as well as indexes of terms and functions in the book

The book is sufficient for beginning SQL users to get an

overview of what SQL Server entails and how to use SQL ManySQL programmers have based their employment on this

material The book gives a very good feel for what SQL is, andhow SQL is used in SQL Server

Trang 18

The following conventions are used in this book:

Italic

Used for URLs and for emphasis when introducing a newterm

Constant width

Used for MySQL and SQL keywords and for code examples

Constant width bold

In some code examples, highlights the statements beingdiscussed

Trang 19

In code examples, generally indicates user-defined itemssuch as variables, parameters, etc

In code examples and related discussions, qualifies a

reference by separating an object name from a componentname

Trang 20

In syntax descriptions, enclose optional items

{ }

In syntax descriptions, enclose a set of items from whichyou must choose only one

|

In syntax descriptions, separates the items enclosed incurly brackets, as in {TRUE | FALSE}

In syntax descriptions, indicates repeating elements Anellipsis also shows that statements or clauses irrelevant tothe discussion were left out

Indicates a tip, suggestion, or general note For example, we'll tell you

if a certain setting is version-specific.

Indicates a warning or caution For example, we'll tell you if a certain setting has some kind of negative impact on the system.

Trang 22

This book is here to help you get your job done In general, youmay use the code in this book in your programs and

documentation You do not need to contact us for permissionunless you're reproducing a significant portion of the code Forexample, writing a program that uses several chunks of codefrom this book does not require permission Selling or

distributing a CD-ROM of examples from O'Reilly books does

require permission Answering a question by citing this bookand quoting example code does not require permission

Incorporating a significant amount of example code from this

book into your product's documentation does require

permission

We appreciate, but do not require, attribution An attributionusually includes the title, author, publisher, and ISBN For

example: "Learning SQL on SQL Server 2005 by Sikha Saha

Bagui and Richard Walsh Earp Copyright 2006 O'Reilly Media,Inc., 0-596-10215-1."

If you feel that your use of code examples falls outside fair use

or the permission given here, feel free to contact us at

permissions@oreilly.com

Trang 23

We have tested and verified the information in this book and inthe source code to the best of our ability, but given the amount

of text and the rapid evolution of technology, you may find thatfeatures have changed or that we have made mistakes If so,please notify us by writing to:

info@oreilly.com

To ask technical questions or comment on the book, send emailto:

bookquestions@oreilly.com

As mentioned in the earlier section, we have a web site for thisbook where you can find code, errata (previously reported

errors and corrections available for public view), and other bookinformation You can access this web site at:

http://www.oreilly.com/catalog/learnsqlsvr05

For more information about this book and others, see the

O'Reilly web site:

http://www.oreilly.com

Trang 24

Our special thanks are due to our editor, Jeff Pepper, and theproduction crew at O'Reilly for putting up with all the changes

We would also like to thank President John Cavanaugh, DeanJane Halonen, and Provost Sandra Flake of the University ofWest Florida for their inspiration, encouragement, support, andtrue leadership quality We would also like to express our

gratitude to Dr Wes Little on the same endeavor

Our sincere thanks also go to Dr Ed Rodgers for his continuingsupport and encouragement throughout past years We alsoappreciate Dr Leo Terhaar, chair, Computer Science

Department, for his advice, guidance, and support, and

encouraging us to complete this book Last, but not least, wewould like to thank our fellow faculty members, Dr Jim Bezdekand Dr Norman Wilde for their continuous support and

encouragement

Trang 25

Chapter 1 Starting Microsoft SQL Server 2005

This chapter introduces SQL Server 2005 and SQL Server

2005's Management Studio and its basic workings You will

learn how to create a database, view the objects and defaulttables in a database, use a query editor, activate the database

in different ways, and create tables in the database using a loadscript The load script is available at

http://www.cs.uwf.edu/~sbagui The load script will create theStudent_course database for you This database will be used

throughout the rest of the book to learn SQL At this point, you

may want to copy the load script, SQLServer2005_load.sql, to

your working directory on your computer, before you start

working on the next section Right-click on the script on theweb site, select Save Target As, and save it to your workingdirectory

In this chapter, you will also learn how to view and modify tabledefinitions; delete a table and a database; type, parse, executeand save a query; display the results in different forms; stopexecution of a query; and print the query and results The finalsection of this chapter discusses customizing SQL Server 2005'ssettings

Trang 26

1.1 Starting Microsoft SQL Server 2005 and SQL Server 2005's Management Studio

To start Microsoft SQL Server 2005 and open up SQL Server2005's Management Studio, follow these steps:

From the Start menu, go to All Programs, select Microsoft SQLServer 2005, and then SQL Server Management Studio (as

shown in the Figure 1-1)

You will get the screen shown in Figure 1-2 This screen allowsyou to connect to Microsoft SQL Server 2005 If the server typeand server name are different from the defaults that came up,enter the appropriate server type and server name, and selectWindows Authentication Then, click Connect

Figure 1-1 Opening Microsoft SQL Server 2005

and SQL Server Management Studio

Trang 27

Your system may require a username and password for each SQL Server instance.

Once connected to the server that you typed in, you will get theMicrosoft SQL Server Management Studio screen (Figure 1-3)

Trang 29

or other types of objects, as we will soon show you

Trang 30

1.2 Creating a Database in Microsoft SQL Server 2005

Before we begin to work with Microsoft SQL Server 2005, wewill create a database To create a database, as shown in Figure1-4, right click on Databases in the Object Explorer and selectNew Database from the context menu

Figure 1-4 Creating a New Database

You will get the New Database dialog box, as shown in

Figure 1-5 We will create a database called Student_course

Figure 1-5 Typing in the database name

Trang 31

Figure 1-6 The Student_course database

Trang 32

Figure 1-6)

In order to view the Student_course database under the ObjectExplorer (on the left side of your screen) right away, you mayhave to first right-click on the Databases node and then selectRefresh

Then, as shown in Figure 1-6, you may now expand the

Databases node by clicking on the + sign beside Databasesunder the Object Explorer, and you also will see the

Student_course database node under and Databases (under theObject Explorer on the left portion of your screen), as shown in

Figure 1-7

1.2.1 Objects in the Student_course Database

A SQL Server database is a collection of many objects, such astables, views, and synonyms, defined to support activities

performed with data

Trang 33

1.2.2 Default Tables in the Student_course

Database

A database is a collection of related tables So far we have

created the Student_course database, but we have not created anytables

Figure 1-7 The Student_course database under

the Object Explorer

Figure 1-8 Viewing the Objects in the

Student_course database

Trang 34

displayed

Figure 1-9 System tables in the Student_coursedatabase

Trang 35

1.2.3 Default System Databases

master, model, msdb, and tempdb To view these default databasenodes, expand the Database node and then System Databasesnode, as shown in Figure 1-10, and you will be able to see thedefault System databases

SQL Server 2005 comes with some default System databases Figure 1-10 Default System Databases

Trang 36

of server installation as a whole and all other databases that aresubsequently created The SQL Server Management Studio

msdb is a database that contains the metadata and database

objects used by the SQL Server agent that performs scheduledactivities such as backups and replication tasks

tempdb is a temporary database or workspace recreated everytime SQL Server is restarted tempdb is used for temporary tablescreated by users and to hold intermediate results created

internally by SQL Server during query processing and sorting

Trang 37

The most important thing you do in SQL Server 2005, or in anyother database for that matter, is query the database Queries

in SQL Server 2005 are typed in the query editor The queryeditor can be opened in two ways, as discussed in the followingsubsections: (a) by right-clicking, and (b) by using the NewQuery button

The first query will be called SQLQuery1.sql by default Later wewill show you how to change the name of the query when

saving it

If the query editor is opened in this way, the Student_course

database automatically becomes the database against whichthe queries are executed, because you initially selected

Student_course and then right-clicked If we want to work in our Student_course database, we have to make sure that the

Student_course database is active If the Student_course database isnot active, we have to activate itwe show you how to do this indifferent ways in the following sections

Figure 1-11 Opening the query editor

Trang 38

Figure 1-12 The query editor

Trang 39

1.3.2 Opening the Query Editor Using the New Query Button

You can also open the query editor by selecting the New Querybutton from the top menu (leftmost icon), as shown in Figure 1-

13

Figure 1-13 Using the New Query icon

If you used the New Query icon from Figure 1-13 (without

selecting the Student_course database), you will get Figure 1-14.Here, note that the Student_course database is not the active

database; master is the active database, because SQL Server

2005 defaults to master

Figure 1-14 The query screen

Ngày đăng: 26/03/2019, 17:07

w