Databases A Beginner's Guide

  Databases:

A฀Beginner’s฀Guide About the Author

Andrew฀J.฀(Andy)฀Oppel฀is฀a฀proud฀graduate฀of฀The฀Boys’฀Latin฀School฀of฀Maryland฀and฀of฀

  Transylvania฀University฀(Lexington,฀Kentucky)฀where฀he฀earned฀a฀BA฀in฀computer฀science฀ in฀1974.฀Since฀then,฀he฀has฀been฀continuously฀employed฀in฀a฀wide฀variety฀of฀information฀ technology฀positions,฀including฀programmer,฀programmer/analyst,฀systems฀architect,฀project฀ manager,฀senior฀database฀administrator,฀database฀group฀manager,฀consultant,฀database฀ designer,฀data฀modeler,฀and฀data฀architect.฀In฀addition,฀he฀has฀served฀as฀a฀part-time฀instructor฀ with฀the฀University฀of฀California,฀Berkeley,฀Extension฀for฀more฀than฀20฀years฀and฀received฀ the฀Honored฀Instructor฀Award฀for฀the฀year฀2000.฀His฀teaching฀work฀included฀developing฀three฀ courses฀for฀UC฀Extension,฀“Concepts฀of฀Database฀Management฀Systems,”฀“Introduction฀to฀ Relational฀Database฀Management฀Systems,”฀and฀“Data฀Modeling฀and฀Database฀Design.”฀He฀ also฀earned฀his฀Oracle฀9i฀Database฀Associate฀certification฀in฀2003.฀He฀is฀currently฀employed฀ as฀a฀senior฀data฀modeler฀for฀Blue฀Shield฀of฀California.฀In฀addition฀to฀computer฀systems,฀Andy฀ enjoys฀music฀(guitar฀and฀vocals),฀amateur฀radio฀(Pacific฀Division฀Vice฀Director,฀American฀ Radio฀Relay฀League),฀and฀soccer฀(Referee฀Instructor,฀U.S.฀Soccer).

  Andy฀has฀designed฀and฀implemented฀hundreds฀of฀databases฀for฀a฀wide฀range฀of฀ applications,฀including฀medical฀research,฀banking,฀insurance,฀apparel฀manufacturing,฀ telecommunications,฀wireless฀communications,฀and฀human฀resources.฀He฀is฀the฀author฀ of฀Databases฀Demystified฀(McGraw-Hill฀Professional,฀2004)฀and฀SQL฀Demystified฀ (McGraw-Hill฀Professional,฀2005),฀and฀is฀co-author฀of฀SQL:฀A฀Beginner’s฀Guide฀ (McGraw-Hill฀Professional,฀2009).฀His฀database฀product฀experience฀includes฀IMS,฀DB2,฀ Sybase฀ASE,฀Microsoft฀SQL฀Server,฀Microsoft฀Access,฀MySQL,฀and฀Oracle฀(versions฀7,฀ 8,฀8i,฀9i,฀and฀10g).

  If฀you฀have฀any฀comments,฀please฀contact฀Andy฀at฀andy@andyoppel.com.

  About the Technical Editor

Todd฀Meister฀has฀been฀developing฀using฀Microsoft฀technologies฀for฀more฀than฀ten฀years.฀

  He’s฀been฀a฀Technical฀Editor฀on฀more฀than฀50฀books฀with฀topics฀ranging฀from฀SQL฀Server฀ to฀the฀.NET฀Framework.฀In฀addition฀to฀technical฀editing,฀he฀serves฀as฀an฀Assistant฀Director฀ for฀Computing฀Services฀at฀Ball฀State฀University฀in฀Muncie,฀Indiana.฀He฀lives฀in฀central฀ Indiana฀with฀his฀wife,฀Kimberly,฀and฀their฀four฀incredible฀children.฀Contact฀Todd฀at฀฀ tmeister@sycamoresolutions.com.

  Databases:

A฀Beginner’s฀Guide

Andrew฀J.฀Oppel

  New฀York฀฀฀Chicago฀฀฀San฀Francisco฀฀฀฀ Lisbon฀฀฀London฀฀฀Madrid฀฀฀Mexico฀City฀฀฀฀ Milan฀฀฀New฀Delhi฀฀฀San฀Juan฀฀฀฀ Seoul฀฀฀Singapore฀฀฀Sydney฀฀฀Toronto

  ISBN: 978-0-07-160847-3

Copyright Act of 1976, no part of this publication may be reproduced or distributed in any form or by any means, or stored in

a database or retrieval system, without the prior written permission of the publisher.

Copyright © 2009 by The McGraw-Hill Companies. All rights reserved. Except as permitted under the United States

trademarked name, we use names in an editorial fashion only, and to the benefit of the trademark owner, with no intention of

All trademarks are trademarks of their respective owners. Rather than put a trademark symbol after every occurrence of a

The material in this eBook also appears in the print version of this title: ISBN: 978-0-07-160846-6, MHID: 0-07-160846-X.

MHID: 0-07-160847-8

McGraw-Hill eBooks are available at special quantity discounts to use as premiums and sales promotions, or for use in cor-

infringement of the trademark. Where such designations appear in this book, they have been printed with initial caps.

Information has been obtained by McGraw-Hill from sources believed to be reliable. However, because of the possibility of

porate training programs. To contact a representativ TERMS OF USE such information.

or completeness of any information and is not responsible for any errors or omissions or the results obtained from the use of

human or mechanical error by our sources, McGraw-Hill, or others, McGraw-Hill does not guarantee the accuracy, adequacy,

McGraw-Hill’s prior consent. You may use the work for your own noncommercial and personal use; any other use of the work

derivative works based upon, transmit, distribute, disseminate, sell, publish or sublicense the work or any part of it without

store and retrieve one copy of the work, you may not decompile, disassemble, reverse engineer, reproduce, modify, create

is strictly prohibited. Your right to use the work may be terminated if you fail to comply with these terms.

This is a copyrighted work and The McGraw-Hill Companies, Inc. (“McGraw-Hill”) and its licensors reserve all rights in and

