Database SQL Programming
Total Page:16
File Type:pdf, Size:1020Kb
IBM i Version 7.2 Database SQL programming IBM Note Before using this information and the product it supports, read the information in “Notices” on page 375. This document may contain references to Licensed Internal Code. Licensed Internal Code is Machine Code and is licensed to you under the terms of the IBM License Agreement for Machine Code. © Copyright International Business Machines Corporation 1998, 2013. US Government Users Restricted Rights – Use, duplication or disclosure restricted by GSA ADP Schedule Contract with IBM Corp. Contents SQL programming..................................................................................................1 What's new for IBM i 7.2..............................................................................................................................1 PDF file for SQL programming..................................................................................................................... 4 Introduction to Db2 for i Structured Query Language................................................................................ 4 SQL concepts.......................................................................................................................................... 4 SQL objects...........................................................................................................................................10 Application program objects................................................................................................................13 Data definition language............................................................................................................................16 Creating a schema................................................................................................................................16 Creating a table.................................................................................................................................... 17 Creating a table using LIKE.................................................................................................................. 20 Creating a table using AS..................................................................................................................... 21 Creating and altering a materialized query table................................................................................ 22 Declaring a global temporary table......................................................................................................23 Creating a table with remote server data............................................................................................ 23 Creating a row change timestamp column..........................................................................................24 Creating and altering an identity column.............................................................................................24 Using ROWID........................................................................................................................................ 25 Creating and using sequences............................................................................................................. 25 Defining field procedures.....................................................................................................................28 Creating descriptive labels using the LABEL ON statement............................................................... 41 Describing an SQL object using COMMENT ON................................................................................... 42 Changing a table definition.................................................................................................................. 43 Creating and using ALIAS names.........................................................................................................47 Creating and using views..................................................................................................................... 47 Creating indexes...................................................................................................................................52 Creating and using global variables..................................................................................................... 52 Replacing existing objects................................................................................................................... 53 Catalogs in database design................................................................................................................ 53 Dropping a database object................................................................................................................. 54 Data manipulation language......................................................................................................................55 Retrieving data using the SELECT statement...................................................................................... 55 Inserting rows using the INSERT statement.....................................................................................104 Changing data in a table using the UPDATE statement.................................................................... 110 Removing rows from a table using the DELETE statement...............................................................115 Removing rows from a table using the TRUNCATE statement.........................................................118 Merging data ......................................................................................................................................119 Using subqueries................................................................................................................................120 Sort sequences and normalization in SQL.............................................................................................. 127 Sort sequence used with ORDER BY and row selection................................................................... 128 Sort sequence and views................................................................................................................... 131 Sort sequence and the CREATE INDEX statement........................................................................... 131 Sort sequence and constraints.......................................................................................................... 132 ICU sort sequence..............................................................................................................................132 Normalization..................................................................................................................................... 133 Data protection........................................................................................................................................ 133 Security for SQL objects.....................................................................................................................133 Data integrity......................................................................................................................................135 Routines................................................................................................................................................... 148 Stored procedures............................................................................................................................. 148 Dynamic compound statement......................................................................................................... 185 iii Using user-defined functions.............................................................................................................186 Triggers...............................................................................................................................................212 Varying length parameter lists for external procedures and functions ........................................... 220 Using the INCLUDE statement...........................................................................................................220 Array support in SQL procedures and functions............................................................................... 225 Debugging an SQL routine..................................................................................................................226 Obfuscating an SQL routine or SQL trigger........................................................................................228 Managing SQL and external routine objects......................................................................................229 Improving performance of procedures and functions...................................................................... 230 Processing special data types.................................................................................................................233 Large objects...................................................................................................................................... 233 User-defined distinct types............................................................................................................... 242 Examples: Using UDTs, UDFs, and LOBs...........................................................................................248 Using DataLinks..................................................................................................................................250