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 other- wise, 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 1.1 Introduction 25 1.2 , Database , and Database Language 26 1.3 The 27 1.4 What Is SQL? 32 1.5 The History of SQL 36 1.6 From Monolithic via /Server to the Internet 37 1.7 Standardization of SQL 39 1.8 The History of Pervasive PSQL 43 1.9 The Structure of This Book 45

2 The Tennis Club Sample Database 47 2.1 Introduction 47 2.2 Description of the Tennis Club 47 2.3 The Contents of the Tables 50 2.4 Integrity Constraints 53

3 Installing the Software 55 3.1 Introduction 55 3.2 Downloading Pervasive PSQL 55 3.3 Installation of Pervasive PSQL 55 3.4 Installing a Query Tool 56 3.5 Downloading SQL Statements from the Website 56 3.6 Ready? 56

4 SQL in a Nutshell 57 4.1 Introduction 57 4.2 Creating 57 4.3 Creating New SQL Users 59 4.4 Creating Tables 61 4.5 Populating Tables with Data 62 4.6 Querying Tables 64 vi | The SQL Guide to Pervasive PSQL

4.7 Updating and Deleting Rows 66 4.8 Optimizing Query Processing with Indexes 68 4.9 Views 69 4.10 Users and Data Security 70 4.11 Deleting Database Objects 71 4.12 Grouping of SQL Statements 71 4.13 The Catalog Tables 72 4.14 Definitions of SQL Statements 80

Part II Querying and Updating Data 83

5 SELECT Statement: Common Elements 85 5.1 Introduction 85 5.2 Literals and their Data Types 86 5.3 Expressions 94 5.4 Assigning Names to Result Columns 97 5.5 The Column Specification 99 5.6 The System Variable 100 5.7 The Case Expression 101 5.8 The Scalar Expression Between Round Brackets 106 5.9 The Scalar Function 106 5.10 Casting of Expressions 110 5.11 The Null Value as an Expression 113 5.12 The Compound Scalar Expression 114 5.13 The Aggregation Function and the Scalar Subquery 125 5.14 The Row Expression 125 5.15 The Expression 126 5.16 Answers 126

6 SELECT Statements, Table Expressions, and Subqueries 131 6.1 Introduction 131 6.2 The Definition of the SELECT Statement 131 6.3 Processing the Clauses in a Select Block 135 6.4 Possible Forms of a Table Expression 140 6.5 What is a SELECT Statement? 143 6.6 What is a Subquery? 143 6.7 Answers 149

7 SELECT Statement: the FROM Clause 153 7.1 Introduction 153 7.2 Table Specifications in the FROM Clause 153 7.3 Again, the Column Specification 154 7.4 Multiple Table Specifications in the FROM Clause 155 7.5 Pseudonyms for Table Names 158 7.6 Various Examples of Joins 159 7.7 Mandatory Use of Pseudonyms 162 7.8 Accessing Tables of Different Databases 165 7.9 Explicit Joins in the FROM Clause 165 7.10 Outer Joins 169 7.11 Additional Conditions in the Join Condition 176 7.12 The Cross Join 179 7.13 The FROM Clause with Table Expressions 179 7.14 Answers 187

Contents | vii

8 SELECT Statement: the WHERE Clause 193 8.1 Introduction 193 8.2 Conditions Using Comparison Operators 194 8.3 Comparison Operators with Subqueries 198 8.4 Comparison Operators with Correlated Subqueries 201 8.5 Conditions Coupled with AND, OR, and NOT 203 8.6 The IN Operator with Expression List 206 8.7 The IN Operator with Subquery 208 8.8 The BETWEEN Operator 214 8.9 The LIKE Operator 216 8.10 The IS NULL Operator 218 8.11 The EXISTS Operator 220 8.12 The ALL and ANY Operators 223 8.13 Scope of Columns in Subqueries 230 8.14 More Examples with Correlated Subqueries 234 8.15 Conditions with Negation 239 8.16 Answers 242

9 SELECT Statement: SELECT Clause and Aggregation Functions 251 9.1 Introduction 251 9.2 Selecting All Columns (*) 252 9.3 Expressions in the SELECT Clause 253 9.4 Removing Duplicate Rows with DISTINCT 254 9.5 When Are Two Rows Equal? 257 9.6 An Introduction to Aggregation Functions 259 9.7 COUNT Function 261 9.8 MAX and MIN Functions 265 9.9 SUM and AVG Functions 270 9.10 The Top Specification 274 9.11 Answers 277

10 SELECT Statement: the GROUP BY Clause 281 10.1 Introduction 281 10.2 Grouping on One Column 282 10.3 Grouping on Two or More Columns 285 10.4 Grouping on Expressions 287 10.5 Grouping of Null Values 289 10.6 General Rules for the GROUP BY Clause 289 10.7 Complex Examples with GROUP BY 292 10.8 Answers 297

11 SELECT Statement: the HAVING Clause 301 11.1 Introduction 301 11.2 Examples of the HAVING Clause 302 11.3 General Rule for the HAVING Clause 305 11.4 Answers 306

12 SELECT Statement: the ORDER BY Clause 309 12.1 Introduction 309 12.2 Sorting on Column Names 310 12.3 Sorting on Expressions 311 12.4 Sorting with Sequence Numbers 313 12.5 Sorting in Ascending and Descending Order 315 12.6 Sorting of Null Values 317 viii | The SQL Guide to Pervasive PSQL

12.7 Case Insensitive Sorting 318 12.8 Answers 319

13 Combining Table Expressions 321 13.1 Introduction 321 13.2 Combining with UNION 322 13.3 Rules for Using UNION 324 13.4 Keeping Duplicate Rows with ALL 327 13.5 Set Operators and the Null Value 328 13.6 Answers 329