to the work. Use of this work is subject to these terms. Except as permitted under the Copyright Act of 1976 and the right to

PURPOSE. McGraw-Hill and its licensors do not warrant or guarantee that the functions contained in the work will meet your

ING BUT NOT LIMITED TO IMPLIED WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR

HYPERLINK OR OTHERWISE, AND EXPRESSLY DISCLAIM ANY WARRANTY, EXPRESS OR IMPLIED, INCLUD-

USING THE WORK, INCLUDING ANY INFORMATION THAT CAN BE ACCESSED THROUGH THE WORK VIA

RANTIES AS TO THE ACCURACY, ADEQUACY OR COMPLETENESS OF OR RESULTS TO BE OBTAINED FROM

THE WORK IS PROVIDED “AS IS.” McGRAW-HILL AND ITS LICENSORS MAKE NO GUARANTEES OR WAR-

damages. This limitation of liability shall apply to any claim or cause whatsoever whether such claim or cause arises in con-

damages that result from the use of or inability to use the work, even if any of them has been advised of the possibility of such

stances shall McGraw-Hill and/or its licensors be liable for any indirect, incidental, special, punitive, consequential or similar

from. McGraw-Hill has no responsibility for the content of any information accessed through the work. Under no circum-

you or anyone else for any inaccuracy, error or omission, regardless of cause, in the work or for any damages resulting there-

requirements or that its operation will be uninterrupted or error free. Neither McGraw-Hill nor its licensors shall be liable to

tract, tort or otherwise.

  

v

Contents

  

   ฀

  

  

  

  

  

  

  

  ฀

  

  ฀ vi ฀ Databases:฀A฀Beginner’s฀Guide

  

  

  

  ฀

  

  ฀

  

  

  

  ฀ Contents฀ vii

  

  

  

  

   ฀

  

  

  

  

  ฀

  

  

  ฀ viii ฀ Databases:฀A฀Beginner’s฀Guide

  

  ฀

  

  

  

  ฀

  

  

  

   ฀

  

  

  

   ฀

  

  

  

  

  

  ฀

  

  

  

  

   ฀

  

  

  ฀ Contents฀ ix

  ฀ x ฀ Databases:฀A฀Beginner’s฀Guide

  ฀

  

  

  

   ฀

  

  ฀

  

  ฀ ฀

  Acknowledgments

  any฀people฀were฀involved฀in฀the฀development฀of฀Databases:฀A฀Beginner’s฀Guide—฀ ฀many฀of฀whom฀I฀do฀not฀know฀by฀name.฀First,฀the฀editors฀and฀staff฀at฀McGraw-Hill฀฀

M

  provided฀untold฀hours฀of฀support฀for฀this฀project.฀I฀wish฀to฀especially฀thank฀Editorial฀ Director฀Wendy฀Rinaldi฀as฀the฀individual฀who฀has฀provided฀the฀most฀advice฀and฀inspiration฀ throughout฀the฀development฀of฀all฀my฀books.฀In฀fact,฀it฀was฀Wendy฀who฀got฀me฀started฀as฀a฀ McGraw-Hill฀author.฀I฀also฀wish฀to฀thank฀Lisa฀Theobald฀for฀her฀excellent฀copy฀editing฀and฀ all฀the฀other฀editors,฀proofreaders,฀indexers,฀designers,฀illustrators,฀and฀other฀participants.฀ My฀special฀thanks฀go฀to฀Todd฀Meister,฀the฀technical฀editor,฀for฀his฀attention฀to฀detail฀and฀ his฀helpful฀inputs฀throughout฀the฀editing฀process.฀Finally,฀my฀thanks฀to฀my฀family฀for฀their฀ support฀and฀understanding฀as฀I฀fit฀the฀writing฀schedule฀into฀an฀already฀overly฀busy฀life.

  xi

  This page intentionally left blank

  Introduction

  hirty-five฀years฀ago,฀databases฀were฀found฀only฀in฀special฀research฀laboratories,฀ where฀computer฀scientists฀struggled฀with฀ways฀to฀make฀them฀efficient฀and฀useful,฀

