Title: SQL Jumpstart
1SQLJumpstart
- Mark Holm
- Centerfield Technology
2Goals
- Get you started with SQL
- Show how SQL works with an existing environment
- Lay the groundwork for learning more
2
3Notes
- V4R3 and higher syntax used in examples
- Examples show only a small subset of what can be
done!
3
4Agenda
- Explain why SQL is important
- Compare SQL with traditional approaches
- Data definition statements
- Selecting data
- Changing data
- Using SQL day to day
4
5Why SQL?
- Simplifies cross platform development
- SQL generation can be automated
- Allows quick and flexible access to data
- Operates on sets of data -gt powerful
- Official industry standard used by pervasive APIs
products - ODBC/JDBC
- Most query tools
- Can perform better than traditional methods!
5
6Why SQL?
- Business Intelligence
- Query tools
- Data manipulation (some) data cleanup
- Web sites internet applications
- Net.Data
- JDBC
- ERP applications
- SSA eBPCS
- JDE One World
- Mapics XA
6
7Tools
- Interactive SQL (STRSQL)
- Run SQL statement (RUNSQLSTM)
- Operations Navigator
- Query tools (ODBC)
- And many others..
7
8Basic statements
- Create Collection CRTLIB
- Create Table CRTPF
- Create View CRTLF (no key)
- Create Index CRTLF (key)
- Insert, Update, Delete add, change, and remove
records - Select select records columns to read
8
9Data definition (DDL)
- Data definition statements create objects
- Collections (libraries)
- Tables (physical files)
- Views (non-keyed logical files)
- Indexes (keyed logical files)
9
10Create collection
- Create collection Create library
- Example CREATE COLLECTION HR
- Objects created
- Library named HR
- Journal journal receiver
- Catalogs (e.g. SYSTABLES)
10
11DSPLIB of a newly created COLLECTION
12Create Table
- Create table Create physical file (CRTPF)
- Objects created, etc.
- Physical file
- File automatically journaled to QSQJRN
- Example Create a table to hold employee
information
12
13Create table
CREATE TABLE HR/EMPLOYEE
(FIRST_NAME FOR COLUMN FNAME CHAR (20 )
NOT NULL, LAST_NAME FOR COLUMN LNAME
CHAR (30 ) NOT NULL,
EMPLOYEE_ID FOR COLUMN EMP_ID NUMERIC (6 , 0) NOT
NULL WITH DEFAULT 0, DEPARTMENT FOR COLUMN DEPT
DECIMAL (3 , 0) NOT NULL WITH DEFAULT,
TITLE
CHAR (30 ) NOT NULL
WITH DEFAULT, HIRE_DATE FOR COLUMN HDATE
DATE NOT NULL)
13
14Tables vs. Physicals
- Tables
- Dirty data cant be inserted
- Marked as a table
- Maximum 1 member
- No maximum size
- Default to reuse deleted rows
- Table is automatically journaled (collection)
- Physical files
- Dirty data can be inserted
- Not an SQL object
- No limit on members
- Default is to cap size
- Default to not reuse deleted records
- File is not automatically journaled
14
15Prompt for CREATE TABLE in Interactive SQL
16Create View
- Create View Create logical file (CRTLF)
- Objects created
- Logical file
- No keyed access path
- Example View to format an employees first last
name and identify employees hired less than 2
years prior to today
16
17Create View
CREATE VIEW HR/NEWBIES (EMPLOYEE_NAME,
DEPARTMENT, HIRE_DATE) AS
SELECT concat(concat(strip(last_name),','),st
rip(first_name)),
department, hire_date
FROM HR/EMPLOYEE WHERE (year(current
date)-year(hire_date)) lt 2
17
18DSPFD of a newly created VIEW
19View uses
- Subset columns to users or applications
- Create new columns for users or applications
- Subset the rows returned (hide data)
- Join tables together
19
20Views vs. Logical files
- Logical File
- Is a logical file
- Can be keyed or non-keyed
- Basic data manipulation
- Faster OPEN time
- Logical files can not reference LFs
- Many members
- View
- Looks like logical file
- Never has an access path (index)
- Powerful data manipulation
- Slower OPEN time
- Views can reference views
- One member
20
21Create Index
- Create Index CRTLF (keyed logical)
- Objects created
- Logical file
- Physical access path (index)
- Created to improve performance -- not for
function - Example Create an index over the employee
identifier
21
22Create Index
CREATE UNIQUE INDEX HR/EMPIDIX
ON HR/EMPLOYEE (EMPLOYEE_ID ASC)
22
23Indexes vs. Keyed logical files
- Index
- Looks like logical file
- Powerful data manipulation
- Not usable as file
- One member
- Logical file
- Is a logical file
- Basic data manipulation
- Like a normal file
- Many members
23
24Deleting objects
- Done with a DROP statement
- DROP TABLE
- DROP VIEW
- DROP INDEX
- By default these statements drop dependent
objects unlike CL commands (e.g. you cant delete
a PF via DLTF if there are dependent logical
files)
24
25Data Manipulation (DML)
- Insert - add rows to a table
- Update - update column values in a table's rows
- Delete - delete rows in a table
- Select - retrieve rows from one or more tables
25
26Insert
- Insert add a new row (record)
- Works just like HLL verbs
- Example Add a couple of employees to our table
in the human resources library
26
27Insert
INSERT INTO HR/EMPLOYEE
(FIRST_NAME, LAST_NAME, EMPLOYEE_ID, DEPARTMENT,
TITLE, HIRE_DATE)
VALUES ('Joe', 'Jones', 4793, 522, Hotshot
Programmer', '1998-01-16')
INSERT INTO HR/EMPLOYEE
VALUES ('Jane', Smith', 3290, 712, Psychic
Business Planner', '1999-12-01')
27
28Update
- Update change data in existing row
- Works just like HLL verbs
- Example Give a 20 raise to one our star
employees
UPDATE HR/EMPLOYEE
SET Salary Salary 1.2
WHERE Employee_id 5228
28
29Delete
- Delete Remove one or more rows
- Example Fire the human resources department
DELETE FROM HR/EMPLOYEE
WHERE Department 108
29
30Select
- Select statement is the most complex and powerful
SQL statement - If you know how to write good select statements
you are well on your way to mastering the language
30
31Select
- SELECT statement overview
- Select column(s)
- From table(s) or view(s)
- Where selection criteria
- Group By grouping columns
- Having selection criteria for groups
- Order By sort order for result rows
-
31
32Select - example 1
SELECT FROM HR/EMPLOYEE
- Selects all columns (designated by )
- Selects data from the HR/EMPLOYEE table
- Select all rows
32
33Select - example 2
SELECT LNAME, FNAME FROM HR/EMPLOYEE WHERE
EMPID 556
- Selects only two columns
- Selects data from the HR/EMPLOYEE table
- Select only rows with EMPID EQ 556
33
34Select - example 3
SELECT ShirtName, SKU, PRICE.8
AS DISCPRICE FROM PRODLIB/INVENTORY WHERE
SYLE HAWIAN OR COLOR PINK
- Selects two columns and calculates a third called
DISCPRICE - Selects data from the INVENTORY table
- Selects parts in a defined range
34
35Select - example 4
SELECT TERRITORY, SUM(SaleAmt),
AVG(SaleAmt), COUNT() FROM
PRODLIB/SALES WHERE COUNTRY USA GROUP
BY TERRITORY HAVING AVG(SaleAmt) gt
10000.0 ORDER BY 2 DESC
- Rank sales in largest to smallest order
summarized by territory. Only look at sales in
the United States that average more than 10000.
35
36Useful functions
36
37More useful functions
37
38Embedded SQL
- Combines the power of SQL with HLLs like RPG,
COBOL, or C - All of the benefits of HLLs such as performance,
complex logic, and control over data
manipulation.with SQLs capabilities for set at
a time processing and dynamic selection
38
39Embedded SQL
- The following statement is an example of what an
embedded statement looks like - SELECT fname, lname, address
- FROM employee
- WHERE empid INPUTID
- INPUTID is a host variable
39
40Embedded SQL
- Embedded SQL programs are pre-compiled and then
compiled again - CRTSQLRPGI, CRTSQLCBLI
- Pre-compiler identifies SQL with special tags to
indicate start and end of SQL statement
40
4141
42Embedded SQL
- C/Exec SQL
- C Update Employee
- C Set lname NewLN -- assign new last
name - C Where empId EmployId
- C/End-Exec
- --------------------------------------------------
--- - Exec SQL
- Update Employee
- Set lname NewLN
- Where empId EmployId
- End-Exec
RPG
COBOL
42
43Tips
- To start learning SQL use ISQL or Operations
Navigator - Create a sample collection and tables
- Add, change, and delete data
- Try different select statements and functions to
get a feel for the power of SQL - Read and try examples listed in the book and
internet resources in this presentation - Create tables with short and long names
- Put related SQL objects in the same collection
- If selecting a small amount of data, create an
index over the columns that most uniquely
identify the data - Be careful with UPDATE and DELETE statements
without a WHERE clause - Use the AS clause to give calculations
understandable names
43
44Other resources
- Database Design and Programming for DB2/400 -
book by Paul Conte - SQL for Smarties - book by Joe Celko
- SQL Tutorial - www.as400network.com
- AS/400 DB2 web site at http//www.as400.ibm.com/db
2/db2main.htm - Publications at http//publib.boulder.ibm.com/pubs
/html/as400/ - Our web site at http//www.centerfieldtechnology.c
om
44
45Summary
- SQL is becoming increasingly important for many
reasons - Basic DDL is very similar to DDS
- Power and flexibility come with the DML
statements -- in particular SELECT - There are many resources to help take you to the
next level of understanding
46- I hear and forget.
- I see and remember.
- I do and I understand.
-
- Kung Futse
- 551 B.C.