Instant SQL Programming



Similar documents
Oracle Database: SQL and PL/SQL Fundamentals

Oracle SQL. Course Summary. Duration. Objectives

Oracle Database: SQL and PL/SQL Fundamentals NEW

Oracle Database: SQL and PL/SQL Fundamentals

Duration Vendor Audience 5 Days Oracle End Users, Developers, Technical Consultants and Support Staff

Oracle Database 12c: Introduction to SQL Ed 1.1

Programming with SQL

Oracle Database 10g: Introduction to SQL

IT2304: Database Systems 1 (DBS 1)

IT2305 Database Systems I (Compulsory)

Database Programming with PL/SQL: Learning Objectives

Oracle Database 11g SQL

Oracle Database: SQL and PL/SQL Fundamentals NEW

Oracle Database: Introduction to SQL

Oracle Database: Introduction to SQL

MOC 20461C: Querying Microsoft SQL Server. Course Overview

MySQL for Beginners Ed 3

Oracle 10g PL/SQL Training

Oracle Database: Introduction to SQL

COMP 5138 Relational Database Management Systems. Week 5 : Basic SQL. Today s Agenda. Overview. Basic SQL Queries. Joins Queries

DBMS / Business Intelligence, SQL Server

ORACLE 9I / 10G / 11G / PL/SQL COURSE CONTENT

T-SQL STANDARD ELEMENTS

SQL Server. 1. What is RDBMS?

Chapter 5 More SQL: Complex Queries, Triggers, Views, and Schema Modification


SQL Server for developers. murach's TRAINING & REFERENCE. Bryan Syverson. Mike Murach & Associates, Inc. Joel Murach

ICAB4136B Use structured query language to create database structures and manipulate data

Discovering SQL. Wiley Publishing, Inc. A HANDS-ON GUIDE FOR BEGINNERS. Alex Kriegel WILEY

Demystified CONTENTS Acknowledgments xvii Introduction xix CHAPTER 1 Database Fundamentals CHAPTER 2 Exploring Relational Database Components

How To Create A Table In Sql (Ahem)

D61830GC30. MySQL for Developers. Summary. Introduction. Prerequisites. At Course completion After completing this course, students will be able to:

Part A: Data Definition Language (DDL) Schema and Catalog CREAT TABLE. Referential Triggered Actions. CSC 742 Database Management Systems

BCA. Database Management System

Guide to SQL Programming: SQL:1999 and Oracle Rdb V7.1

SQL Databases Course. by Applied Technology Research Center. This course provides training for MySQL, Oracle, SQL Server and PostgreSQL databases.

A Comparison of Database Query Languages: SQL, SPARQL, CQL, DMX

FHE DEFINITIVE GUIDE. ^phihri^^lv JEFFREY GARBUS. Joe Celko. Alvin Chang. PLAMEN ratchev JONES & BARTLETT LEARN IN G. y ti rvrrtuttnrr i t i r

Querying Microsoft SQL Server

Microsoft Access 3: Understanding and Creating Queries

Netezza SQL Class Outline

David Dye. Extract, Transform, Load

Database Administration with MySQL

The Guru's Guide to Transact-SQL

Course ID#: W 35 Hrs. Course Content

Database SQL messages and codes

Querying Microsoft SQL Server 20461C; 5 days

Course 20461C: Querying Microsoft SQL Server Duration: 35 hours

THE BCS PROFESSIONAL EXAMINATION Diploma. October 2004 EXAMINERS REPORT. Database Systems

Beginning C# 5.0. Databases. Vidya Vrat Agarwal. Second Edition

GUJARAT TECHNOLOGICAL UNIVERSITY, AHMEDABAD, GUJARAT. COURSE CURRICULUM COURSE TITLE: DATABASE MANAGEMENT (Code: ) Information Technology

Oracle Database 10g: Program with PL/SQL

Querying Microsoft SQL Server Course M Day(s) 30:00 Hours

Teradata Utilities Class Outline

Handling Missing Values in the SQL Procedure

DATABASE DESIGN & PROGRAMMING WITH SQL COURSE CODE: 5324

MOC QUERYING MICROSOFT SQL SERVER

SQL Server 2008 Core Skills. Gary Young 2011

Introduction to Querying & Reporting with SQL Server

Introduction to Microsoft Jet SQL