T

  publishing฀their฀findings฀in฀countless฀research฀papers.฀Today฀databases฀are฀a฀ubiquitous฀ part฀of฀the฀information฀technology฀(IT)฀industry฀and฀business฀in฀general.฀We฀directly฀and฀ indirectly฀use฀databases฀every฀day—banking฀transactions,฀travel฀reservations,฀employment฀ relationships,฀website฀searches,฀online฀and฀offline฀purchases,฀and฀most฀other฀transactions฀ are฀recorded฀in฀and฀served฀by฀databases.

  As฀is฀the฀case฀with฀many฀fast-growing฀technologies,฀industry฀standards฀have฀lagged฀ behind฀in฀the฀development฀of฀database฀technology,฀resulting฀in฀myriad฀commercial฀ products,฀each฀following฀a฀particular฀software฀vendor’s฀vision.฀Moreover,฀a฀number฀ of฀different฀database฀models฀have฀emerged,฀with฀the฀relational฀model฀being฀the฀most฀ prevalent.฀Databases:฀A฀Beginner’s฀Guide฀examines฀all฀of฀the฀major฀database฀models,฀ including฀hierarchical,฀network,฀relational,฀object-oriented,฀and฀object-relational.฀This฀ book฀concentrates฀heavily฀on฀the฀relational฀and฀object-relational฀models,฀however,฀ because฀these฀are฀the฀mainstream฀of฀the฀IT฀industry฀and฀will฀likely฀remain฀so฀in฀the฀ foreseeable฀future.

  The฀most฀significant฀challenge฀in฀implementing฀a฀database฀is฀correctly฀designing฀the฀ structure฀of฀the฀database.฀Without฀a฀thorough฀understanding฀of฀the฀problem฀the฀database฀is฀ intended฀to฀solve,฀and฀without฀knowledge฀of฀the฀best฀practices฀for฀organizing฀the฀required฀ data,฀the฀implemented฀database฀becomes฀an฀unwieldy฀beast฀that฀requires฀constant฀attention.฀

  xiii

  ฀ xiv ฀ Databases:฀A฀Beginner’s฀Guide Databases:฀A฀Beginner’s฀Guide ฀focuses฀on฀the฀transformation฀of฀requirements฀into฀a฀

  working฀data฀model฀with฀special฀emphasis฀on฀a฀process฀called฀normalization,฀which฀ has฀proven฀to฀be฀an฀effective฀technique฀for฀designing฀relational฀databases.฀In฀fact,฀ normalization฀can฀be฀applied฀successfully฀to฀other฀database฀models.฀And,฀in฀keeping฀ with฀the฀notion฀that฀you฀cannot฀design฀an฀automobile฀if฀you฀have฀never฀driven฀one,฀the฀ Structured฀Query฀Language฀(SQL)฀is฀introduced฀so฀that฀the฀reader฀may฀“drive”฀a฀database฀ before฀delving฀into฀the฀details฀of฀designing฀one.

  I’ve฀drawn฀on฀my฀extensive฀experience฀as฀a฀database฀designer,฀administrator,฀and฀ instructor฀to฀provide฀you฀with฀this฀self-help฀guide฀to฀the฀fascinating฀and฀complex฀world฀ of฀database฀technology.฀Examples฀are฀included฀using฀both฀Microsoft฀Access฀and฀Oracle.฀ Publicly฀available฀sample฀databases฀supplied฀by฀these฀vendors฀(the฀Microsoft฀Access฀ Northwind฀database฀and฀the฀Oracle฀Human฀Resources฀database฀schema)฀are฀used฀in฀example฀ figures฀whenever฀possible฀so฀that฀you฀can฀try฀the฀examples฀directly฀on฀your฀own฀computer฀ system.฀A฀self฀test฀is฀provided฀at฀the฀end฀of฀each฀chapter฀to฀help฀reinforce฀your฀learning.

  Who Should Read This Book Databases:฀A฀Beginner’s฀Guide ฀is฀recommended฀for฀anyone฀trying฀to฀build฀a฀foundation฀

  in฀database฀design฀and฀management,฀whether฀for฀personal฀or฀professional฀use.฀The฀book฀ is฀designed฀specifically฀for฀those฀who฀are฀new฀or฀relatively฀new฀to฀database฀technology;฀ however,฀those฀of฀you฀who฀need฀a฀refresher฀in฀normalization฀and฀database฀design฀ and฀management฀will฀also฀find฀this฀book฀beneficial.฀Whether฀you’re฀an฀experienced฀ developer,฀you’ve฀had฀some฀development฀experience,฀you’re฀a฀database฀administrator,฀or฀ you’re฀new฀to฀programming฀and฀databases,฀Databases:฀A฀Beginner’s฀Guide฀provides฀a฀ strong฀foundation฀that฀will฀be฀useful฀to฀any฀of฀you฀wanting฀to฀learn฀more฀about฀database฀ technology.฀In฀fact,฀any฀of฀the฀following฀individuals฀will฀find฀this฀book฀helpful฀when฀ trying฀to฀understand฀and฀use฀databases:

  ●

  ฀ The฀novice฀new฀to฀database฀design฀and฀SQL฀programming

  ●

  ฀ The฀analyst฀or฀manager฀who฀wants฀a฀better฀understanding฀of฀฀how฀to฀design,฀ implement,฀and฀access฀databases

  ●

  ฀ The฀database฀administrator฀who฀wants฀to฀learn฀more฀about฀database฀design

  ●

  ฀ The฀technical฀support฀professional฀or฀testing/QA฀engineer฀who฀must฀perform฀ad฀hoc฀ queries฀against฀SQL฀databases

  ●

  ฀ The฀web฀developer฀writing฀applications฀that฀require฀databases฀for฀data฀persistence

  ฀ Introduction฀ xv

  ●

  ฀ The฀third-generation฀language฀(3GL)฀programmer฀embedding฀SQL฀within฀an฀ application’s฀source฀code

  ●

  ฀ Any฀other฀individual฀who฀wants฀to฀learn฀how฀to฀design฀databases฀and฀write฀SQL฀code฀ to฀create฀and฀access฀databases฀within฀an฀RDBMS No฀matter฀which฀category฀you฀fit฀into,฀you฀must฀remember฀that฀the฀book฀is฀geared฀ toward฀anyone฀wanting฀to฀learn฀standard฀database฀design฀techniques฀that฀work฀on฀any฀ database,฀not฀one฀specific฀vendor’s฀product.฀This฀lets฀you฀apply฀the฀skills฀you฀learn฀in฀ this฀book฀to฀real-world฀situations,฀without฀being฀limited฀to฀product฀standards.฀You฀will,฀ of฀course,฀still฀need฀to฀be฀aware฀of฀how฀the฀product฀you฀work฀on฀implements฀databases,฀ particularly฀dialects฀of฀SQL,฀but฀with฀the฀foundation฀provided฀in฀these฀pages,฀you’ll฀ be฀able฀to฀move฀from฀one฀RDBMS฀to฀the฀next฀and฀still฀have฀a฀solid฀understanding฀of฀ database฀design฀theory.฀As฀a฀result,฀you’ll฀find฀that฀this฀book฀is฀a฀useful฀tool฀to฀anyone฀ new฀to฀databases,฀particularly฀relational฀databases,฀regardless฀of฀the฀product฀used.฀You฀ will฀easily฀be฀able฀to฀adapt฀your฀knowledge฀to฀the฀specific฀RDBMS.

  What the Book Covers Databases:฀A฀Beginner’s฀Guide ฀is฀divided฀into฀three฀parts.฀Part฀I฀introduces฀you฀to฀basic฀

  database฀concepts฀and฀explains฀how฀to฀create฀and฀access฀objects฀within฀your฀database฀ using฀SQL.฀Part฀II฀provides฀you฀with฀a฀foundation฀in฀database฀development,฀including฀ the฀database฀life฀cycle,฀logical฀design฀using฀the฀normalization฀process,฀transforming฀the฀ logical฀design฀into฀a฀physical฀database,฀and฀data฀and฀process฀modeling.฀Part฀III฀focuses฀ on฀database฀implementation฀with฀emphasis฀on฀database฀security,฀as฀well฀as฀the฀advanced฀ topics฀of฀databases฀for฀online฀analytical฀processing฀(OLAP)฀and฀integrating฀objects฀and฀

  XML฀documents฀into฀the฀database,฀allowing฀you฀to฀expand฀on฀what฀you฀learned฀in฀Parts฀I฀ and฀II.฀In฀addition฀to฀the฀three฀parts,฀Databases:฀A฀Beginner’s฀Guide฀contains฀appendices฀ that฀include฀answers฀to฀the฀self-test฀questions฀and฀solutions฀to฀the฀Try฀This฀exercises฀that฀ appear฀throughout฀the฀book.

  Content Description

  The฀following฀outline฀describes฀the฀contents฀of฀the฀book฀and฀shows฀how฀the฀book฀is฀ broken฀down฀into฀task-focused฀chapters:

  Part I: Database Concepts Part฀I฀introduces฀you฀to฀basic฀database฀concepts฀and฀explains฀how฀to฀create฀and฀access฀ objects฀within฀your฀database฀using฀SQL. This฀chapter฀introduces฀fundamental฀concepts฀ Chapter฀1:฀Database฀Fundamentals฀

  and฀definitions฀regarding฀databases,฀including฀properties฀common฀to฀databases,฀prevalent฀

  ฀ xvi ฀ Databases:฀A฀Beginner’s฀Guide

  database฀models,฀a฀brief฀history฀of฀databases,฀and฀the฀rationale฀for฀focusing฀on฀the฀ relational฀model.

  This฀chapter฀explores฀

  Chapter฀2:฀Exploring฀Relational฀Database฀Components฀

  the฀conceptual,฀logical,฀and฀physical฀components฀that฀make฀up฀the฀relational฀model.฀

  Conceptual฀database฀design ฀involves฀studying฀and฀modeling฀the฀data฀in฀a฀technology-

  independent฀manner.฀Logical฀database฀design฀is฀the฀process฀of฀translating,฀or฀mapping,฀ the฀conceptual฀design฀into฀a฀logical฀design฀that฀fits฀the฀chosen฀database฀model฀(relational,฀ object-oriented,฀object-relational,฀and฀so฀on).฀The฀final฀design฀step฀is฀physical฀database฀

  design, ฀which฀involves฀mapping฀the฀logical฀design฀to฀one฀or฀more฀physical฀designs—each฀

  tailored฀to฀the฀particular฀DBMS฀that฀will฀manage฀the฀database฀and฀the฀particular฀computer฀ system฀on฀which฀the฀database฀will฀run.

  This฀chapter฀provides฀an฀overview฀

  Chapter฀3:฀Forms-based฀Database฀Queries฀

  of฀forming฀and฀running฀database฀queries฀using฀the฀forms-based฀query฀tool฀in฀Microsoft฀ Access,฀providing฀a฀foundation฀in฀database฀query฀concepts฀for฀the฀database฀design฀theory฀ that฀follows฀in฀later฀chapters.

  This฀chapter฀introduces฀SQL,฀which฀has฀become฀

  Chapter฀4:฀Introduction฀to฀SQL฀

  the฀universal฀language฀for฀relational฀databases฀that฀nearly฀every฀DBMS฀in฀modern฀use฀ supports.฀The฀reason฀for฀its฀wide฀acceptance฀is฀clearly฀the฀time฀and฀effort฀that฀went฀into฀ the฀development฀of฀language฀features฀and฀standards,฀making฀SQL฀highly฀portable฀across฀ different฀RDBMS฀products.

  Part II: Database Development Part฀II฀provides฀you฀with฀a฀foundation฀in฀database฀development,฀including฀the฀database฀

  life฀cycle,฀logical฀design฀using฀the฀normalization฀process,฀transforming฀the฀logical฀design฀ into฀a฀physical฀database,฀and฀data฀and฀process฀modeling.

  This฀chapter฀introduces฀the฀framework฀in฀which฀

  Chapter฀5:฀The฀Database฀Life฀Cycle฀

  database฀design฀takes฀place,฀a฀useful฀precursor฀to฀the฀particulars฀of฀database฀design.฀The฀

  life฀cycle฀ of฀a฀database฀(or฀computer฀system)฀is฀the฀term฀we฀use฀for฀all฀the฀events฀that฀take฀

  place฀between฀the฀time฀we฀first฀recognize฀the฀need฀for฀a฀database,฀continuing฀through฀its฀ development฀and฀deployment,฀and฀finally฀ending฀with฀the฀day฀it฀is฀retired฀from฀service.

  In฀this฀chapter,฀you฀will฀learn฀

  Chapter฀6:฀Database฀Design฀Using฀Normalization฀

  how฀to฀perform฀logical฀database฀design฀using฀a฀process฀called฀normalization.฀In฀terms฀of฀ understanding฀relational฀database฀technology,฀this฀is฀the฀most฀important฀topic฀in฀this฀book,฀ because฀normalization฀teaches฀you฀how฀best฀to฀organize฀your฀data฀into฀tables.

  In฀this฀chapter,฀we฀will฀look฀at฀entity-

  Chapter฀7:฀Data฀and฀Process฀Modeling฀

  relationship฀diagrams฀(ERDs)฀and฀data฀modeling฀in฀more฀detail.฀The฀second฀part฀of฀the฀ chapter฀includes฀a฀high-level฀survey฀of฀process฀design฀concepts฀and฀diagramming฀techniques.

  ฀ Introduction฀ xvii

  Chapter฀8:฀Physical฀Database฀Design฀ This฀chapter฀focuses฀on฀the฀database฀

  designer’s฀physical฀design฀work,฀which฀is฀transforming฀the฀logical฀database฀design฀into฀ one฀or฀more฀physical฀database฀designs.

  Part III: Database Implementation Part฀III฀focuses฀on฀database฀implementation฀with฀emphasis฀on฀database฀security฀as฀well฀as฀

  the฀advanced฀topics฀of฀databases฀for฀online฀analytical฀processing฀(OLAP)฀and฀integrating฀ objects฀and฀Extensible฀Markup฀Language฀(XML)฀documents฀into฀the฀database;฀this฀allows฀ you฀to฀expand฀on฀what฀you฀learned฀in฀Parts฀I฀and฀II.

