0% found this document useful (0 votes)
56 views57 pages

DB2 Day - 01

The document provides an introduction to the relational database management system DB2. It discusses key concepts such as tables, records, fields, primary keys and relationships. It also covers SQL components and functions like DDL, DML, DCL and scalar functions.

Uploaded by

Divya Gurumurthy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
56 views57 pages

DB2 Day - 01

The document provides an introduction to the relational database management system DB2. It discusses key concepts such as tables, records, fields, primary keys and relationships. It also covers SQL components and functions like DDL, DML, DCL and scalar functions.

Uploaded by

Divya Gurumurthy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 57

Introduction to DB2

DB2 is an abbreviation for ‘IBM DATABASE 2”

DB2 is a subsystem of the z/OS Operating System

It is a database relational management system


(DBRM) – RDBMS (Relational Database
Management System)

It is a system that allows any number of z/OS users


to access any number of relational databases by
means of the well-known relational language SQL

SQL stands for Structured Query Language


What is Data and Information

Information is nothing but refined data and is the


backbone of an organization.

Consists of
o Data

o Images

o Documents

o Even voice
DB2 databases are Relational
A relational database is a database that is perceived by
its users as a collection of tables

Supplier Supplier Name Status City


S1 John 20 Chennai
S2 Peter 10 Bangalore
S3 Walter 30 Delhi
Supplier Part Qty Part No Part Weight Color
Number Name
S1 P1 100 P1 Nut 17 Red
P2 Bolt 12 Blue
S1 P2 200
P3 Screw 14 Red
S2 P1 300
P4 Cog 12 No color
S3 P2 100
What is Relation?
“ Relation” is just a mathematical term for a table.

Relational systems are based on what is


called the relational model of data

The principles of the relational model


introduced by Dr. E F Codd – IBM researcher.

All the RDBMS follows CODD's Relational


rules 12 rules
Terms we use in DB2

Table , Record , Row , Field , Column and Primary


Key

We use the terms ‘Record’ and ‘Row’


interchangeably

and the terms ‘Field’ and ‘Column’ likewise


Formal Relational Term Informal Equivalents

Relation Table

Tuple Record, Row

Attribute Field, Column

Primary Key Unique Identified


Apart from any other database?

 Apart from Relational Data Base, We have

 Hierarchical Data Base and Network Database


HIERARCHICAL

 Top down structure resembling an upside-down tree


 Parent child relationship

 First logical database model

 Available on most of the Mainframe computers

 Example – IMS
NETWORK

 Does not distinguish between parent and child. Any


record type can be associated with any number of
arbitrary record types
 Example - IDMS
 Primary Key
Key which uniquely identifies each row in the table

 Candidate key
Set of attributes which uniquely identify the row

 Alternate key
Remaining candidate key that were not chosen as primary key

 Foreign key
An attribute of one table can be the primary key of another table
STRUCTURED QUERY LANGUAGE

Introduction
Characteristics and Components
Types
SQL Introduction

 Consists of set of facilities for defining, accessing and


managing relational databases.
 SQL can be invoked either interactively, or by
embedding them in application programs.
 Very flexible
Advantages of SQL

 Provides greater degree of abstraction than procedural


languages.
 Enables end-users to deal with multiple number of
databases at the same time.
 Specifies what is required and not how it should be
done.
Purpose and use of SQL

 Query
 Definition of data structures
 Controlling access to the data
 Delete/Modify occurrences of the data .
DB2 - Data Types
DB2 – DATA TYPES
• Char (n) 1 < n < 254 bytes

• Varchar(n) variable length

• Smallint 2 bytes +- 32 K

• Integer 4 bytes +- 2 GB

• Decimal(x,y) max 31 digits

• Date 0001-01-01

• Time 00:00:00

• Timestamp 0001-01-01-00.00.00.000000
String Data type
Numeric Data type
Numeric Data type
SCALAR FUNCTIONS

 CHAR- Converts date, time or timestamp to


its character string representation

 DATE - Converts a scalar value to date

 Days- Converts a date or timestamp to a


number of days

 Month, Year, Vargraphic etc.,


SCALAR FUNCTIONS
 SCALAR function example

 SELECT YEAR(HIREDATE) AS HIREYEAR FROM


EMP WHERE DEPT = 'A00';

 Example: YEAR: This query, which uses the YEAR


