The SQL Guide to Pervasive PSQL. Rick F. van der Lans

Similar documents
MariaDB Crash Course. A Addison-Wesley. Ben Forta. Upper Saddle River, NJ Boston. Indianapolis. Singapore Mexico City. Cape Town Sydney.

Language. f SQL. Larry Rockoff COURSE TECHNOLOGY. Kingdom United States. Course Technology PTR. A part ofcenqaqe Learninq

Oracle Syllabus Course code-r10605 SQL

Course Details Duration: 3 days Starting time: 9.00 am Finishing time: 4.30 pm Lunch and refreshments are provided.

SQL Queries. for. Mere Mortals. Third Edition. A Hands-On Guide to Data Manipulation in SQL. John L. Viescas Michael J. Hernandez

Table of Contents. PDF created with FinePrint pdffactory Pro trial version

5. Single-row function

1 Writing Basic SQL SELECT Statements 2 Restricting and Sorting Data

"Charting the Course... MOC C: Querying Data with Transact-SQL. Course Summary

T-SQL Training: T-SQL for SQL Server for Developers

HA150 SQL Basics for SAP HANA

Oracle Database 11g: SQL Fundamentals I

"Charting the Course to Your Success!" MOC D Querying Microsoft SQL Server Course Summary

Index. Bitmap Heap Scan, 156 Bitmap Index Scan, 156. Rahul Batra 2018 R. Batra, SQL Primer,

Oracle Database 11g: SQL and PL/SQL Fundamentals

CSC Web Programming. Introduction to SQL

Querying Data with Transact SQL

HA150. SAP HANA 2.0 SPS02 - SQL and SQLScript for SAP HANA COURSE OUTLINE. Course Version: 14 Course Duration: 3 Day(s)

SQL Interview Questions

Ryan Stephens. Ron Plew Arie D. Jones. Sams Teach Yourself FIFTH EDITION. 800 East 96th Street, Indianapolis, Indiana, 46240

COPYRIGHTED MATERIAL. Contents. Chapter 1: Introducing T-SQL and Data Management Systems 1. Chapter 2: SQL Server Fundamentals 23.

AO3 - Version: 2. Oracle Database 11g SQL

Writing Queries Using Microsoft SQL Server 2008 Transact-SQL. Overview

Oracle Database: SQL and PL/SQL Fundamentals NEW

HA150. SAP HANA 2.0 SPS03 - SQL and SQLScript for SAP HANA COURSE OUTLINE. Course Version: 15 Course Duration:

Oracle Database: SQL and PL/SQL Fundamentals Ed 2

"Charting the Course... Oracle18c SQL (5 Day) Course Summary

Oracle Database 10g: SQL Fundamentals I

Optional SQL Feature Summary

"Charting the Course... Intermediate PHP & MySQL Course Summary

MySQL 5.0 Certification Study Guide

Learning Alliance Corporation, Inc. For more info: go to

ORACLE TRAINING CURRICULUM. Relational Databases and Relational Database Management Systems

Oracle Database: Introduction to SQL Ed 2

Foreword Preface Db2 Family And Db2 For Z/Os Environment Product Overview DB2 and the On-Demand Business DB2 Universal Database DB2 Middleware and

Oracle Database 10g: Introduction to SQL

UNIT-IV (Relational Database Language, PL/SQL)

6232B: Implementing a Microsoft SQL Server 2008 R2 Database

Introduction to Computer Science and Business

MySQL for Developers with Developer Techniques Accelerated

Oracle Database 10g: SQL Fundamentals I

Relational Database Index Design and the Optimizers

Course Modules for MCSA: SQL Server 2016 Database Development Training & Certification Course:

Sql Server Syllabus. Overview

Deccansoft softwareservices-microsoft Silver Learing Partner. SQL Server Syllabus

20461: Querying Microsoft SQL Server 2014 Databases

Essential SQLite3. Section Title Page

Scenario Manager User Guide. Release September 2013

Oracle SQL Developer. Oracle TimesTen In-Memory Database Support User's Guide Release 4.0 E

Querying Data with Transact-SQL

Querying Data with Transact-SQL

Teradata SQL Features Overview Version

Processor : Intel Pentium D3.0 GigaHtz

Introduction to Computer Science and Business

Querying Data with Transact-SQL (20761)

Course Outline and Objectives: Database Programming with SQL

20461: Querying Microsoft SQL Server