Chapter฀9:฀Connecting฀Databases฀to฀the฀Outside฀World฀ This฀chapter฀begins฀with฀

  a฀look฀at฀the฀evolution฀of฀database฀deployment฀models,฀meaning฀the฀ways฀that฀databases฀ have฀been฀connected฀with฀the฀database฀users฀and฀the฀other฀computer฀systems฀within฀the฀ enterprise฀computing฀infrastructure฀(the฀internal฀structure฀that฀organizes฀all฀the฀computing฀ resources฀of฀an฀enterprise,฀including฀databases,฀applications,฀computer฀hardware,฀and฀ the฀network).฀The฀chapter฀then฀explores฀the฀methods฀used฀to฀connect฀databases฀to฀ applications฀that฀use฀a฀web฀browser฀as฀the฀primary฀user฀interface,฀which฀is฀the฀way฀many฀ modern฀application฀systems฀are฀constructed.฀It฀concludes฀with฀a฀look฀at฀current฀methods฀ for฀connecting฀databases฀to฀applications,฀namely฀using฀ODBC฀connections฀(for฀most฀ programming฀languages)฀and฀various฀methods฀for฀connecting฀databases฀to฀applications฀ written฀in฀Java฀(a฀commonly฀used฀object-oriented฀language).

  Chapter฀10:฀Database฀Security฀ This฀chapter฀presents฀the฀need฀for฀security,฀the฀

  security฀considerations฀for฀deploying฀database฀servers฀and฀clients฀that฀access฀those฀ servers,฀and฀methods฀for฀implementing฀database฀access฀security,฀concluding฀with฀a฀ discussion฀of฀security฀monitoring฀and฀auditing.

  Chapter฀11:฀Deploying฀Databases฀ This฀chapter฀covers฀some฀considerations฀

  regarding฀the฀development฀of฀applications฀that฀use฀the฀database฀system.฀These฀include฀ cursor฀processing,฀transaction฀management,฀performance฀tuning,฀and฀change฀control.

  Chapter฀12:฀Databases฀for฀Online฀Analytical฀Processing฀ This฀chapter฀presents฀the฀

  concepts฀of฀databases฀for฀analytical฀processing,฀including฀data฀warehouses฀and฀data฀marts,฀ an฀overview฀of฀data฀mining฀and฀other฀data฀analysis฀techniques,฀along฀with฀the฀design฀ variations฀required฀for฀these฀types฀of฀databases.

  Chapter฀13:฀Integrating฀XML฀Documents฀and฀Objects฀into฀Databases฀ This฀ chapter฀explores฀a฀number฀of฀ways฀to฀integrate฀XML฀and฀object฀content฀into฀databases.

  Part IV: Appendices The฀appendices฀include฀answers฀to฀the฀Self฀Test฀questions฀and฀solutions฀to฀the฀Try฀This฀ exercises฀that฀appear฀throughout฀the฀book.

  ฀ xviii ฀ Databases:฀A฀Beginner’s฀Guide Appendix฀A:฀Answers฀to฀Self฀Tests฀ This฀appendix฀provides฀the฀answers฀to฀the฀Self฀ Test฀questions฀listed฀at฀the฀end฀of฀each฀chapter. Appendix฀B:฀Solutions฀to฀the฀Try฀This฀Exercises฀ This฀appendix฀contains฀solutions,฀

  including฀diagrams฀and฀applicable฀SQL฀code,฀for฀the฀Try฀This฀exercises฀that฀appear฀in฀ nearly฀every฀chapter฀of฀the฀book.

  Chapter Content

  As฀you฀can฀see฀from฀the฀outline,฀Databases:฀A฀Beginner’s฀Guide฀is฀organized฀into฀ chapters.฀Each฀chapter฀focuses฀on฀a฀set฀of฀key฀skills฀and฀concepts฀and฀contains฀the฀ background฀information฀you฀need฀to฀understand฀the฀concepts,฀plus฀the฀skills฀required฀ to฀apply฀these฀concepts.฀Each฀chapter฀contains฀additional฀elements฀to฀help฀you฀better฀ understand฀the฀information฀covered฀in฀that฀chapter:

  Ask the Expert

  Each฀chapter฀contains฀one฀or฀two฀Ask฀the฀Expert฀sections฀that฀provide฀information฀on฀ questions฀that฀might฀arise฀regarding฀the฀information฀presented฀in฀the฀chapter.

  Self Test

  Each฀chapter฀ends฀with฀a฀Self฀Test,฀a฀set฀of฀questions฀that฀test฀you฀on฀the฀information฀and฀ skills฀you฀learned฀in฀that฀chapter.฀The฀answers฀to฀the฀Self฀Tests฀are฀included฀in฀Appendix฀A.

  Try This Exercises

  Most฀chapters฀contain฀one฀or฀two฀Try฀This฀exercises฀that฀allow฀you฀to฀apply฀the฀information฀ that฀you฀learned฀in฀the฀chapter.฀Each฀exercise฀is฀broken฀down฀into฀steps฀that฀walk฀you฀ through฀the฀process฀of฀completing฀a฀particular฀task.฀Where฀applicable,฀the฀exercises฀include฀

  

  related฀files฀that฀you฀can฀download฀from฀our฀website฀at Click฀ Computing฀and฀then฀click฀the฀Downloads฀Section฀link฀on฀the฀left฀side฀of฀the฀page.฀On฀the฀ downloads฀page,฀scroll฀down฀to฀the฀listing฀for฀this฀book฀and฀select฀the฀files฀you฀wish฀to฀ download.฀The฀files฀usually฀include฀the฀SQL฀statements฀or฀diagrams฀used฀within฀the฀Try฀ This฀exercise.

  To฀complete฀many฀of฀the฀Try฀This฀exercises฀in฀this฀book,฀you’ll฀need฀to฀have฀access฀to฀ an฀RDBMS฀that฀allows฀you฀to฀enter฀and฀execute฀SQL฀statements฀interactively.฀If฀you’re฀ accessing฀an฀RDBMS฀over฀a฀network,฀check฀with฀the฀database฀administrator฀to฀make฀sure฀ that฀you’re฀logging฀in฀with฀the฀credentials฀necessary฀to฀create฀a฀database฀and฀schema.฀You฀ might฀need฀special฀permissions฀to฀create฀these฀objects.฀Also฀verify฀whether฀you฀should฀ include฀any฀particular฀parameters฀when฀creating฀the฀database฀(for฀example,฀log฀file฀size),฀ and฀whether฀restrictions฀on฀the฀names฀you฀can฀use฀or฀other฀restrictions฀apply.฀Be฀sure฀to฀ check฀the฀appropriate฀documentation฀before฀working฀with฀any฀database฀product. Part

  

