DB2 fundamentals - Part1

download DB2 fundamentals - Part1

of 26

Transcript of DB2 fundamentals - Part1

  • 7/29/2019 DB2 fundamentals - Part1

    1/26

    1Copyright 2004, Cognizant Academy, All Rights Reserved

    Database & RDBMS concepts: Data,

    Database & database Systems

    What is Data? A representation of facts or instruction in a form suitable for

    communication - IBM Dictionary

    What is a Database?

    Is a repositoryfor stored data - C.J.Date

    What is a database system?

    An integrated and shared repository for stored data or collection of storedoperational data used by application systems of some particular enterprise.

    Or

    Nothing more than a computer-based record keeping system.

    Types of Database System

    Hierarchical

    Network

    Relational

  • 7/29/2019 DB2 fundamentals - Part1

    2/26

    2Copyright 2004, Cognizant Academy, All Rights Reserved

    Can be visualized an upside down tree of

    data

    a single table acts as the "root"

    (Parent)of the database from which othertables "branch(Children) out

    Child may only have one parent but a

    parent can have multiple children

    This child/parent rule assures that data is

    systematically accessible

    Redundancy would occur because

    hierarchical databases handle one-to-

    many relationships well but do not handle

    many-to-many relationships well

    Characteristics of Hierarchical Database

    Hierarchical Database

    Database & RDBMS concepts: Data,

    Database & database Systems

  • 7/29/2019 DB2 fundamentals - Part1

    3/26

    3Copyright 2004, Cognizant Academy, All Rights Reserved

    Characteristics of Network Database

    Child tables were allowed to have more than one parent

    Network model supports many-to-many relationships

    The Network model solves the problem of data redundancy by

    representing relationships in terms of sets rather than hierarchy

    Database & RDBMS concepts: Data,

    Database & database Systems

  • 7/29/2019 DB2 fundamentals - Part1

    4/26

    4Copyright 2004, Cognizant Academy, All Rights Reserved

    Customers

    Sold -To

    Invoices

    C# CName

    C Addr

    I# C# Date

    Amt

    The concept of a table in which all data is

    stored

    The database is represented in terms of

    Entities, Relations and Attributes.

    Database is a collection of tables , constraints

    Characteristics of Relational Database

    Relational Database

    Database & RDBMS concepts: Data,

    Database & database Systems

  • 7/29/2019 DB2 fundamentals - Part1

    5/26

    5Copyright 2004, Cognizant Academy, All Rights Reserved

    Advantages of Database systems over File Management Systemare:

    Data redundancy

    Multiple views

    Shared data

    Data independence (logical/physical)

    Data dictionary

    Search versatility

    Cost effective

    Security & Control

    Recovery restart & Backup

    Concurrency

    Database & RDBMS concepts: Data,

    Database & database Systems

  • 7/29/2019 DB2 fundamentals - Part1

    6/26

    6Copyright 2004, Cognizant Academy, All Rights Reserved

    Database & RDBMS concepts: Relational

    Properties & Terminology

    Relation is a mathematical term for a table - Hence Relationaldatabase 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

    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 value Atomic : Smallest unit of data; the individual data value

  • 7/29/2019 DB2 fundamentals - Part1

    7/267Copyright 2004, Cognizant Academy, All Rights Reserved

    Database & RDBMS concepts:E-R Model

    Entity-Relationship model is alogical representation of data for

    a business area

    Represented as entities,

    relationship between entities and

    attributes of both relationshipsand entities

    E-R models are outputs of

    analysis phase i.e they are

    conceptual data models

    expressed in the form of an E-R

    diagram

  • 7/29/2019 DB2 fundamentals - Part1

    8/268Copyright 2004, Cognizant Academy, All Rights Reserved

    Database & RDBMS concepts:

    Normalization (1NF 5NF)

    Normalization is done to bring the design of database to a standardizedmode

    1NF : All entities must have a unique identifier, or key, that can be composed

    of one or more attributes. All attributes must be atomic and non repeating.

    2NF : Partial functional dependencies removed - all attributes that are not a

    part of the key must depend on the entire key for that entity.

    3NF : Transitive dependencies removed - attributes that are not a part of the

    key must not depend on any non-key attribute.

    4NF : Multi valued dependencies removed

    5NF : Remaining anomalies removed

  • 7/29/2019 DB2 fundamentals - Part1

    9/269Copyright 2004, Cognizant Academy, All Rights Reserved

    Database & RDBMS concepts:

    Types of Integrity

    Entity Integrity: Rule states that no column that is part of a primary keycan have a null value

    Referential Integrity: Rule states that every foreign key in the first tablemust either match a primary key value in the second table or must be

    wholly null

    Domain Integrity: Integrity of information allowed in column

  • 7/29/2019 DB2 fundamentals - Part1

    10/2610Copyright 2004, Cognizant Academy, All Rights Reserved

    DB2 Overview : DB2 Internal structure

    System Services

    CICS

    TSO

    IMS

    Relational Data Systems

    Data Manager

    Buffer Manager

    DB2 Utilities

    DB2 Optimizer

    BufferPools

    ActiveLogs

    IRLMVSAM

    Media Manager

    DB2 Table Data DB2Directory

    DB2Catalog

    DD

    F

  • 7/29/2019 DB2 fundamentals - Part1

    11/2611Copyright 2004, Cognizant Academy, All Rights Reserved

    DB2 objects and Data types:

    STOGROUP

    STOrage GROUP It is a collection of direct access

    volumes, all of the same device

    type

    The option is defined as a part of

    tablespace definition

    When a given space needs to be

    extended, storage is acquired

    from the appropriate stogroup

    STORAGE GROUP

    DATA BASE(S)

    TABLESPACE(S)

    TABLE(S)

    VIEW(S)

    INDEX

  • 7/29/2019 DB2 fundamentals - Part1

    12/2612Copyright 2004, Cognizant Academy, All Rights Reserved

    DB2 objects and Data types : Database

    DATABASE It is a collection of logically related objects - like Tablespaces, Indexspaces,

    Tables etc.

    A STOGROUP & BUFFERPOOL (is buffer area used to maintain recently

    accessed table and index pages) must be defined for each database.

    A database may occupy more than one disk space

    In a given database, all the spaces need not have the same stogroup

  • 7/29/2019 DB2 fundamentals - Part1

    13/2613Copyright 2004, Cognizant Academy, All Rights Reserved

    DB2 objects and Data types:

    Tablespaces

    TABLESPACES Logical address space on secondary storage to hold one or more tables

    A SPACE is basically an extendable collection of pages with each page of

    size 4K or 32K bytes.

    It is the storage unit for recovery and reorganizing purpose

    Three Type of Tablespaces - Simple, Partitioned & Segmented

    Simple Tablespace

    Can contain more than one stored table

    Depending on application, storing more than one Table might enable faster

    retrieval for joins using these tables Usually only one table is preferred. This is because a single page can

    contain rows from all tables defined in the database.

    LOAD with replace option deletes all data

  • 7/29/2019 DB2 fundamentals - Part1

    14/2614Copyright 2004, Cognizant Academy, All Rights Reserved

    DB2 objects and Data types:

    Tablespaces

    Segmented Tablespace Can contain multiple tables.The tables are stored in different segments.

    A Segment consists of a logically contiguous set of n pages.

    Segsize parameter decides the allocation size for the tablespace

    No segment is allowed to contain records for more than one table

    Sequential access to a particular table is more efficient

    Mass Delete is much more efficient than in any other Tablespace

    Reorganizing the tablespace will restore every table to its clustered order

    Lock Table on table, locks only the table, not the entire tablespace

    If a table is dropped, the space for that table can be reclaimed with minimum

    reorg

  • 7/29/2019 DB2 fundamentals - Part1

    15/2615Copyright 2004, Cognizant Academy, All Rights Reserved

    DB2 objects and Data types:

    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

    Individual partitions can be independently recovered and reorganized

    Different partitions can be stored on different storage groups for efficient

    access.

  • 7/29/2019 DB2 fundamentals - Part1

    16/2616Copyright 2004, Cognizant Academy, All Rights Reserved

    DB2 objects and Data types:

    Tables, Views & Index Tables store the data in the form of

    Columns of values attached to a specific datatype with allowable valueconstraints

    A row for each record.

    A View is a subset of columns that are open for a specific purpose. It can be a join of more than one tables.

    Indexes are based on the values of data in one or more columns. An index is anobject that is separate from the data in the table.

    Indexes are used to enhance performance.

    These are discussed in detail further in the course.

    EMPNO FIRSTNME MIDINIT LASTNAME WORKDEPT

    10 CHRISTINE I HAAS A00

    20 MICHAEL L THOMPSON B01

    30 SALLY A KWAN C01

    Columns

    Rows

  • 7/29/2019 DB2 fundamentals - Part1

    17/2617Copyright 2004, Cognizant Academy, All Rights Reserved

    Views

    Views are a logical derivation of a table from other table/tables. A Viewdoes not exist in its own right.

    They provide a certain amount if logical independence

    They allow the same data to be seen by different users in different ways

    In DB2 a view that is to accept a update must be derived from a single

    base table

  • 7/29/2019 DB2 fundamentals - Part1

    18/2618Copyright 2004, Cognizant Academy, All Rights Reserved

    Alias & Synonym

    Alias Mean another name for the table.

    Aliases are used basically for accessing remote tables (in distributed

    data processing), which add a location prefix to their names.

    Using aliases creates a shorter name.

    Synonym

    Also means another name for the table, but is private to the user who

    created it.

  • 7/29/2019 DB2 fundamentals - Part1

    19/26

    19Copyright 2004, Cognizant Academy, All Rights Reserved

    Introduction to SQL : Features

    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

    SQL - Features

    Unlike COBOL or 4GLs, SQL is coded without data-navigational instructions.

    The optimal access paths are determined by the DBMS. This isadvantageous because the database knows better how it has stored data

    than the user.

    What you want and not how to get it

    Set level processing & multiple row processing

  • 7/29/2019 DB2 fundamentals - Part1

    20/26

    20Copyright 2004, Cognizant Academy, All Rights Reserved

    Introduction to SQL: Tools

    Query Management Facility (QMF) It is an MVS- and VM- based query tool

    It allows end users to enter SQL queries to produce a variety of reports and

    graphs as a result of this query

    QMF queries can be formulated in several ways : by direct SQL stmts, by

    means of relational prompted query interface or by query-by-example (QBE).

    QBE is similar to SQL in some ways but more user friendly

    SPUFI

    It supports the online execution of SQL statements from a TSO terminal

    Used for developers to check SQL statements or view table details Spufi menu contains the input file in which the SQL statements are coded,

    option for default settings and editing and the output file.

    Enter option 8.1 on the Cognizant m/f main menu. Provide the input/output

    datasets.

  • 7/29/2019 DB2 fundamentals - Part1

    21/26

    21Copyright 2004, Cognizant Academy, All Rights Reserved

    Introduction to SQL : DDL

    Data Definition Language

    Create Table Syntax:

    CREATE TABLE (

    Col Name Datatype,)

    PRIMARY KEY(Columns)

    FOREIGN KEY (Column)

    REFERENCESdbname.tablename ON DELETE(SET NULL/CASCADE)

    UNIQUE (Col name) (referential

    constraint)1. LIKE Table name / View name

    2. IN Database Tablespace Name

    Foreign Key references dbname.tableon relation condition for delete.

    CREATE

    ALTER

    DROP

    DDL

    INSERT

    SELECT

    UPDATEDELETE

    DML

    GRANT

    REVOKE

    DCL

    SQL

  • 7/29/2019 DB2 fundamentals - Part1

    22/26

    22Copyright 2004, Cognizant Academy, All Rights Reserved

    Introduction to SQL : DDL

    Table1 references table2(target) - Table2s Primary key is the foreignkey defined in Table1

    The Conditions are CASCADE, RESTRICT & SET NULL (referentialconstraint for the foreign key definition)

    Inserting (or updating ) rows in the target is allowed only if there are norows in the referencing table

    Alter Table Format:

    ALTER : ALTER TABLE

    ADD Column Data-type [ not null with default]

    Alter allows primary & Foreign key specifications to be changed

    It does not support changes to width or data type of a column or droppinga column

    Drop Table Format:

    DROP : DROP TABLE

    Similar statements are there for other database objects

  • 7/29/2019 DB2 fundamentals - Part1

    23/26

    23Copyright 2004, Cognizant Academy, All Rights Reserved

    Introduction to SQL : DDL Rules

    Some general rules for RI & Table Parameters Avoid nulls in columns participating in Arithmetic logic or comparisons

    Primary key cols cannot be nulls

    Limit referential structures to no more than three levels in a direction

    Use DB2s inherent features rather than program coded RIs. Do not use RIs on tables built from another RI system

    Consider using Fieldprocs or Editprocs or Validprocs

  • 7/29/2019 DB2 fundamentals - Part1

    24/26

    24Copyright 2004, Cognizant Academy, All Rights Reserved

    Introduction to SQL : DCL

    GRANT & REVOKE

    GRANT : grants the table privileges, plan & package privileges, collectionprivileges, database privileges, use privileges and system privileges

    user with a SYSADM privilege will be responsible for overall control of thesystem

    The table privileges allowed are SELECT, UPDATE, DELETE, INSERT,(both base tables & views), ALTER(Table) & (Create)INDEX(only to basetables)

    There are no specific DROP privilages;the table can be dropped by its owneror a SYSADM

    Format of GRANT :

    GRANT SELECT, UPDATE(NAME,NO)

    ON TABLE EMPLTO A, B, C(or PUBLIC);

    GRANT ALL ON EMPL TO PUBLIC;

    GRANT EXECUTE ON PLAN PLANA TO USER;

    A user having authority to grant privilege to another, also has the authority togrant the privilage with with the GRANT Option

  • 7/29/2019 DB2 fundamentals - Part1

    25/26

    25Copyright 2004, Cognizant Academy, All Rights Reserved

    Introduction to SQL : DCL

    REVOKE : this stmt revokes the privileges given to a user. The usergranting the privileges has the authority to REVOKE also.

    It is not possible to be column specific when revoking an UPDATE

    privilege

    REVOKE SELECT ON TABLE EMPL FROM USERA;

  • 7/29/2019 DB2 fundamentals - Part1

    26/26

    26C i h 2004 C i A d All Ri h R d

    Queries

    ?