Oracle Developer Track Course Contents. Mr. Sandeep M Shinde. Oracle Application Techno-Functional Consultant

Listing of SQLSTATE values

COURSE OUTLINE MOC 20461: QUERYING MICROSOFT SQL SERVER 2014

Querying Data with Transact-SQL (761)

MTA Database Administrator Fundamentals Course

foreword to the first edition preface xxi acknowledgments xxiii about this book xxv about the cover illustration

Microsoft Power Tools for Data Analysis #10 Power BI M Code: Helper Table to Calculate MAT By Month & Product. Notes from Video:

Querying Data with Transact SQL Microsoft Official Curriculum (MOC 20761)

After completing this course, participants will be able to:

ORACLE TRAINING. ORACLE Training Course syllabus ORACLE SQL ORACLE PLSQL. Oracle SQL Training Syllabus

Querying Microsoft SQL Server

SQL Antipatterns. Extracted from: Avoiding the Pitfalls of Database Programming. The Pragmatic Bookshelf

Oracle Database: Introduction to SQL

Announcements (September 14) SQL: Part I SQL. Creating and dropping tables. Basic queries: SFW statement. Example: reading a table

Oracle. SQL(Structured Query Language) Introduction of DBMS. Build In Function. Introduction of RDBMS. Grouping the Result of a Query

SQL Server T-SQL Recipes. Andy Roberts Wayne Sheffield. A Problem-Solution Approach. Jonathan Gennick. Jason Brimhall David Dye

DB2 UDB: Application Programming

MySQL Database Administrator Training NIIT, Gurgaon India 31 August-10 September 2015

Unit Assessment Guide

COURSE OUTLINE: Querying Microsoft SQL Server

Relational Database Management Systems for Epidemiologists: SQL Part II

Chapter 1 Readme.doc definitions you need to know 1

Oracle SQL Tuning for Developers Workshop Student Guide - Volume I

Querying Microsoft SQL Server 2014

normalization are being violated o Apply the rule of Third Normal Form to resolve a violation in the model

Writing Queries Using Microsoft SQL Server 2008 Transact- SQL

MySQL for Developers Ed 3

20761 Querying Data with Transact SQL

[AVNICF-MCSASQL2012]: NICF - Microsoft Certified Solutions Associate (MCSA): SQL Server 2012

Oracle Database: Introduction to SQL

Relational Database Management Systems for Epidemiologists: SQL Part I

HA150 SQL Basics for SAP HANA

Computing for Medicine (C4M) Seminar 3: Databases. Michelle Craig Associate Professor, Teaching Stream

Sample Question Paper

Oracle Database: Introduction to SQL

Querying Data with Transact-SQL

Querying Microsoft SQL Server (MOC 20461C)

Course 20461C: Querying Microsoft SQL Server

Chapter 7. Introduction to Structured Query Language (SQL) Database Systems: Design, Implementation, and Management, Seventh Edition, Rob and Coronel

Querying Microsoft SQL Server 2012/2014

AVANTUS TRAINING PTE LTD

A Unit of SequelGate Innovative Technologies Pvt. Ltd. All Training Sessions are Completely Practical & Real-time

Pro JPA 2. Mastering the Java Persistence API. Apress* Mike Keith and Merrick Schnicariol