I

Database Concepts

  This page intentionally left blank Chapter

  1 Database Fundamentals

  3

  ฀ 4 ฀ Databases:฀A฀Beginner’s฀Guide Key฀Skills฀&฀Concepts

  ●

  ฀ Properties฀of฀a฀Database

  ●

  ฀ Prevalent฀Database฀Models

  ●

  ฀ A฀Brief฀History฀of฀Databases

  ● Why฀Focus฀on฀Relational?

  ฀ his฀chapter฀introduces฀fundamental฀concepts฀and฀definitions฀regarding฀databases,฀ including฀properties฀common฀to฀databases,฀prevalent฀database฀models,฀a฀brief฀history฀of฀

  T databases,฀and฀the฀rationale฀for฀focusing฀on฀the฀relational฀model.

Properties of a Database

  A฀database฀is฀a฀collection฀of฀interrelated฀data฀items฀that฀are฀managed฀as฀a฀single฀unit.฀This฀ definition฀is฀deliberately฀broad฀because฀so฀much฀variety฀exists฀across฀the฀various฀software฀ vendors฀that฀provide฀database฀systems.฀For฀example,฀Microsoft฀Access฀places฀the฀entire฀ database฀in฀a฀single฀data฀file,฀so฀an฀Access฀database฀can฀be฀defined฀as฀the฀file฀that฀contains฀ the฀data฀items.฀Oracle฀Corporation฀defines฀its฀database฀as฀a฀collection฀of฀physical฀files฀that฀ are฀managed฀by฀an฀instance฀of฀its฀database฀software฀product.฀An฀instance฀is฀a฀copy฀of฀the฀ database฀software฀running฀in฀memory.฀Microsoft฀SQL฀Server฀and฀Sybase฀Adaptive฀Server฀ Enterprise฀(ASE)฀define฀a฀database฀as฀a฀collection฀of฀data฀items฀that฀have฀a฀common฀ owner,฀and฀multiple฀databases฀are฀typically฀managed฀by฀a฀single฀instance฀of฀the฀database฀ management฀software.฀This฀can฀all฀be฀quite฀confusing฀if฀you฀work฀with฀multiple฀products,฀ because,฀for฀example,฀a฀database฀as฀defined฀by฀Microsoft฀SQL฀Server฀or฀Sybase฀ASE฀is฀ exactly฀what฀Oracle฀Corporation฀calls฀a฀schema.

  A฀database฀object฀is฀a฀named฀data฀structure฀that฀is฀stored฀in฀a฀database.฀The฀specific฀ types฀of฀database฀objects฀supported฀in฀a฀database฀vary฀from฀vendor฀to฀vendor฀and฀from฀ one฀database฀model฀to฀another.฀Database฀model฀refers฀to฀the฀way฀in฀which฀a฀database฀ organizes฀its฀data฀to฀pattern฀the฀real฀world.฀The฀most฀common฀database฀models฀are฀ presented฀in฀the฀“Prevalent฀Database฀Models”฀section฀later฀in฀this฀chapter.

  A฀file฀is฀a฀collection฀of฀related฀records฀that฀are฀stored฀as฀a฀single฀unit฀by฀an฀operating฀ system.฀Given฀the฀unfortunately฀similar฀definitions฀of฀files฀and฀databases,฀how฀can฀we฀

  ฀ Chapter฀1:฀ Database฀Fundamentals฀

  5

  make฀a฀distinction?฀A฀number฀of฀Unix฀operating฀system฀vendors฀call฀their฀password฀files฀ “databases,”฀yet฀database฀experts฀will฀quickly฀point฀out฀that,฀in฀fact,฀these฀are฀not฀actually฀ databases.฀Clearly,฀we฀need฀a฀bit฀more฀rigor฀in฀our฀definitions.฀The฀answer฀lies฀in฀an฀ understanding฀of฀certain฀characteristics฀or฀properties฀that฀databases฀possess฀which฀are฀not฀ found฀in฀ordinary฀files,฀including฀the฀following:

  ●

  ฀ Management฀by฀a฀database฀management฀system฀(DBMS)

  ●

  ฀ Layers฀of฀data฀abstraction

  ●

  ฀ Physical฀data฀independence

  ●

  ฀ Logical฀data฀independence These฀properties฀are฀discussed฀in฀the฀following฀subsections.