Design and Implementation

- Eliminating redundant data - Ensuring data dependencies makes sense. ie:- data is stored logically

Querying Microsoft SQL Server (20461) H8N61S

New SQL Features in Firebird 3

Oracle. Brief Course Content This course can be done in modular form as per the detail below. ORA-1 Oracle Database 10g: SQL 4 Weeks 4000/-

History of SQL. Relational Database Languages. Tuple relational calculus ALPHA (Codd, 1970s) QUEL (based on ALPHA) Datalog (rule-based, like PROLOG)

Introducing Microsoft SQL Server 2012 Getting Started with SQL Server Management Studio

LearnFromGuru Polish your knowledge

Advance DBMS. Structured Query Language (SQL)

Chapter 5. SQL: Queries, Constraints, Triggers

SQL SERVER DEVELOPER Available Features and Tools New Capabilities SQL Services Product Licensing Product Editions Will teach in class room

Course -Oracle 10g SQL (Exam Code IZ0-047) Session number Module Topics 1 Retrieving Data Using the SQL SELECT Statement

KB_SQL SQL Reference Guide Version 4

SQL. by Steven Holzner, Ph.D. ALPHA. A member of Penguin Group (USA) Inc.

Relational Databases

Querying Microsoft SQL Server 2012

Database Design and Database Programming with SQL - 5 Day In Class Event Day 1 Activity Start Time Length

Course 10774A: Querying Microsoft SQL Server 2012

5.1 Database Schema Schema Generation in SQL

Course 10774A: Querying Microsoft SQL Server 2012 Length: 5 Days Published: May 25, 2012 Language(s): English Audience(s): IT Professionals

Business Systems Analysis Certificate Program. Millennium Communications & Training Inc. 2013, All rights reserved

MySQL Command Syntax

2874CD1EssentialSQL.qxd 6/25/01 3:06 PM Page 1 Essential SQL Copyright 2001 SYBEX, Inc., Alameda, CA

Basic Concepts of Database Systems

DBMS Questions. 3.) For which two constraints are indexes created when the constraint is added?

AV-005: Administering and Implementing a Data Warehouse with SQL Server 2014

Consulting. Personal Attention, Expert Assistance

Duration Vendor Audience 5 Days Oracle Developers, Technical Consultants, Database Administrators and System Analysts

More on SQL. Juliana Freire. Some slides adapted from J. Ullman, L. Delcambre, R. Ramakrishnan, G. Lindstrom and Silberschatz, Korth and Sudarshan

Advanced Query for Query Developers

CHAPTER 2 DATABASE MANAGEMENT SYSTEM AND SECURITY

Advanced SQL. Jim Mason. Web solutions for iseries engineer, build, deploy, support, train

SQL - QUICK GUIDE. Allows users to access data in relational database management systems.

Querying Microsoft SQL Server 2012

Oracle Database: Develop PL/SQL Program Units

SQL Query Performance Tuning: Tips and Best Practices

Database Query 1: SQL Basics

SQL SELECT Query: Intermediate

Oracle Database: Program with PL/SQL

Transcription:

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 21 Columns 22 The Spreadsheet Analogy 22 The SQL Sub-languages 23

XII 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 31 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 51 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

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 191 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 201 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 212 The Working Table 212 Common Problems with GROUP BY 216 GROUP BY and NULLS 217 Aggregate Functions in Groups 218 NULLs in Aggregate Functions 218 The COUNT Functions 219 The SUM Functions 220 The AVG Functions 222 The Extreme Functions.224 The MAX Function 225 The MIN Function 226 GROUP BY and Aggregate Functions 227 GROUP BY and HAVING 229 Summary 231 Exercises 232 Chapter 9: Using Subqueries 235 Subqueries 236 Queries with Subquery Predicates 236 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 249 Stand-alone Scalar Subqueries 249 Subqueries in DELETE FROM Statements 250 Subqueries in INSERT Statements 250 Subqueries in UPDATE Statements 251 Summary 252 Exercises 252 Chapter 10: Views 255 Introducing VIEWs 256 Handling VIEWS 256 The CREATE VIEW Syntax 257 Materialized VIEWs 257 In-line VIEWs 259 Updatable and Read-Only VIEWs 261 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 281 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 301 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 Bag of Tricks 309 Summary 310 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 361 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