Transcription:

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 Foreword by Nancy Woodward Preface xiii xv xvii Part I 23 1 1.1 1.2 1.3 1.4 1.5 1.6 1.7 1.8 1.9 2 2.1 2.2 2.3 2.4 3 3.1 3.2 3.3 3.4 3.5 3.6 4 4.1 4.2 4.3 4.4 4.5 4.6 to Pervasive PSQL 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 The Tennis Club Sample Database Description of the Tennis Club The Contents of the Tables Integrity Constraints Installing the Software Downloading Pervasive PSQL Installation of Pervasive PSQL Installing a Query Tool Downloading SQL Statements from the Website Ready? SQL in a Nutshell Creating Databases Creating New SQL Users Creating Tables Populating Tables with Data Querying Tables 25 25 26 27 32 36 37 39 43 45 47 47 47 50 53 55 55 55 55 56 56 56 57 57 57 59 61 62 64

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 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 6.1 6.2 6.3 6.4 6.5 6.6 6.7 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 SELECT Statement: Common Elements 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 SELECT Statements, Table Expressions, and Subqueries 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? SELECT Statement: the FROM Clause 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 85 85 86 94 97 99 100 101 106 106 110 113 114 125 125 126 126 131 131 131 135 140 143 143 149 153 153 153 154 155 158 159 162 165 165 169 176 179 179 187

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 9 9.1 9.2 9.3 9.4 9.5 9.6 9.7 9.8 9.9 9.10 9.11 10 10.1 10.2 10.3 10.4 10.5 10.6 10.7 10.8 11 11.1 11.2 11.3 11.4 12 12.1 12.2 12.3 12.4 12.5 12.6 SELECT Statement: the WHERE Clause 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 SELECT Statement: SELECT Clause and Aggregation Functions Selecting All Columns (*) Expressions in the SELECT Clause Removing Duplicate Rows with DISTINCT When Are Two Rows Equal? An to Aggregation Functions COUNT Function MAX and MIN Functions SUM and AVG Functions The Top Specification SELECT Statement: the GROUP BY Clause 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 SELECT Statement: the HAVING Clause Examples of the HAVING Clause General Rule for the HAVING Clause SELECT Statement: the ORDER BY Clause Sorting on Column Names Sorting on Expressions Sorting with Sequence Numbers Sorting in Ascending and Descending Order Sorting of Null Values Contents vii 193 193 194 198 201 203 206 208 214 216 218 220 223 230 234 239 242 251 251 252 253 254 257 259 261 265 270 274 277 281 281 282 285 287 289 289 292 297 301 301 302 305 306 309 309 310 311 313 315 317

viii The SQL Guide to Pervasive PSQL 12.7 12.8 13 13.1 13.2 13.3 13.4 13.5 13.6 14 14.1 14.2 14.3 14.4 14.5 14.6 Case Insensitive Sorting Combining Table Expressions Combining with UNION Rules for Using UNION Keeping Duplicate Rows with ALL Set Operators and the Null Value Updating Tables Inserting New Rows Populating a Table with Rows from Another Table Updating Values in Rows Deleting Rows from a Table 318 319 321 321 322 324 327 328 329 331 331 331 334 337 342 343 Part III Creating Database Objects 345 15 15.1 15.2 15.3 15.4 15.5 16 16.1 16.2 16.3 16.4 16.5 16.6 16.7 16.8 16.9 17 17.1 17.2 17.3 17.4 17.5 17.6 17.7 17.8 17.9 17.10 Databases Creating Databases Retrieving and Changing Data on Databases Security of Databases Dropping Databases Creating Tables 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 Integrity Constraints Primary Keys Alternate Keys Foreign Keys The Referencing Action Naming Integrity Constraints Non-modifiable Columns Deleting Integrity Constraints Integrity Constraints and the Catalog 347 347 348 350 352 353 355 355 355 358 367 368 369 371 374 376 379 379 381 383 385 389 391 391 392 392 393

18 18.1 18.2 18.3 18.4 18.5 19 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 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 21.1 21.2 21.3 21.4 21.5 21.6 21.7 21.8 21.9 21.10 Characters Sets and Collations The Character Sets of Pervasive PSQL Assigning a Collation to a Column Sorting with Collations The SET DEFAULTCOLLATE statement Changing and Dropping Tables 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 Storage Structures and Indexes 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 Views 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 Contents ix 395 395 397 399 399 400 401 401 401 402 403 404 405 408 408 411 412 413 414 417 419 419 419 421 424 428 429 430 431 431 433 434 436 437 438 443 445 447 449 449 449 453 454 455 456 456 457 460 464

x The SQL Guide to Pervasive PSQL 22 22.1 22.2 22.3 22.4 22.5 22.6 22.7 22.8 22.9 22.10 22.11 22.12 Users and Data Security 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 465 465 466 467 468 469 473 474 477 478 480 481 482 Part IV Procedural Database Objects 483 23 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 24 24.1 24.2 24.3 25 25.1 25.2 25.3 25.4 25.5 25.6 Stored Procedures 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 Stored Functions Examples of Stored Functions Removing Stored Functions Triggers An Example of a Trigger More Complex Examples Triggers and Integrity Constraints Removing Triggers Triggers and the Catalog 485 485 486 490 491 493 495 496 502 505 509 511 514 516 521 523 525 527 527 528 528 531 531 532 536 537 537 538 540 545 547 548

31 31.1 31.2 31.3 31.4 31.5 31.6 31.7 31.8 31.9 31.10 31.11 Transaction and Multi-user Usage 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 Contents xi 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