summary of contents - springer978-1-4302-5377-8/1.pdf · chapter 2: core database concepts 1 5 23...

15
Summary of Contents lntroduction Chapter 1: Database lntroduction Chapter 2: Core Database Concepts 1 5 23 Chapter 3: Creating and Using Relational Databases with SQL 53 Chapter 4: Fundamentals of Database Design 101 Chapter 5: Planning the Database 139 Chapter 6: lmplementing the Database 155 Chapter 7: Database Security 177 Chapter 8: Advanced Database Feature 197 Chapter 9: Database Maintenance 225 Appendix: Available Database Plattarms 257 Index 281

Upload: doantu

Post on 26-Feb-2018

217 views

Category:

Documents


0 download

TRANSCRIPT

Summary of Contents

lntroduction

Chapter 1: Database lntroduction

Chapter 2: Core Database Concepts

1

5

23

Chapter 3: Creating and Using Relational Databases with SQL 53

Chapter 4: Fundamentals of Database Design 101

Chapter 5: Planning the Database 139

Chapter 6: lmplementing the Database 155

Chapter 7: Database Security 177

Chapter 8: Advanced Database Feature 197

Chapter 9: Database Maintenance 225

Appendix: Available Database Plattarms 257

Index 281

Practical Database Design for the Web

Chris Auld

Allan Kent

Rudy Limeback

Nigel Stanger

Thearon Willis

© 2002 Apress

Originally published by glasshaus in 2002

ISBN 978-1-59059-194-9 ISBN 978-1-4302-5377-8 (eBook) DOI 10.1007/978-1-4302-5377-8

Practical Database Design for the Web All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or

transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embodied in critical articles or reviews.

The authors and publisher have made every effort in the preparation of this book to ensure the accuracy of the information. However, the information contained in this book is sold without warranty,

either express or implied. Neither the authors, glasshaus nor its dealers or distributors will be held liable for any damages caused or alleged to be caused either directly or indirectly by this book.

Cover Image The Cover Image was provided by Raphael J. Wiszowaty

Raphael J. Wiszowaty was born in Poland in 1978 and immigrated to the United States in 1994 tagether with his family. His interest in art and design began when he was still in elementary school.

He developed a serious vision of becoming an artist and designer when he joined Gallery 37 in Chicago in 1996. His five year training involved working with different media: painting, sculpture,

graphic design, animation, video, and sound.

Currently he is a student at Columbia College Chicago, with already completed concentration in Graphie Design. He will graduate with honors and BFA in lnteractive Multimedia in June of 2003. lf

you would like to find out more about Raphael's work, please feel free to visit his site at: www.multimus.com

~shaus lobor-Nvlng clovlc:esl<>< web pn>leulonalo

© 2002 glasshaus

Trademark Acknowledgements glasshaus has endeavered to provide trademark information about all the companies and products mentioned in this book by the appropriate use of capitals. However, glasshaus cannot guarantee the accuracy of this information.

Authors Chris Auld Allan Kent

Rudy Urneback Nigel Stanger Thearon Willis

Techincal Reviewers Robin Dewson

Damien Foggon Dan Maharry Dan O'Reilly

David Schultz Alex Shiell

Rick Stones

Proof Reader Agnes Wiggers

Technical Editors Mark Waterhouse

Chris Mills

Credits

Commissioning Editor Chris Mills

Publisher Viv Emery

Managing Editor Liz Toy

Project Manager Sophie Edwards

Graphie Editors Rache! Taylor Pip Wonson

Cover Dawn Chellingworth

lndexer Bill Johncocks

About the Authors

Chris Auld Chris Auld is Chief Architect at New Zealand based software firm eMedia. Chris divides his time between architecting large systems, building mobile applications, and running areund like a headless chicken evangelising all that is "new" and "only just released". Chris is a Microsoft platform specialist and a self-professed XML geek - he even has the car number plate to prove it! Chris is a graduate of Otago University in Law and Information Science.

ln his "spare" time, he collects University Degrees and DJs Dark Progressive House music, and partakes in silly ritualistic adventure sport activities such as kitesurfing, whitewater kayaking and mountain biking.

I would like to thank: my Mum and Dad for getting me to where I am and for supporting me in where I am going; my darfing girltriend for enduring my late nights in front of the computer screen. I also need to thank the glasshaus crew for their encouragement and support, despite my being on the other side of the world with 12 hours time difference.

Allan Kent

Rudy Limeback

Allan has been programming seriously for the last nine years and other than a single blemish when he achieved a diploma in Cobol programming, is entirely self-taught. He runs his own company where they try to make a living out of making a Iot of noise and playing Quake. When that doesn't work they make a Iot of noise while doing development and design for an ad agency. Allan lives in Cape Town, South Africa with his girltriend and four cats.