The Database Management System

  The฀database฀management฀system฀(DBMS)฀is฀software฀provided฀by฀the฀database฀vendor.฀ Software฀products฀such฀as฀Microsoft฀Access,฀Oracle,฀Microsoft฀SQL฀Server,฀Sybase฀ASE,฀ DB2,฀Ingres,฀and฀MySQL฀are฀all฀DBMSs.฀If฀it฀seems฀odd฀to฀you฀that฀the฀DBMS฀acronym฀ is฀used฀instead฀of฀merely฀DMS,฀remember฀that฀the฀term฀database฀was฀originally฀written฀as฀ two฀words,฀and฀by฀convention฀has฀since฀become฀a฀single฀compound฀word.

  The฀DBMS฀provides฀all฀the฀basic฀services฀required฀to฀organize฀and฀maintain฀the฀ database,฀including฀the฀following:

  ● ฀ Moves฀data฀to฀and฀from฀the฀physical฀data฀files฀as฀needed. ●

  ฀ Manages฀concurrent฀data฀access฀by฀multiple฀users,฀including฀provisions฀to฀prevent฀ simultaneous฀updates฀from฀conflicting฀with฀one฀another.

  ●

  ฀ Manages฀transactions฀so฀that฀each฀transaction’s฀database฀changes฀are฀an฀all-or-nothing฀ unit฀of฀work.฀In฀other฀words,฀if฀the฀transaction฀succeeds,฀all฀database฀changes฀made฀by฀ it฀are฀recorded฀in฀the฀database;฀if฀the฀transaction฀fails,฀none฀of฀the฀changes฀it฀made฀are฀ recorded฀in฀the฀database.

  ●

  ฀ Supports฀a฀query฀language,฀which฀is฀a฀system฀of฀commands฀that฀a฀database฀user฀ employs฀to฀retrieve฀data฀from฀the฀database.

  ● ฀ Provides฀provisions฀for฀backing฀up฀the฀database฀and฀recovering฀from฀failures. ● ฀ Provides฀security฀mechanisms฀to฀prevent฀unauthorized฀data฀access฀and฀modification.

  ฀ 6 ฀ Databases:฀A฀Beginner’s฀Guide Ask the Expert

  ฀ I’ve฀heard฀the฀term฀“data฀bank”฀used.฀What฀is฀the฀difference฀between฀a฀data฀bank฀and฀ Q: a฀database?

  ฀ A฀data฀bank฀and฀a฀database฀are฀the฀same฀thing.฀Databank฀is฀merely฀an฀older฀term฀that฀was฀

