Downloading SQL Statements from the Website ..... was a member of the Dutch
ISO committee responsible for the ISO SQL .... entitled Het SQL Leerboek.
The SQL Guide to Pervasive PSQL Rick F. van der Lans
Copyright © 2009 by R20/Consultancy All rights reserved; no part of this publication may be reproduced, stored in a retrieval system, or transmitted in any form of by any means, electronic, mechanical, photocopying, recording or otherwise, without the prior written permission of the publisher. Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in this book, and the publisher was aware of a trademark claim, the designations have been printed with initial capital letters or in all capitals. The author and publisher have taken care in the preparation of this book, but make no expressed or implied warranty of any kind and assume no responsibility for errors or omissions. No liability is assumed for incidental or consequential damages in connection with or arising out of the use of the information or programs contained herein. Author: Rick F. van der Lans Title: The SQL Guide to Pervasive PSQL First Lulu edition 2009 Uploaded October 09 ISBN: 978-0-557-10543-4 Published by Lulu www.lulu.com Edited by Diane Cools Book design by R20/Consultancy
Contents About the Author
xiii
Foreword by Nancy Woodward
xv
Preface
xvii
Part I
Introduction
23
1
Introduction to Pervasive PSQL
25 25 26 27 32 36 37 39 43 45
2
The Tennis Club Sample Database
47 47 47 50 53
3
Installing the Software
55 55 55 55 56 56 56
4
SQL in a Nutshell
57 57 57 59 61 62 64
1.1 1.2 1.3 1.4 1.5 1.6 1.7 1.8 1.9
2.1 2.2 2.3 2.4
3.1 3.2 3.3 3.4 3.5 3.6
4.1 4.2 4.3 4.4 4.5 4.6
Introduction Database, Database Server, and Database Language The Relational Model What Is SQL? The History of SQL From Monolithic via Client/Server to the Internet Standardization of SQL The History of Pervasive PSQL The Structure of This Book
Introduction Description of the Tennis Club The Contents of the Tables Integrity Constraints
Introduction Downloading Pervasive PSQL Installation of Pervasive PSQL Installing a Query Tool Downloading SQL Statements from the Website Ready?
Introduction Creating Databases Creating New SQL Users Creating Tables Populating Tables with Data Querying Tables
vi | The SQL Guide to Pervasive PSQL
4.7 4.8 4.9 4.10 4.11 4.12 4.13 4.14
Updating and Deleting Rows Optimizing Query Processing with Indexes Views Users and Data Security Deleting Database Objects Grouping of SQL Statements The Catalog Tables Definitions of SQL Statements
66 68 69 70 71 71 72 80
Part II
Querying and Updating Data
83
5
SELECT Statement: Common Elements
85 85 86 94 97 99 100 101 106 106 110 113 114 125 125 126 126
6
SELECT Statements, Table Expressions, and Subqueries
131 131 131 135 140 143 143 149
7
SELECT Statement: the FROM Clause
153 153 153 154 155 158 159 162 165 165 169 176 179 179 187
5.1 5.2 5.3 5.4 5.5 5.6 5.7 5.8 5.9 5.10 5.11 5.12 5.13 5.14 5.15 5.16
6.1 6.2 6.3 6.4 6.5 6.6 6.7
7.1 7.2 7.3 7.4 7.5 7.6 7.7 7.8 7.9 7.10 7.11 7.12 7.13 7.14
Introduction Literals and their Data Types Expressions Assigning Names to Result Columns The Column Specification The System Variable The Case Expression The Scalar Expression Between Round Brackets The Scalar Function Casting of Expressions The Null Value as an Expression The Compound Scalar Expression The Aggregation Function and the Scalar Subquery The Row Expression The Table Expression Answers
Introduction The Definition of the SELECT Statement Processing the Clauses in a Select Block Possible Forms of a Table Expression What is a SELECT Statement? What is a Subquery? Answers
Introduction Table Specifications in the FROM Clause Again, the Column Specification Multiple Table Specifications in the FROM Clause Pseudonyms for Table Names Various Examples of Joins Mandatory Use of Pseudonyms Accessing Tables of Different Databases Explicit Joins in the FROM Clause Outer Joins Additional Conditions in the Join Condition The Cross Join The FROM Clause with Table Expressions Answers
Contents | vii
8
8.1 8.2 8.3 8.4 8.5 8.6 8.7 8.8 8.9 8.10 8.11 8.12 8.13 8.14 8.15 8.16
SELECT Statement: the WHERE Clause
193 193 194 198 201 203 206 208 214 216 218 220 223 230 234 239 242
Introduction Conditions Using Comparison Operators Comparison Operators with Subqueries Comparison Operators with Correlated Subqueries Conditions Coupled with AND, OR, and NOT The IN Operator with Expression List The IN Operator with Subquery The BETWEEN Operator The LIKE Operator The IS NULL Operator The EXISTS Operator The ALL and ANY Operators Scope of Columns in Subqueries More Examples with Correlated Subqueries Conditions with Negation Answers
9.1 9.2 9.3 9.4 9.5 9.6 9.7 9.8 9.9 9.10 9.11
Introduction Selecting All Columns (*) Expressions in the SELECT Clause Removing Duplicate Rows with DISTINCT When Are Two Rows Equal? An Introduction to Aggregation Functions COUNT Function MAX and MIN Functions SUM and AVG Functions The Top Specification Answers
SELECT Statement: SELECT Clause and Aggregation Functions
251 251 252 253 254 257 259 261 265 270 274 277
10
SELECT Statement: the GROUP BY Clause
281 281 282 285 287 289 289 292 297
11
SELECT Statement: the HAVING Clause
301 301 302 305 306
12
SELECT Statement: the ORDER BY Clause
309 309 310 311 313 315 317
9
10.1 10.2 10.3 10.4 10.5 10.6 10.7 10.8
11.1 11.2 11.3 11.4
12.1 12.2 12.3 12.4 12.5 12.6
Introduction Grouping on One Column Grouping on Two or More Columns Grouping on Expressions Grouping of Null Values General Rules for the GROUP BY Clause Complex Examples with GROUP BY Answers
Introduction Examples of the HAVING Clause General Rule for the HAVING Clause Answers
Introduction Sorting on Column Names Sorting on Expressions Sorting with Sequence Numbers Sorting in Ascending and Descending Order Sorting of Null Values
viii | The SQL Guide to Pervasive PSQL
12.7 12.8
Case Insensitive Sorting Answers
318 319
13
Combining Table Expressions
321 321 322 324 327 328 329
14
Updating Tables
Introduction Inserting New Rows Populating a Table with Rows from Another Table Updating Values in Rows Deleting Rows from a Table Answers
331 331 331 334 337 342 343
Part III
Creating Database Objects
345
15
Databases
347 347 348 350 352 353
16
Creating Tables
355 355 355 358 367 368 369 371 374 376
17
Integrity Constraints
379 379 381 383 385 389 391 391 392 392 393
13.1 13.2 13.3 13.4 13.5 13.6
14.1 14.2 14.3 14.4 14.5 14.6
15.1 15.2 15.3 15.4 15.5
16.1 16.2 16.3 16.4 16.5 16.6 16.7 16.8 16.9
17.1 17.2 17.3 17.4 17.5 17.6 17.7 17.8 17.9 17.10
Introduction Combining with UNION Rules for Using UNION Keeping Duplicate Rows with ALL Set Operators and the Null Value Answers
Introduction Creating Databases Retrieving and Changing Data on Databases Security of Databases Dropping Databases
Introduction Creating New Tables Data Types of Columns Creating Temporary Tables Naming Tables and Columns Default Value of a column Table Options Tables and the Catalog Answers
Introduction Primary Keys Alternate Keys Foreign Keys The Referencing Action Naming Integrity Constraints Non-modifiable Columns Deleting Integrity Constraints Integrity Constraints and the Catalog Answers
Contents | ix
18
Characters Sets and Collations
395 395 397 399 399 400
19
Changing and Dropping Tables
401 401 401 402 403 404 405 408 408 411 412 413 414 417
20
Storage Structures and Indexes
419 419 419 421 424 428 429 430 431 431 433 434 436 437 438 443 445 447
21
Views
449 449 449 453 454 455 456 456 457 460 464
18.1 18.2 18.3 18.4 18.5
19.1 19.2 19.3 19.4 19.5 19.6 19.7 19.8 19.9 19.10 19.11 19.12 19.13
20.1 20.2 20.3 20.4 20.5 20.6 20.7 20.8 20.9 20.10 20.11 20.12 20.13 20.14 20.15 20.16 20.17
21.1 21.2 21.3 21.4 21.5 21.6 21.7 21.8 21.9 21.10
Introduction The Character Sets of Pervasive PSQL Assigning a Collation to a Column Sorting with Collations The SET DEFAULTCOLLATE statement
Introduction Removing Entire Tables Renaming Tables Changing Table Characteristics Adding Columns Moving Columns Renaming Columns Changing Columns Dropping Columns Adding Integrity Constraints Dropping Integrity Constraints Decoupling Tables from Files Answers
Introduction Rows, Tables, and Files How does an Index Work? Processing a SELECT statement: the Steps Creating Indexes Defining Partial Indexes Non-modifiable Indexes Decoupling Indexes from Files The Table Option LINKDUP Dropping Indexes Indexes and Primary Keys Indexes and Foreign Keys The Big PLAYERS_XXL Table Choosing columns for Indexes Hints, Indexes, and the Performance of Queries Indexes and the Catalog Answers
Introduction Creating Views The Column Names of Views Views and Privileges Deleting Views Views and the Catalog Restrictions on Changing the Contents of Views Processing View Statements Application Areas of Views Answers
x | The SQL Guide to Pervasive PSQL
22
Users and Data Security
Introduction Adding and Removing users Changing the Names of Users Changing Passwords Granting Table and Column Privileges Granting Database Privileges Assigning Users to Groups Recording Privileges in the Catalog Revoking Privileges Security of and through Views Protecting the Catalog Tables Answers
465 465 466 467 468 469 473 474 477 478 480 481 482
Part IV
Procedural Database Objects
483
23
Stored Procedures
485 485 486 490 491 493 495 496 502 505 509 511 514 516 521 523 525 527 527 528 528
22.1 22.2 22.3 22.4 22.5 22.6 22.7 22.8 22.9 22.10 22.11 22.12
23.1 23.2 23.3 23.4 23.5 23.6 23.7 23.8 23.9 23.10 23.11 23.12 23.13 23.14 23.15 23.16 23.17 23.18 23.19 23.20
Introduction An Example of a Stored Procedure The Parameters of a Stored Procedure The Body of a Stored Procedure Local Variables The SET Statement Flow Control Statements Calling Stored Procedures Querying Data with SELECT INTO Error Messages, SQLSTATE, and the SIGNAL Statement The BEGIN ATOMIC Statement A Set of Rows as Output Retrieving Data with a Cursor Updating and Deleting Data with a Cursor The Options of a Stored Procedure Stored Procedures and the Memory Cache Stored Procedures and the Catalog Removing Stored Procedures Security with Stored Procedures Advantages of Stored Procedures
24.1 24.2 24.3
Introduction Examples of Stored Functions Removing Stored Functions
Stored Functions
531 531 532 536
25
Triggers
537 537 538 540 545 547 548
24
25.1 25.2 25.3 25.4 25.5 25.6
Introduction An Example of a Trigger More Complex Examples Triggers and Integrity Constraints Removing Triggers Triggers and the Catalog
Contents | xi
Transaction and Multi-user Usage Introduction What is a Transaction? Starting of Transactions Savepoints Stored Procedures and Transactions Problems with Multi-user Usage Locking Deadlocks The Isolation Level Waiting for a Lock Answers
549 549 549 553 554 556 557 560 561 561 562 562
Appendices
565
A
Syntax of SQL
565
B
Scalar Functions
603
C
System Stored Procedures
623
D
Bibliography
633
Index
635
31
31.1 31.2 31.3 31.4 31.5 31.6 31.7 31.8 31.9 31.10 31.11