Rudy Urneback is a web and SQL consultant in Toronto Canada. Rudy started as a Cobol programmer in 1971, turning to database design with IDMS soon after. Subsequent positions included database software sales, data administration and management, data warehousing, and data modeling. Rudy began using SQL with DB2 in 1987, and has also worked with SQL Server, Access, Oracle, and MySQL. His interest in the Internet started in 1996 with a personal web site. Rudy was one of the founders of the http://evolt.org/ Web developer community and remains active on its discussion Iist, helping web developers use SQL for dynamic sites. Rudy is also a regular contributor on several database forums. Visit http://rudy.ca/tor more information or just to say hello.

Nigel Stanger Nigel is a lecturer in the Department of Information Science at the University of Otago School of Business in Dunedin, New Zealand. He has almost fifteen years experience with database systems, mainly relational, but also object and other types. He teaches both introductory and advanced database courses, covering everything from database theory and SOL to database tuning and data warehouses.

He has research interests in database system performance (especially in distributed and web-based environments), wireless mobile databases and XML-based systems. His PhD thesis was on translating between data modeling representations. He's also recently been looking at spam filtering using neural networks.

He's a keen mountain biker and goes on at least one major cycle tour every year. He also enjoy car trialing and recently helped set part of the course for an event. He has been a Mac user since 1985, defiantly cheerful in a sea of Windows machines.

Thearon Willis Thearon is a Senior Systems Engineer with over 20 years programming experience. He currently develops Intranet applications, XML Web services and server-side and client-side utilities using Visual Basic .NET, ASP, DHTML, XML, JavaScript, VBScript, VB COM Components and SOL Server. He lives with his wife Margie and daughter Stephanie in the Raleigh, North Carolina area.

As always, I want to thank my wife Margie and my daughter Stephanie for the patience they have shown while I write another book. Without their Iove and support, none of this would be possible.

Table of Contents

lntroduction 1

Support and Feedback ...................................................................................... 3

1 : Database lntroduction 5

What is a Database? ........................................................................................ 5

A Short History Lesson .................................................................................... 8

Database Models ............................................................................................ 1 0

DBMS ......................................................................................................... ..... 17

Databases on the Web .................................................................................... 18

Summary ........................................................................................................ 20

2: Core Database Concepts 23

lntroduction ...... ......................................................................... ....................... 23

But ls All This Theory Actually Useful? ...................................................................... 24

What is a Data Model? .................................................................................... 25

The Relational Model of Data .......................................................................... 27

The Basics: Tables, Rows, and Columns .................................................................. 28

Domains and Data Types .......................................................................................... 31

ldentifying Rows: Primary Keys ................................................................................ 34

Linking Related Tables: Foreign Keys ........................................................................ 36

Normalization ............................................................................................................ 37

A Quick Recap .......................................................................................................... 42

Data lntegrity .................................................................................................. 42

Metadata and the Data Dictionary ................................................................. .44

Physical Data Access Methods ..................................................................... .45

Sequential Access ..................................................................................................... 46

lndexed Access .......................................................................................................... 47

Direct Access ............................................................................................................ 48

Fitting lt All Tagether ........................................................................................ 49

Summary ........................................................................................................ 50

3: Creating and Using Relational Databases with SQL 53

lntroduction ...................................................................................................... 53

A Note on the Examples ............................................................................................ 54

Structured Query Language ............................................................................ 54

A Little History ............................................................................................................ 55

Dialects ...................................................................................................................... 55

Syntax Components .................................................................................................. 56

Data Definition ................................................................................................ 57

CREATE TABLE ................. , ...................................................................................... 57

DROP TABLE ............................................................................................................ 63

ALTER TABLE ............................................................................................................ 64

CREATE INDEX, DROP INDEX ................................................................................ 66

ii

Data Manipulation ...................................................................... ... ........ ........... 67

Creating Data: INSERT .............................................................................................. 67

Updating Data: UPDATE ....... ..................................................................................... 69

Deleting Data: DELETE ............................................................................................ 70

The WHERE Clause .................................................................................................. 71

Raading Data from the Database .................................................................... 72

Querying in SOL: SELECT ............................................................ ............................ 73

Joins .......................................................................................................................... 77

Useful Reporting Features .... ................................... .............................. ...... ............. 84

Other Miscellaneous Useful Stuff .......................................... .. ................................ .. 90

Techniques for Building SELECT Quaries ...................................................... 94

A Detailed Example .................................................................................................. 95

Summary ........................................................................................................ 99

4: Fundamentals of Database Design 101

Data Modeling .............................................................................................. 1 01

Why Bother Modeling? ............................................................................................ 1 02

Modeling Entities, Attributes, and Relationships .............. ............................ 1 03

Entities ................................ .................................................................................... i 04

Attributes .................................................................................................................. 1 04

Relationships ............................................................................................................ 1 05

Common Structures ........... ........................................................................... 1 05

One-to-Many ............................................................................................................ 1 06

Many-to-Many ................................ .................................. ........................................ 1 07

One-to-Many Recursive .......................................................................................... 1 09

Many-to-Many Recursive ........................................................................................ 112

