Overview of SQL




CS185c

Chris Pollett

Mar 9, 2010

Outline

Introduction

Connection Pooling

An image illustrating how connections are pooled on the app server, within DB2 connect, and on DB2 for z/OS

Quiz

Which of the following statements is false?

  1. In DDF several DB2 threads might be in the same enclave.
  2. The WLM can assign dispatching priorities to stored procedure transactions based on the service class goals.
  3. In the Distributed Unit of Work DRDA level, a two-phase commit protocol is needed.

Structured Query Language (SQL)

Relational Closure

DB2 Supported Data Types (High Level)

image showing the supported datatypes of DB2

Datetime Family

DB2 can convert between different regions formatting of dates and times. There are four supported output formats: ISO YYYY-MM-DD HH.MM.SS (24hr), USA MM/DD/YYYY Hour.Minutes am/pm, EUR DD.MM.YYYY HH.MM.SS (24hr), and JIS YYYY-MM-DD HH:MM:SS. For example,
SELECT EMPNO, CHAR(HIREDATE, USA) FROM EMP;

SQL also supports functions to get components out of dates. For example, DAYS(MY_DATE_VAR).

String Family of Datatypes

image showing the supported string datatypes of DB2

More Info about String Datatypes

Numeric SQL Datatypes

image showing the supported numeric datatypes of DB2

More Numeric Datatypes

Types of SQL Statement

As we've mentioned before SQL statements can be classified into three types:

CREATE TABLE

Defines the structure of a table and its relationship to other tables in the database.

CREATE TABLE table_name ( column_defn1, column_defn2, ... )

column_defn: column_name column_type [DEFAULT expr]
  [column_constraint]

column_constraint: [CONSTRAINT constraint_name]
  {NOT NULL | CHECK (check-condition) | 
   PRIMARY KEY | FOREIGN KEY
   REFERENCES ref_table[(ref_column)] [ON DELETE action]}

action:
   NO ACTION | RESTRICT | CASCADE | SET NULL

Referential Integrity Illustration

CREATE TABLE DEPT ( DEPTNO CHAR(3) NOT NULL PRIMARY KEY, DEPTNAME VARCHAR(32), ... );

CREATE TABLE EMP ( EMPNO CHAR (6),
   DEPTNO CHAR(3),... 
   CONSTRAINT FK FOREIGN KEY (DEPTNO) 
     REFERENCES DEPT ON DELETE SET NULL);

Table Examples

CREATE TABLE BOOK (
   BOOKNO INTEGER NOT NULL PRIMARY KEY,
   TITLE VARCHAR(100),
   AUTHOR VARCHAR(100));

CREATE TABLE MEMBER (
   CARDNO INTEGER NOT NULL PRIMARY KEY,
   NAME VARCHAR(100), 
   CITY VARCHAR(100));

NULL

CREATE INDEX

CREATE [UNIQUE] INDEX ON 
table_name(column_list) [CLUSTER | NOT CLUSTER]

Index Examples

CREATE UNIQUE INDEX BOOKINDEX ON BOOK(BOOKNO)
  CLUSTER; 

CREATE INDEX CHECKOUTINDEX ON CHECKOUT(BOOKNO);
Two indexes one clustered one not

ALTER

The ALTER TABLE statement is used to add a column to an existing table, to increase the length of a VARCHAR column, or to add or drop some other table property such as a constraint.

ALTER TABLE Tablename {
 ADD {column_defn | table_constraint} |
 ALTER column_name SET DATA TYPE data_type |
 DROP {PRIMARY KEY | {FOREIGN KEY | CHECK |
 CONSTRAINT} constraint_name}}

ALTER INDEX Indexname{ {CLUSTER | NOT CLUSTER} |
 {ADD COLUMN (column_name)}}

DROP

The DROP command can be used to delete a table or an index. The syntax looks like:

DROP TABLE table_name
DROP INDEX index_name