DB2 Session01
DB2 Session01
DB2 – Session 1
DB2 2
Introduction to Databases
What is Data ?
‘A representation of facts or instruction in a form
suitable for communication’ - IBM Dictionary
What is a Database ?
‘Is a repository for stored data’ - C.J.Date
DB2 4
• Hierarchical Model
• Network Model
• Relational Model
• Object-Oriented Model
DB2 7
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
DB2 8
NETWORK
• Does not distinguish between parent and child. Any
record type can be associated with any number of
arbitrary record types
• Enhanced to overcome limitations of other models
but in reality, there is minimal difference due to
frequent enhancements
• Example - IDMS
DB2 9
RELATIONAL
• Data stored in the form of tables consists of
multiple rows and columns.
• Examples - DB2, Oracle, Sybase, Ingres etc.
RELATIONAL DB CONCEPTS
DB2 11
Relational Properties
• Why Relational ? - Relation is a mathematical
term for a table - Hence Relational database ‘is
perceived’ by the users as a set of tables.
• All data values are atomic.
• Entries in columns are from the same domain
• Sequence of rows (T-B) is insignificant
• Each row is unique
• Sequence of columns (L-R) is insignificant
DB2 12
Relational Concepts (Terminology)
• Relation : A table or File
• Tuple : Row contains an entry for each attribute
• Attributes : Columns or the characteristics that
define the entity
• Domain:. A range of values (or Pool)
• Entity : Some object about which we wish to store
information
• Null : Represents an unknown/empty value
• Atomic Value: Smallest unit of data; the individual
data value
DB2 13
Types of Integrity
Table ‘CUSTOMER’ -
Attributes - CUST_ID, CUST_NAME,
CUST_ADDRESS, CUST_ZIPCODE,
CUST_CCARD_NO...
Table ‘ORDER’ -
Attributes – ORDER_NO, CUST_ID, ORDER_DATE,
ORDER_AMT, ORDER_STATUS...
An introduction to SQL
SQL or Structured Query Language is
• A Powerful language that performs the functions of
data manipulation(DML), data definition(DDL) and
data control or data authorization(DAL/DCL).
• A Non procedural language - the capability to act
on a set of data and the lack of need to know how to
retrieve it. An SQL can perform the functions of
more than a procedure.
• The De Facto Standard query language for RDBMS
• Very flexible
DB2 27
DB2 Objects
Stogroup
Database
Tablespaces
Simple Tablespace
Segmented Tablespaces
Partitioned Tablespaces
• Primarily used for Very large tables
• Only one table in a partitioned TS; 1 to 64
partitions/TS
• Numpart parameter specifies the no. of partitions
• It is partitioned in accordance with value ranges for
single or a combination of columns. Hence these
column(s) cannot be updated
DB2 38
Data types
Data Type
Floating
Integer Numeric
Point
Fixed Variable
Length Length
Small Large Single Double
DB2 39
• Char(n)
• Maximum of 254 bytes
• PIC X(n)
• Varchar(n)
• Maximum of 4046 bytes.
• A structure containing
• PIC S9(4) COMP for length and
• PIC X(n) for the data
• Use only for descriptive fields.
• Can be indexed from DB2 version 7 onwards.
• Provide sufficient free space.
• Should ideally be the last field in the table.
DB2 41
• Integer
• 4 bytes (5 if nullable)
• PIC S9(10) COMP
• Ranges from -2147483648 to +2147483647.
• Small int
• 2 bytes (3 if nullable)
• PIC S9(5) COMP
• Ranges from -32768 to 32767
• Numeric(p,s) – means s digits after decimal & total p digits
• Max of 16 bytes
• PIC S9(p-s)V9(s)COMP-3
DB2 42
• Time
• 3 bytes (4 if nullable)
• PIC X(8)
• Date
• 4 bytes (5 if nullable)
• PIC X(10)
• Timestamp
• 10 bytes (11 if nullable)
• PIC X(26)
DB2 43
NULL value
• DB2 puts the default value of the data type in that field while
inserting a record. If no default value is specified, then DB2
uses the following:
• Character fields - spaces.
• Numeric fields - zeros.
• Date fields - current date.
• Time fields - current time.
CUSTOMER(CUST_ID)
ON DELETE CASCADE
• Default is RESTRICT
• SET NULL is allowed only if column accepts NULLs
in the foreign key table
• Inserting (or updating ) rows in the main table is
allowed only if there are no corresponding rows in the
foreign key table
DB2 48
Examples…
CREATE TABLE TRGA25.DEGREES
START_YEAR NUMERIC(4,0),
PRIMARY KEY(DEGREE_NAME)) IN
DLFDB.DLFTS;
CREATE UNIQUE INDEX TRGA25.DEGREES_PIDX
ON TRGA25.DEGREES(DEGREE_NAME);
…Examples…
• It is imperative to create unique index on the primary
key at the time of table creation itself
• DEGREES is the table name and the columns in it are
DEGREE_NAME and START_YEAR
• Primary key must be defined with NOT NULL, as
columns are NULLable by default
• In create index, the table name and column name
should be referred to as table name(column name)
DB2 50
…Examples…
CREATE TABLE TRGA25.STUDENTS
NAME CHAR(20),
DEGREE_NAME CHAR(4),
PRIMARY KEY(REG_NO),
TRGA25.DEGREES(DEGREE_NAME)
DB2 51
…Examples…
• It is also possible to restrict a column to have only a
particular set of values. For this, use CHECK. That is:
CREATE TABLE TRGA25.STUDENTS
NAME CHAR(20),
DEGREE_NAME CHAR(4)
CHECK(DEGREE_NAME IN (‘ECE’, ‘EEE’)),
PRIMARY KEY(REG_NO),
…Examples…
• For a column that has an automatically incremented
sequence number, use the following:
CREATE TABLE TRGA25.STUDENTS
NAME CHAR(20),
DEGREE_NAME CHAR(4)
PRIMARY KEY(REG_NO),
DB2 53
…Examples
• Creation of table and index is similar to previous
example
• Extra is definition of foreign key – the related table
should have been created before, with the appropriate
primary key and index
• Better to define foreign key along with the table
creation, else database will go into CHECKPENDING
• Good practice to give constraint name for foreign key
so that dropping it will be easier. That is:
FOREIGN KEY D_NAME(DEGREE_NAME)
REFERENCES …
DB2 54
TRGA25.DEGREES(DEGREE_NAME)