One-to-One .......................... .................................................................................... 113

E-Commerce Example Database Diagram .............................................................. 116

iii

lntegrity Constraints ...................................................................................... 116

Entity lntegrity .......................................................................................................... 117

Attribute or Domain lntegrity .................................................................................... 121

Referential lntegrity .................................................................................................. 128

Denormalizing for Performance .................................................................... 131

Using CASE Tools for Modeling .................................................................... 132

GASE Demonstration - Visio 2002 .......................................................................... 132

lmportance of Sampie Data .......................................................................... 136

Summary ...................................................................................................... 137

5: Planning the Database 139

Gather Information ........................................................................................ 140

Store Owner ............................................................................................................ 141

Customer .................................................................................................................. 144

Payment Processing ................................................................................................ 145

ldentify Entities and Attributes ...................................................................... 146

ldentify Entity Relationships .......................................................................... 14 7

Product Relationships .............................................................................................. 147

Category Relationships ............................................................................................ 150

Customer Relationships .......................................................................................... 151

Prepare Conceptual Data Model .................................................................. 151

Summary ...................................................................................................... 152

6: lmplementing the Database 155

Target Datebase System .............................................................................. 156

Naming Conventions .................................................................................... 156

Table Names ............................................................................................................ 157

Column Names ........................................................................................................ 157

iv

Associative Entities for Many-to-Many Relationships .................................... 159

Data Types and Nullability ............................................................................ 161

Character Data Types ............. ................................................................................. 161

Numeric Data Types ................................................................................................ 161

Date Data Types ................................................... ................................................... 162

Nullability ... ............................................................................................................... 162

Natural and Surrogate Primary Keys ......................................................... ... 163

Foreign Keys and Relational lntegrity ............................................................ 164

Table and Column Definitions ........................................................................ 165

Product .................................................................................................................... 166

Category ................................................................................................................ 167

ProductCategory ...................................................................................................... 168

Customer .................................................................................................................. 168

Orders .......................................................................................... ............................ 169

Orderltem ................................................................................................................ 170

The Overall Database .............................................................................................. 170

Database Indexes .......................................................................................... 171

Sampie DDL .................................................................................................. 172

Sampie SOL .................................................................................................. 173

Search for Products ................................................................................................ 173

Add a Product to a Customer's Order ("add to cart") .............................................. 17 4

Summary ...................................................................................................... 175

7: Database Security 177

Designing a Security Model .......................................................................... 178

ldentifying Your Target Audience .............................................................................. 178

Choosing an Authentication Method ........................................................................ 179

ldentifying the Type of Data Aceass Needed .......................................................... 180

V

Defining Users and Groups .......................................................................... 181

Using Strongly Typed Passwords ............................................................................ 182

Encrypt Your Passwords .......................................................................................... 182

How to Secure Your Database ...................................................................... 183

Table Permissions .................................................................................................... 183

Stored Procedure Permissions ................................................................................ 184

View Permissions .................................................................................................... 184

Setting Permissions .............. , .................................................................................. 185

lnline SQL vs. Stored Procedures .................................................... 186

Using Views to Restriet Data Access ............................................................ 191

Network Security for Your RDBMS ................................................................ 192

Summary ...................................................................................................... 193

8: Advanced Database Features 197

Advanced Quaries ........................................................................................ 197

Sub-Queries ............................................................................................................ 198

Stored Procedures and Triggers .................................................................... 205

Stored Procedures .................................................................................................. 206

Triggers .................................................................................................................... 207

Physical Database Tuning ............................................................................ 209

Query Plans, or, What's That RDBMS Thinking? .................................................... 21 0

Indexes .......................................................................................................... 213

Types of Index .......................................................................................................... 215

Managing Concurrency ................................................................................ 216

Locking .................................................................................................................... 216

Transactions ............................................................................................................ 217

Summary ...................................................................................................... 222

vi

9: Database Maintenance 225

Why a Developer Should Know This ............................................................ 226

Backup and Restore ...................................................................................... 226

Backing Up Your Database ...................................................................................... 227

Restering Your Database ..... ......... ..... .......... .... .. ...... .... .................. ..... ... ... .. ............ 231

Modifying Tables ............................................................................................ 234

Changing Columns .................................................................................................. 235

Adding Columns ....................................................................................................... 238

Dropping Columns ............................................... ..................................... .. ............ 239

Adding Indexes ........................................................................................................ 243

Reorganizing Data and Indexes .................................................................... 246

Dropping an lndex .................................................................................................... 247

Drop Existing ............................................................................................................ 248

Building Scripts for Production lmplementation ............................................ 249

Scripts to Populate Tables ........................................................................................ 249

When to Perform Database Maintenance .................................................... 254

Summary ...................................................................................................... 254

Appendix: Available Database Platforms 257

MySQL .......................................................................................................... 257

Oracle ............................................................................................................ 265

Microsoft SQL Server .................................................................................... 271

Summary ...................................................................................................... 278

Index 281

vii