Upload
others
View
6
Download
0
Embed Size (px)
Citation preview
Instant SQL Programming
Joe Celko
Wrox Press Ltd.®
INSTANT
Table of Contents Introduction 1
What Can SQL Do for Me? 2 Who Should Use This Book? 2 How To Use This Book 3 What You Should Know 3
Conventions Used 3 Program Code 4 Fonts and Styles 4
Watcom SQL engine 5 Product Details 5 Requirements 5 Installation Instructions 6 Screen Display 6 Error Trapping 8
Tell Us What You Think 8
Chapter 1: The Nature of SQL 11
Procedural Versus Declarative Languages 12 SQL - the Shopper's Language 12
SQL Basics 15 Vendors and the Relational Model 15
Sets Versus Sequences 17 Tables and Files 19 An Example of Structure Considerations 20 Rows 2 1 Columns 22 The Spreadsheet Analogy 22
The SQL Sub-languages 23
Table of Contents
Data Definition Language - DDL 23 Data Manipulation Language- DML 24 Data Control Language - DCL 25
Transaction Control 26 Integrity 26 Interactive SQL Tools 27
Connecting to the Example Database 28 Creating the Connection 28 Logging In to the Database 29 Logging Out of the Database 29 Alternative SQL Systems 29
Introduction to the Example Database 30 Syntax Awareness 3 1
Terminal 32 Non-Terminal 32 Example of Syntax 33
Exercises 39 Summary 40
Chapter 2: Defining a Database 43
Languages and Their Underlying Model 44 The Set IVIodei 44 Schema and Table 45 Common Conceptual Errors 47
Table Terminology 48 Cardinality of a Table 48 Degree of a Table 48 Domain Compatible Data Types 48 Union Compatible Rows 49
Manipulating Tables 49 Guidelines for Defining Schema and Tables 50
Making Rules for Data 50 Create Table Statements 5 1 SQL Data Types 54
Numeric Types 54 Exact Numerics 55 Approximate Numerics 55
Character Types 56 Temporal Data Types 57
The Default Clause 58 Column Constraints 59
Not Null Constraint 60 Check() Constraint 62 Unique and Primary Key Constraints 63
• •
XII
References 64 Comments 66 The Example Database 67 Other Table Manipulation Commands 73
Drop Table 74 Alter Table 75
Summary 78
Chapter 3: SQL Arithmetic, Logic and the NULL 81
Numeric Type Conversion 82 Rounding and Truncation 82
Rounding 82 Truncation 83
CASTQ or CONVERTO Function 83 Four Function Math 84
Vendor Math Functions 86 Problems of String Equality 86 String Functions 88
The Substring Function 88 The Fold Functions 88 The Trim Function 89 Length and Position 89
Vendor String Functions 90 Reverse 90 Flip 90 Numtowords 91
Displaying Dates 91 Timestamps 91 Times 92 Intervals 92 Date Arithmetic 93 DATE and TIME Functions 93
Week Functions 94 Comparison Operators in SQL 95
Converting Data Types 96 Logic and SQL 97
Boolean Operators for Three Values 98 Unknown Logical Values 99
The Dreaded NULL 100 Arithmetic and NULLS 100 Missing Values 101 What NULLS Are Not! 104 Comparing NULLS 104 Converting Values to and from NULL 105
xiii
Table of Contents
NULLIFO Function 105 COALESCEO Function 106
Concatenating NULLs 106 Advice Time 106 Row Comparisons in SQL-92 107 Other Logical Operators 109
Summary 110
Chapter 4: Keys and Referential Integrity 113
Definition of a Key 114 Types of Keys 115 A Brief Look at Normalization 117
First Normal Form (Inf) 118 Deletion Update and Insertion Anomalies 120
Second Normal Form (2nf) 120 Third Normal Form (3nf) 123 Other Normal Forms 124
The Example Database 125 Referential Integrity and Other Constraints 126
Check Constraint 127 The UNIQUE Constraint 128 The PRIMARY KEY Constraint 130 Foreign Key Constraints 130 Restricts and Cascades 132
Referential Actions 134 Triggers 136
The Example Database: Revisited 137 Summary 140
Chapter 5: Manipulating Database Information 143
The Structure of the Example Database 144
Using the Interactive SQL Engine 145 The INSERT INTO Command 145
Value List Insertion 146 Query Insertion 146
Inserting Data into the Example Database 146 Selective INSERTing 148 Import and Export of Bulk Data 149
Vtyua\H\& av\d Oe\etm& Rows 149 The DELETE FROM Statement 149 The UPDATE Statement 150 The WHERE Clause 151
Summary 154 Exercises 154
Chapter 6: Querying the Database - the SELECT command 157
SELECT Command Basics 158 The Simple Single Table SELECT Statement 161
SELECT and ALTER TABLE 161 Creating New Columns - the AS Operator 163
How the SELECT Statement Works 163 The WHERE Clause 166
Boolean Search Conditions 168 Predicates with the WHERE clause 169
The LIKE Predicate 170 NULLS and Empty Strings 172 The IN Predicate 172 The BETWEEN Predicate 174 IS NULL, IS TRUE, IS FALSE and IS UNKNOWN 176
The ORDER BY Clause 177 Summary 179 Exercises 180
Chapter 7: Joining Tables 183
Two or More Tables in a SELECT Statement 184 Cross Joins 185 Equi Joins 187
An Equi Join Example: at the Bank 187 An Example from the Database 188 Non-equi Joins 190 Theta Joins 191
Self Joins and Aliases 1 9 1 Correlation Names 192 Joining a Table to a Copy of Itself 192 Pitfalls of Self Joins 194 Three Table Joins 197
Joins in SQL-92 Syntax 200 Outer Join in SQL-92 Syntax 2 0 1
Left Outer Join 203 Right Outer Join 204 Full Outer Join 204
Summary 207
Exercises 208
Chapter 8: Grouped Tables and Aggregate Functions 211
Table of Contents
The GROUP BY Clause 2 1 2 The Working Table 212
Common Problems with GROUP BY 216 GROUP BY and NULLS 217
Aggregate Functions in Groups 2 1 8 NULLs in Aggregate Functions 218 The COUNT Functions 219 The SUM Functions 220 The AVG Functions 2 2 2 The Extreme Functions .224
The MAX Function 225 The MIN Function 226
GROUP BY and Aggregate Functions 2 2 7 GROUP BY and HAVING 229
Summary 2 3 1 Exercises 2 3 2
Chapter 9: Using Subqueries 235
Subqueries 2 3 6
Queries with Subquery Predicates 2 3 6 The Nested Scalar Subquery 237 Predicates and Relational Operators 239
The ANY Predicate 239 The ALL Predicate 242
The Correlated Subquery 244 EXISTS <subquery> Predicate 245 Subqueries in the HAVING Clause 248
Using Subquery Predicates in Other Statements 2 4 9 Stand-alone Scalar Subqueries 249 Subqueries in DELETE FROM Statements 250 Subqueries in INSERT Statements 250
Subqueries in UPDATE Statements 2 5 1
Summary 2 5 2
Exercises 2 5 2
Chapter 10: Views 255
Introducing VIEWs 2 5 6 Handling VIEWS 256 The CREATE VIEW Syntax 257
Materialized VIEWs 257 In-line VIEWs 259
Updatable and Read-Only VIEWs 2 6 1 View Column List 263
WITH CHECK OPTION Clause 263 Dropping Views 265 Hints on Using VIEWs 266
The CREATE SCHEMA Statement 266 Summary 266
Chapter 11 : Embedded SQL 269
Where Are We Now? 270 Embedded SQL 270
Problems of using SQL in a Procedural Host Language 271 How Embedded SQL Operates 271
SQLDA 272 SQLCA 273
Declarations 273 CURSORS 275
The DECLARE CURSOR Statement 275 The OPEN CURSOR Statement 277 The FETCH Statement 277 The CLOSE CURSOR Statement 278 The Single Row SELECT Statement 278 Update and Delete by Position on Updatable Cursors 279 The Positioned DELETE Statement 279 The Positioned UPDATE Statement 280
An Example of C-Embedded SQL 2 8 1 Summary 286
Chapter 12: Optimization and Performance Tips 289
The SQL Database Engine 290 Database Schemas 290
The Optimizer 292 Queries 292 The lnformation_schema Tables 292
Database Access Methods 293 Indexing a Table 293
Keys and Indexes 294 Index Defining 294 An Example of Indexing 294 Clustered Indexes 295 Other Index Options 295 Binary Tree Indexing 296 Indexing Techniques 297
Hashing 297
Hints on How to Write Queries 299 Understanding the Question 299
Table of Contents
Fill in the Select List First 300 Put Table Names in the From Clause 3 0 1 Joining Tables Together 302 Group the Table 303 Design the Having Clause 303
Back up and Decompose the Problem into Simpler Parts 304 Put the Simpler Parts Together 304
Repeat the Process Iteratively and Recursively 306 Test and Repair your Query 307
B a g of Tr icks 3 0 9
Summary 3 1 0
Where Do We Go From Here? 313
Appendix A: Conversion Notes for Different Vendor SQLs 315
Oracle 316 Chapter 2 : Table Creation 316 Chapter 2 : General Implementation Notes 317 Chapter 3 : General Implementation Notes 317 Chapter 4 : Code Details 317 Chapter 4 : General Implementation Notes 319 Chapter 6 : General Implementation Notes 319 Chapter 7 : General Implementation Notes 319 Chapter 8 : General Implementation Notes 320 Chapter 9 : General Implementation Notes 320 Chapter 10 : General Implementation Notes 321
Sybase 321 Initialization 321 Chapter 2 : Code Details 322 Chapter 2 : General Implementation Notes 323 Chapter 3 : General Implementation Notes 324 Chapter 4 : Code Details 325 Chapter 4 : General Implementation Notes 326 Chapter 5 : General Implementation Notes 327 Chapter 6 : General Implementation Notes 327 Chapter 7 : General Implementation Notes 328 Chapter 9 : General Implementation Notes 328 Chapter 10 : General Implementation Notes 329 Chapter 12 : General Implementation Notes 329
Access 330 Chapter 2 : Code Details 331 Chapter 2 : General Implementation Notes 331 Chapter 3 : General Implementation Notes 332 Chapter 4 : Code Details 332 Chapter 4 : General Implementation Notes 333 Chapter 6 : General Implementation Notes 333
xviii
Chapter 7 : General Implementation Notes 334 Chapter 8 : General Implementation Notes 335 Chapter 9 : General Implementation Notes 335 Chapter 10 : General Implementation Notes 336 Chapter 11 : General Implementation Notes 337
Appendix B: Example Databases 339
How to Create the Example Database 339 Inserting the Data 340
Chapter 6 Examples 355 The Foobar Table 355
Creating the Table 355 Inserting the Data 356
The Foo Table 356 The Bar Table 356
Appendix C: Security and Control in Your Database 359
Privileges and Security 359 The GRANT Statement in Watcom SQL 360
Users 360 Granting Privileges 3 6 1
Grantors and Grantees 361 The PUBLIC Grantee 362
Schema Objects 362 Privileges 362
Actions 363 Example Usage of the GRANT Statement 364 The REVOKE Statement 365 Example Usage of the REVOKE Statement 366 Summary 366
Appendix D: Standards and Quasi-standards
Groups Involved with SQL 369
Appendix E: Table of Legal Conversions 373
Index 377