A:

  used฀by฀the฀scientists฀who฀developed฀early฀database฀systems.฀In฀fact,฀the฀term฀data฀bank฀is฀ still฀used฀in฀a฀few฀human฀languages,฀such฀as฀banco฀de฀dados฀in฀Portuguese.

  Layers of Data Abstraction

  Databases฀are฀unique฀in฀their฀ability฀to฀present฀multiple฀users฀with฀their฀own฀distinct฀views฀ of฀the฀data฀while฀storing฀the฀underlying฀data฀only฀once.฀These฀are฀collectively฀called฀user฀

  views .฀A฀user฀in฀this฀context฀is฀any฀person฀or฀application฀that฀signs฀on฀to฀the฀database฀for฀

  the฀purpose฀of฀storing฀and/or฀retrieving฀data.฀An฀application฀is฀a฀set฀of฀computer฀programs฀ designed฀to฀solve฀a฀particular฀business฀problem,฀such฀as฀an฀order-entry฀system,฀a฀payroll- processing฀system,฀or฀an฀accounting฀system.

  When฀an฀electronic฀spreadsheet฀application฀such฀as฀Microsoft฀Excel฀is฀used,฀all฀ users฀must฀share฀a฀common฀view฀of฀the฀data,฀and฀that฀view฀must฀match฀the฀way฀the฀ data฀is฀physically฀stored฀in฀the฀underlying฀data฀file.฀If฀a฀user฀hides฀some฀columns฀in฀ a฀spreadsheet,฀reorders฀the฀rows,฀and฀saves฀the฀spreadsheet,฀the฀next฀user฀who฀opens฀ the฀spreadsheet฀will฀view฀the฀data฀in฀the฀manner฀in฀which฀the฀first฀user฀saved฀it.฀An฀ alternative,฀of฀course,฀is฀for฀each฀user฀to฀save฀his฀or฀her฀own฀copy฀in฀separate฀physical฀ files,฀but฀then฀as฀one฀user฀applies฀updates,฀the฀other฀users’฀data฀becomes฀out฀of฀date.฀ Database฀systems฀present฀each฀user฀a฀view฀of฀the฀same฀data,฀but฀the฀views฀can฀be฀tailored฀ to฀the฀needs฀of฀the฀individual฀users,฀even฀though฀they฀all฀come฀from฀one฀commonly฀stored฀ copy฀of฀the฀data.฀Because฀views฀store฀no฀actual฀data,฀they฀automatically฀reflect฀any฀data฀ changes฀made฀to฀the฀underlying฀database฀objects.฀This฀is฀all฀possible฀through฀layers฀of฀

  ,฀which฀is฀shown฀in฀Figure฀1-1.

  abstraction

  The฀architecture฀shown฀in฀Figure฀1-1฀was฀first฀developed฀by฀ANSI/SPARC฀(American฀ National฀Standards฀Institute/Standards฀Planning฀and฀Requirements฀Committee)฀in฀ the฀1970s฀and฀quickly฀became฀a฀foundation฀for฀much฀of฀the฀database฀research฀and฀ development฀efforts฀that฀followed.฀Most฀modern฀DBMSs฀follow฀this฀architecture,฀which฀ is฀composed฀of฀three฀primary฀layers:฀the฀physical฀layer,฀the฀logical฀layer,฀and฀the฀external฀ layer.฀The฀original฀architecture฀included฀a฀conceptual฀layer,฀which฀has฀been฀omitted฀here฀ because฀none฀of฀the฀modern฀database฀vendors฀implement฀it.

  ฀ Chapter฀1:฀ Database฀Fundamentals฀

  7 External Layer View 1 View 2 View 3 Logical Data Independence

  

Internal Schema

Logical Layer

(Logical Schema)

Physical Data

  Independence Physical Layer

File File File File File

Database Database Database Database Database

  Figure฀1-1 Database layers of abstraction