14 Updating Tables 331 14.1 Introduction 331 14.2 Inserting New Rows 331 14.3 Populating a Table with Rows from Another Table 334 14.4 Updating Values in Rows 337 14.5 Deleting Rows from a Table 342 14.6 Answers 343

Part III Creating Database Objects 345

15 Databases 347 15.1 Introduction 347 15.2 Creating Databases 348 15.3 Retrieving and Changing Data on Databases 350 15.4 Security of Databases 352 15.5 Dropping Databases 353

16 Creating Tables 355 16.1 Introduction 355 16.2 Creating New Tables 355 16.3 Data Types of Columns 358 16.4 Creating Temporary Tables 367 16.5 Naming Tables and Columns 368 16.6 Default Value of a column 369 16.7 Table Options 371 16.8 Tables and the Catalog 374 16.9 Answers 376

17 Integrity Constraints 379 17.1 Introduction 379 17.2 Primary Keys 381 17.3 Alternate Keys 383 17.4 Foreign Keys 385 17.5 The Referencing Action 389 17.6 Naming Integrity Constraints 391 17.7 Non-modifiable Columns 391 17.8 Deleting Integrity Constraints 392 17.9 Integrity Constraints and the Catalog 392 17.10 Answers 393

Contents | ix

18 Characters Sets and Collations 395 18.1 Introduction 395 18.2 The Character Sets of Pervasive PSQL 397 18.3 Assigning a Collation to a Column 399 18.4 Sorting with Collations 399 18.5 The SET DEFAULTCOLLATE statement 400

19 Changing and Dropping Tables 401 19.1 Introduction 401 19.2 Removing Entire Tables 401 19.3 Renaming Tables 402 19.4 Changing Table Characteristics 403 19.5 Adding Columns 404 19.6 Moving Columns 405 19.7 Renaming Columns 408 19.8 Changing Columns 408 19.9 Dropping Columns 411 19.10 Adding Integrity Constraints 412 19.11 Dropping Integrity Constraints 413 19.12 Decoupling Tables from Files 414 19.13 Answers 417

20 Storage Structures and Indexes 419 20.1 Introduction 419 20.2 Rows, Tables, and Files 419 20.3 How does an Index Work? 421 20.4 Processing a SELECT statement: the Steps 424 20.5 Creating Indexes 428 20.6 Defining Partial Indexes 429 20.7 Non-modifiable Indexes 430 20.8 Decoupling Indexes from Files 431 20.9 The Table Option LINKDUP 431 20.10 Dropping Indexes 433 20.11 Indexes and Primary Keys 434 20.12 Indexes and Foreign Keys 436 20.13 The Big PLAYERS_XXL Table 437 20.14 Choosing columns for Indexes 438 20.15 Hints, Indexes, and the Performance of Queries 443 20.16 Indexes and the Catalog 445 20.17 Answers 447

21 Views 449 21.1 Introduction 449 21.2 Creating Views 449 21.3 The Column Names of Views 453 21.4 Views and Privileges 454 21.5 Deleting Views 455 21.6 Views and the Catalog 456 21.7 Restrictions on Changing the Contents of Views 456 21.8 Processing View Statements 457 21.9 Application Areas of Views 460 21.10 Answers 464 x | The SQL Guide to Pervasive PSQL

22 Users and Data Security 465 22.1 Introduction 465 22.2 Adding and Removing users 466 22.3 Changing the Names of Users 467 22.4 Changing Passwords 468 22.5 Granting Table and Column Privileges 469 22.6 Granting Database Privileges 473 22.7 Assigning Users to Groups 474 22.8 Recording Privileges in the Catalog 477 22.9 Revoking Privileges 478 22.10 Security of and through Views 480 22.11 Protecting the Catalog Tables 481 22.12 Answers 482

Part IV Procedural Database Objects 483

23 Stored Procedures 485 23.1 Introduction 485 23.2 An Example of a 486 23.3 The Parameters of a Stored Procedure 490 23.4 The Body of a Stored Procedure 491 23.5 Local Variables 493 23.6 The SET Statement 495 23.7 Flow Control Statements 496 23.8 Calling Stored Procedures 502 23.9 Querying Data with SELECT INTO 505 23.10 Error Messages, SQLSTATE, and the SIGNAL Statement 509 23.11 The BEGIN ATOMIC Statement 511 23.12 A Set of Rows as Output 514 23.13 Retrieving Data with a Cursor 516 23.14 Updating and Deleting Data with a Cursor 521 23.15 The Options of a Stored Procedure 523 23.16 Stored Procedures and the Memory 525 23.17 Stored Procedures and the Catalog 527 23.18 Removing Stored Procedures 527 23.19 Security with Stored Procedures 528 23.20 Advantages of Stored Procedures 528

24 Stored Functions 531 24.1 Introduction 531 24.2 Examples of Stored Functions 532 24.3 Removing Stored Functions 536

25 Triggers 537 25.1 Introduction 537 25.2 An Example of a Trigger 538 25.3 More Complex Examples 540 25.4 Triggers and Integrity Constraints 545 25.5 Removing Triggers 547 25.6 Triggers and the Catalog 548

Contents | xi

31 Transaction and Multi-user Usage 549 31.1 Introduction 549 31.2 What is a Transaction? 549 31.3 Starting of Transactions 553 31.4 Savepoints 554 31.5 Stored Procedures and Transactions 556 31.6 Problems with Multi-user Usage 557 31.7 Locking 560 31.8 Deadlocks 561 31.9 The Isolation Level 561 31.10 Waiting for a Lock 562 31.11 Answers 562

Appendices 565

A Syntax of SQL 565

B Scalar Functions 603

C System Stored Procedures 623

D Bibliography 633

Index 635