scalar function, returns the year in which each
employee in a particular department was hired:
DB2 Components

 DDL Data Definition Language

 DML Data Manipulation Language

 DCL Data Control Language

 Retrieval Selects the data from the table


DDL – Data Definition Language
 Create – Create schema objects

 Alter – Alter Schema objects

 Drop – Delete schema objects.

 Rename – Rename the schema object.


DML – Data Manipulation Language

 Insert – Add new rows into the table

 Update – Modify values in the existing rows

 Delete – Removing already existing rows


DCL – Data Control Language

 Grant – Grant privileges and roles

 Revoke – Take away privileges.


Retrieval

 SELECT
DDL

 CREATE TABLE
 CREATE INDEX
 ALTER TABLE
 CREATE VIEW
 DROP TABLE,VIEW AND INDEX
Create Table Syntax

CREATE TABLE <table name>


(column1 (data type) NULL/NOT NULL PRIMARY KEY,
column2 (data type), …. )

IN DATABASE.TABLESPACE;
What is NULL?

 NULL means that the value of that field (column) is


unknown.

 NOT NULL means guarantee that every column (field)


contain a genuine value

 NOT NULL WITH DEFAULT means that field (column)


cannot contain nulls (unknown values)
NOT NULL WITH DEFAULT

DB2 automatically moves the following values

Zero (0) for Numeric Field

Blanks (spaces) for Alphanumeric or Alpha


Interacting DB2 with SPUFI

 Now we will discuss about how to communicate with DB2


with SPUFI

 In ISPF panel choose option 8 for DB2 as follows


How to execute SQL with SPUFI
Step 1

Login to TSO region

Step 2

Select 8.1 option in ISPF main menu

Step 3

Specify the Input dataset and Output Dataset for the SQL Query and
result of the Query

Step 4

Now Give the SQL query for executing

Step 5
Type END;;; for execution of the query
Select option
8 for DB2
Select option 1
for SPUFI
(or)
Type 8.1 option
directly in main
menu
Type th Type the output dataset
e Input name (PS dataset)
dataset
n a me
(PDS D
ata Set
)
Give the settings as per our
requirement then press enter
Give the queries to be
executed

Type END;;; for


execution of
these queries
DB2 Queries DEMO
CREATE TABLE
CREATE TABLE <table name>
(column1 data type NULL/NOT NULL PRIMARY KEY,
column2 data type, …. )
IN DATABASE.TABLESPACE;
Result of CREATE TABLE

Success SQLCODE = 0 means


< 0 means Error (E.g.. -104, -
181,)
> 0 means Warning (E.g.. +
100)
INSERT values into the table

INSERT INTO <Table Name>


Values ( col1_value, col2_Value,…………)

 Use this syntax when you are giving values for all the fields

INSERT INTO <Table Name> (Col3,col5,……………...)


Values ( Col3_value, Col5_Value,…………)

 Use this syntax when you want to give value for specific fields
SQLCODE = 0 successful Insertion
SELECT rows from Table
 SELECT is the statement that retrieves the records from
the table. Complete synta
SELECT.
Optional clause w
discussed late
 SELECT <column_list> FROM <table name>

 {WHERE <search condition> } - optional

 {GROUP BY <grouping cols> } - optional

 {HAVING <group search> } - optional

 {ORDER BY <sort order> } - optional


SELECT rows from Table
SELECT * FROM EMP;
Here * refers to all the columns SQLCODE = +100
means end of table or
no more rows
CREATE Table with Primary Key

Primary key column


have unique value and it
should be NOT NULL which
provides a way to identify
each row in the table
Query Result
INSERT value into the table
If table has primary key
then CREATE table
Insert was not success due
definition is incomplete .
to Lack of Primary Index
We have to CREATE
UNIQUE INDEX
CREATE UNIQUE INDEX
CREATE UNIQUE INDEX <index name>
ON <primary key table name>
Result of CREATE UNIQUE INDEX
After UNIQUE INDEX creation,
INSERT values
INSERT Success
CREATE table with different column attributes

NOT NULL
WITH DEFAULT
means - here data
type is CHAR, so
spaces will be
taken if not coded

NOT NULL
means that if WITH DEFAULT
means that value
value is not NULL means if
required always,
giving it will be value is not given
it can not be
assigned as NULL will be
omitted
‘TRAINING’ assigned
DEPT1 table created successfully

You might also like