The Physical Layer

  The฀physical฀layer฀contains฀the฀data฀files฀that฀hold฀all฀the฀data฀for฀the฀database.฀Nearly฀all฀ modern฀DBMSs฀allow฀the฀database฀to฀be฀stored฀in฀multiple฀data฀files,฀which฀are฀usually฀ spread฀out฀over฀multiple฀physical฀disk฀drives.฀With฀this฀arrangement,฀the฀disk฀drives฀can฀ work฀in฀parallel฀for฀maximum฀performance.฀A฀notable฀exception฀among฀the฀DBMSs฀used฀ as฀examples฀in฀this฀book฀is฀Microsoft฀Access,฀which฀stores฀the฀entire฀database฀in฀a฀single฀ physical฀file.฀While฀simplifying฀database฀use฀on฀a฀single-user฀personal฀computer฀system,฀ this฀arrangement฀limits฀the฀ability฀of฀the฀DBMS฀to฀scale฀to฀accommodate฀many฀concurrent฀ users฀of฀the฀database,฀making฀it฀inappropriate฀as฀a฀solution฀for฀large฀enterprise฀systems.฀ In฀all฀fairness,฀Microsoft฀Access฀was฀not฀designed฀to฀be฀a฀robust฀enterprise฀class฀DBMS.฀I฀ have฀included฀it฀in฀discussions฀in฀this฀book฀not฀because฀it฀competes฀with฀products฀such฀as฀ Oracle฀and฀SQL฀Server,฀but฀because฀it’s฀a฀great฀example฀of฀a฀personal฀DBMS฀with฀a฀user฀ interface฀that฀makes฀learning฀database฀concepts฀easy฀and฀fun.

  The฀database฀user฀does฀not฀need฀to฀understand฀how฀the฀data฀is฀actually฀stored฀within฀ the฀data฀files฀or฀even฀which฀file฀contains฀the฀data฀item(s)฀of฀interest.฀In฀most฀organizations,฀ a฀technician฀known฀as฀a฀database฀administrator฀(DBA)฀handles฀the฀details฀of฀installing฀ and฀configuring฀the฀database฀software฀and฀data฀files฀and฀making฀the฀database฀available฀to฀ users.฀The฀DBMS฀works฀with฀the฀computer’s฀operating฀system฀to฀manage฀the฀data฀files฀ automatically,฀including฀all฀file฀opening,฀closing,฀reading,฀and฀writing฀operations.฀The฀database฀

  ฀ 8 ฀ Databases:฀A฀Beginner’s฀Guide

  user฀should฀not฀be฀required฀to฀refer฀to฀physical฀data฀files฀when฀using฀a฀database,฀which฀is฀ in฀sharp฀contrast฀with฀spreadsheets฀and฀word฀processing,฀where฀the฀user฀must฀consciously฀ save฀the฀document(s)฀and฀choose฀file฀names฀and฀storage฀locations.฀Many฀of฀the฀personal฀ computer–based฀DBMSs฀are฀exceptions฀to฀this฀tenet฀because฀the฀user฀is฀required฀to฀locate฀ and฀open฀a฀physical฀file฀as฀part฀of฀the฀process฀of฀signing฀on฀to฀the฀DBMS.฀Conversely,฀ with฀enterprise฀class฀DBMSs฀(such฀as฀Oracle,฀Sybase฀ASE,฀Microsoft฀SQL฀Server,฀and฀ MySQL),฀the฀physical฀files฀are฀managed฀automatically฀and฀the฀database฀user฀never฀needs฀to฀ refer฀to฀them฀when฀using฀the฀database.

The Logical Layer

  The฀logical฀layer฀or฀logical฀model฀comprises฀the฀first฀of฀two฀layers฀of฀abstraction฀in฀the฀ database:฀the฀physical฀layer฀has฀a฀concrete฀existence฀in฀the฀operating฀system฀files,฀whereas฀ the฀logical฀layer฀exists฀only฀as฀abstract฀data฀structures฀assembled฀from฀the฀physical฀layer฀as฀ needed.฀The฀DBMS฀transforms฀the฀data฀in฀the฀data฀files฀into฀a฀common฀structure.฀This฀layer฀ is฀sometimes฀called฀the฀schema,฀a฀term฀used฀for฀the฀collection฀of฀all฀the฀data฀items฀stored฀in฀ a฀particular฀database฀or฀belonging฀to฀a฀particular฀database฀user.฀Depending฀on฀the฀particular฀ DBMS,฀this฀layer฀can฀contain฀a฀set฀of฀two-dimensional฀tables,฀a฀hierarchical฀structure฀ similar฀to฀a฀company’s฀organization฀chart,฀or฀some฀other฀structure.฀The฀“Prevalent฀Database฀ Models”฀section฀later฀in฀this฀chapter฀describes฀the฀possible฀structures฀in฀more฀detail.

The External Layer

  The฀external฀layer฀or฀external฀model฀is฀the฀second฀layer฀of฀abstraction฀in฀the฀database.฀ This฀layer฀is฀composed฀of฀the฀user฀views฀discussed฀earlier,฀which฀are฀collectively฀ called฀the฀subschema.฀In฀this฀layer,฀the฀database฀users฀(application฀programs฀as฀well฀ as฀individuals)฀that฀access฀the฀database฀connect฀and฀issue฀queries฀against฀the฀database.฀ Ideally,฀only฀the฀DBA฀deals฀with฀the฀physical฀and฀logical฀layers.฀The฀DBMS฀handles฀the฀ transformation฀of฀selected฀items฀from฀one฀or฀more฀data฀structures฀in฀the฀logical฀layer฀ to฀form฀each฀user฀view.฀The฀user฀views฀in฀this฀layer฀can฀be฀predefined฀and฀stored฀in฀the฀ database฀for฀reuse,฀or฀they฀can฀be฀temporary฀items฀that฀are฀built฀by฀the฀DBMS฀to฀hold฀the฀ results฀of฀a฀single฀ad฀hoc฀database฀query฀until฀they฀are฀no฀longer฀needed฀by฀the฀database฀ user.฀An฀ad฀hoc฀query฀is฀a฀query฀that฀is฀not฀preconceived฀and฀that฀is฀not฀likely฀to฀be฀ reused.฀Views฀are฀discussed฀in฀more฀detail฀in฀Chapter฀2.

Physical Data Independence

  The฀ability฀to฀alter฀the฀physical฀file฀structure฀of฀a฀database฀without฀disrupting฀existing฀ users฀and฀processes฀is฀known฀as฀physical฀data฀independence.฀As฀shown฀in฀Figure฀1-1,฀the฀ separation฀of฀the฀physical฀layer฀from฀the฀logical฀layer฀provides฀physical฀data฀independence฀

  ฀ Chapter฀1:฀ Database฀Fundamentals฀

  9