The Programmer's Guide to SQL

The Programmer's Guide to SQL

The Programmer's Guide to SQL- CRISTIAN DARIE, KARLI WATSON WITH CHRIS HART, KEVIN HOFFMAN, JULIAN SKINNER APress Media, LLC The Programmer's Guide to SQL Copyright ~2003 by Cristian Darie, Karli Watson with Chris Hart, Kevin Hoffman, Julian Skinner Originally published by Apress in 2003 All rights reserved. No part of this work may be reproduced or transmitted in any form or by any means, electronic or mechanical, including photocopying, recording, or by any information storage or retrieval system, without the prior written permission of the copyright owner and the publisher. ISBN 978-1-59059-218-2 ISBN 978-1-4302-0800-6 (eBook) DOI 10.1007/978-1-4302-0800-6 Trademarked names may appear in this book. Rather than use a trademark symbol with every occurrence of a trademarked name, we use the names only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark. Technical Reviewers: Cristof Falk, Slavomir Furman, Brad Maiani, Judith M. Myerson, Johan Normen, David Schultz Editorial Board: Dan Appleman, Craig Berry, Gary Cornell, Steven Rycroft, Julian Skinner, Martin Streicher, Jim Sumser, Karen Watterson, Gavin Wray, John Zukowski Lead Editor: Tony Davis Assistant Publisher: Grace Wong Project Manager: Darren Murphy Copy Editor: Kim Wnnpsett Production Manager: Kari Brooks Production Editor: KellyWmquist Proofreader: Thistle Hill Publishing Services Compositor: Kinetic Publishing Services, LLC Indexer: John Collin Artist: Kinetic Publishing Services, LLC Cover Designer: Kurt Krames Manufacturing Manager: Tom Debolski The information in this book is distributed on an "as is" basis, without warranty. Although every precaution has been taken in the preparation of this work, neither the author(s) nor Apress shall have any liability to any person or entity with respect to any loss or damage caused or alleged to be caused directly or indirectly by the information contained in this work. Contents at a Glance About the Authors ................................................. ix Introduction ....................................................... xi Chapter 1 Understanding SQL and Relational Databases ....... 1 Chapter 2 Retrieving Data with SQL .......................... 29 Chapter 3 Modifying Data ...................................... 67 Chapter 4 Summarizing and Grouping Data ..................... 83 Chapter 5 Performing Calculations and Using Functions .... 107 Chapter 6 Combining SQL Queries ............................. 147 Chapter 1 Querying Multiple Tables ......................... 167 Chapter 8 Hiding Complex SQL with Views .................. 193 Chapter 9 Using Stored Procedures .......................... 203 Chapter 10 Transactions ....................................... 265 Chapter 11 Users and Security ................................ 301 Chapter 12 Working with Database Objects .................... 329 Chapter 13 Triggers ............................................ 363 Chapter 14 Case Study: Building a Product Catalog ......... 389 Chapter 15 Case Study: Implementing Role-Based Security ............................................ 453 Appendix A Executing SQL Statements ......................... 499 Appendix B Setting Up the InstantUniversity Database ...... 513 Appendix C Data Types ......................................... 531 Index .............................................................. 539 iii Contents About the Authors .............................................. ix Introduction ..................................................... xi Chapter 1 Understanding SQL and Relational Databases ........................................... 1 What Is SQL? ........................................................ 1 Introducing Relational Databases ................................. 7 The Power of SQL .................................................. 25 Summary ............................................................. 28 Chapter 2 Retrieving Data with SQL ..................... 29 Understanding Simple Retrieval .................................. 29 Using Aliases ...................................................... 35 Returning Calculated Columns ..................................... 37 Sorting Data ....................................................... 46 Filtering Data ..................................................... 47 Dealing with NULL Data ........................................... 63 Summary ............................................................. 64 Chapter 3 Modifying Data ................................... 67 Introducing SQL Data Modification ............................... 67 Adding New Data ................................................... 67 Changing Existing Data ........................................... 75 Deleting Data ...................................................... 78 Summary ............................................................. 81 Chapter 4 Summarizing and Grouping Data ............... 83 Summarizing Data .................................................. 83 Grouping Data ...................................................... 90 Using Analytic Functions ........................................ 103 Summary ............................................................ 106 v Contents Chapter 5 Performing Calculations and Using Functions. 107. Performing Calculations in SQL ............ ·..................... 107 Using Functions in SQL .......................................... 111 Summary ............................................................ 145 Chapter 6 Combining SQL Queries ........................ 147 Using Subqueries ................................................. 147 Combining Data from Queries ..................................... 158 Summary ............................................................ 166 Chapter 7 Querying Multiple Tables .................... 167 Understanding Simple Joins ...................................... 167 Inner vs. Outer Joins ........................................... 178 Exploring Additional Joins in Oracle .......................... 190 Summary ............................................................ 192 Chapter 8 Hiding Complex SQL with Views ............. 193 Overview of SQL Views .......................................... 193 Creating a View ................................................. 194 Modifying Data Through a View .................................. 198 Updating a View .................................................. 199 Deleting a View .................................................. 201 Summary ............................................................ 202 Chapter 9 Using Stored Procedures ...................... 203 Stored Procedure Overview ....................................... 203 Coding in Stored Procedures ..................................... 225 Using Cursors ..................................................... 234 Returning Result Sets from a Procedure ........................ 247 Error Handling ................................................... 252 Summary ............................................................ 263 vi Contents Chapter 10 Transactions ..................................... 265 What Is a Transaction? .......................................... 266 Understanding Database Transactions ............................ 268 Transactions at Work ............................................. 275 Moving onto Advanced Topics ..................................... 284 Summary ............................................................ 299 Chapter 11 Users and Security ............................ 301 Let's Talk About Security ....................................... 302 Understanding Database Security and SQL ....................... 304 Working with Access Security ................................... 307 Working with SQL Server Security ............................... 308 Working with Oracle Security ................................... 315 Working with D82 Security ....................................... 321 Working with MySQL Security ..................................... 324 Summary ............................................................ 327 Chapter 12 Working with Database Objects ............. 329 Creating and Dropping Databases ................................ 329 Creating and Dropping Data Tables .............................. 331 Copying Tables ................................................... 339 Altering Database Tables ........................................ 340 Creating Temporary Tables ....................................... 342 Using Sequences .................................................. 348 Using Autonumbered Fields ....................................... 349 Using Indexes ..................................................... 355 Understanding Declarative Referential Integrity and the FOREIGN KEY Constraint ............................... 358 Using Database Schemas .......................................... 361 Summary ............................................................. 362 Chapter 13 Triggers .......................................... 363 What Are Triggers? ............................................... 363 Working with SQL Server Triggers ............................... 367 Working with Oracle Triggers ................................... 375 Working with D82 Triggers ....................................... 382 Summary ............................................................ 387 vii Contents Chapter 14 Case Study: Building a Product Catalog ................................ 389

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    14 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us