db2 material 1

10
DB2 Material – 1 Database: it is way of grouping data together ant it’s not a physical object. DB2 is a relational data base management system for the MVS operating system introduced in 1983. The relational database was invented by E. F. Codd at IBM in 1970. A relational database is a collection of data items organized as a set of tables from which data can be accessed or reassembled in many different ways. In such a database the data and relations between them are organized in tables A relational database system contains one or more objects called tables. The data or information for the database are stored in these tables. Tables are uniquely identified by their names and are comprised of columns and rows. Columns contain the column name, data type, and any other attributes for the column. Rows contain the records or data for the columns. Each table is a set of records. A record in turn is a set of fields and each field is a pair field-name/field-value. All records in a particular table have the same number of fields with the same field-names. Relational data base management system is a system which allows MVS users to access and maintain the Relational Database using SQL. SQL is the language used to define the data and manipulate the data in relational forms. It is the standard language for relational We can use SQL in two interfaces. One is interactive interface which is used in TSO. Second is programming language interface by which we can embed in programming language. Advantages of DB2: Ease of accessing the data using SQL. Concurrency: multiple users can access the data concurrently. Less redundancy: because of concurrent access different applications can share the same data. So duplication of data can be reduced.

Upload: siva-sankar

Post on 20-Nov-2015

1 views

Category:

Documents


0 download

DESCRIPTION

db2

TRANSCRIPT

DB2 Material 1Database: it is way of grouping data together ant its not a physical object.

DB2 is a relational data base management system for the MVS operating system introduced in 1983.The relational database was invented by E. F. Codd at IBM in 1970.

A relational database is a collection of data items organized as a set of tables from which data can be accessed or reassembled in many different ways.

In such a database the data and relations between them are organized in tables

A relational database system contains one or more objects called tables. The data or information for the database are stored in these tables. Tables are uniquely identified by their names and are comprised of columns and rows. Columns contain the column name, data type, and any other attributes for the column. Rows contain the records or data for the columns.Each table is a set of records. A record in turn is a set of fields and each field is a pair field-name/field-value. All records in a particular table have the same number of fields with the same field-names.Relational data base management system is a system which allows MVS users to access and maintain the Relational Database using SQL.

SQL is the language used to define the data and manipulate the data in relational forms.

It is the standard language for relational

We can use SQL in two interfaces. One is interactive interface which is used in TSO. Second is programming language interface by which we can embed in programming language.

Advantages of DB2:Ease of accessing the data using SQL.Concurrency: multiple users can access the data concurrently.Less redundancy: because of concurrent access different applications can share the same data. So duplication of data can be reduced.

Integrity: DB2 maintains the correctness and accuracy of data when different applications are accessing the same data.

Entity integrity: The fields of primary key of the table should not contain any null.

Referential integrity: The values in the field of foreign key should be matched with the values of primary key of parent table

Security: security will be checked each time the user access the data.

DB2 objects:

There are two types of DB2 objects

1. System objects: these are the objects that are controlled by system an will be used by DB2

DB2 catalog, DB2 directory, Active log, Archive log, Boot strap dataset, Buffer pools, Locks

2. Data objects: these objects are created and maintained by user.

Data base, Table space, Table, Index, Index space, Storage groups, view, Alias.Data objects:Storage groups: A DB2 storage group is a set of volumes on direct access storage devices (DASD).All the volumes are of same type.

The volumes hold the data sets in which the tables and indexes are actually stored.

The DB2 catalog table SYSIBM.SYSSTOGROUP contains the information about storage groups.

The DB2 catalog table SYSIBM.SYSVOLUMES contains the information about the DASD volumes of storage groupsData base: it is a logical grouping of tables, table spaces, index, index spaces and views. It is not a physical object.It is a collection of tables that are related logically with there associated index and spaces.Table space: Table space is a physical object which consists of tables.

Table space contains one or more VSAM linear datasets. The maximum size of a table space is 64GB.

Each table space is divided into units called as pages of 4KB.There are three types of table spaces.

1. Simple table spaces

2. Segmented table space

3. Partitioned table space

Simple table space:It contains more than one table. So a single simple table space contains rows from multiple tables. Here more than one table which has related data can be kept together.Segmented table space:The table space is divided into segments where a table space is contiguous set of n pages. Each table is stored in more than 1 segment and no segment should contain more than one table. Sequential access and deletion of bulk data is easy.Partitioned table space:

The table space is divided into partitions and all the partitions contain only one table.Table: table is a collection of columns and rows. It is a logical object.

Index: An index is a set of pointers to the rows of a table. It is used to access the records quickly. Each index is stored in its own index space. It is used by DB2 to improve the performance and to maintain uniqueness.Index space: An index space is a physical object which contains the index. When the index is created the index space will also be created. These are 4KB in size.View: A view is a virtual table deriving its data from base table. These can be derived from more than one base table. We can use the view as a base table.

Data security: specified sensitive columns can be secured so that preventing unauthorized users cannot use it.

We can present additional information which can be derived columns.

It can be used to present the data from two different base tables so that user can access it as it is a single base table.Synonym: it is an alternate name for a table where the user only can use it.When the base table is dropped the synonyms of the associated base table is also dropped.To create synonym SYSADM authority is needed.

Alias: it is an alternate name for a table where all the users can use it. When the base table is dropped the aliases of the associated base table are not dropped. To create synonym SYSADM authority is not needed.

System objects:DB2 catalog: it is a set of tables consists of information about the db2 objects such as tables, index, index space, table space, plan and package. When ever the user creates or alters the objects the information about the object will be stored in DB2 table.Ex: When the table is created the table name is stored in the SYSIBM.SYSTABLE. If there is a plan associated with one table then that information is stored in the SYSIBM.SYSPLAN. If there is an alternate table name for the parent table that info is stored in SYSIBM.SYSSYNONYMS SYSIBM.SYSLINKS -contains information about the links between tables created by referential constraints. SYSIBM.SYSDBAUTH-information about the type of database authority held by a user SYSIBM.SYSINDEXES-whether a column has been defined as an index SYSIBM.SYSVIEWS-When a view is created, system information about the view is storedDB2 directory: it is a set of tables that contain the information about technical aspects that is needed during operation on DB2.

Ex: it contains the internal form of SQL statements It contains the actual plan and package.Active logs: It is a log dataset in a DASD which records the every information of change made to DB2 data. Log records are written for every INSERT, UPDATE, and DELETE SQL statement that is successfully executed and committed. These logs will be used to recover the data in case of any failures.

Archive logs: It is a log dataset which will be filled with the information in active logs when the active log is full.As the active logs are filled, a process called log offloading is invoked by DB2 to move the log information offline to archive log data sets. This process reduces the chance that the active logs will fill up during DB2 processing.BSDS: The BSDS is a VSAM KSDS data set utilized by DB2 to control and administer the DB2 log data sets. It is an integral component of DB2, controlling the log data sets and managing a catalog of logsBuffer pools: These are the virtual, storage areas which are responsible for the data transfer between application program and actual storage area. Buffer manager controls the accessing of data from buffer pools.COMPONENTS OF DB2:

System services component: It controls the connection with other components like CICS and TSO. It is responsible for logging activities which is necessary for recovery purposes.Locking services component: It is used to control the concurrent access of DB2 data by the service of IRLM.IRLM is an integral part of db2 which provides necessary locks to control concurrent access.

Data base services: These are the services provided to support data definition, retrieval and update of data. These are the services to implement SQL functions.1. Run time supervisor: Runtime supervisor is a part of main memory which is responsible for the actual execution of the plan. Whenever there is a request for the execution of any SQL query. It is the runtime supervisor's responsibility to appropriately invoke the corresponding plan/package. It uses the control information in the DB2 catalog to perform the operations on data manager.2. Data manager: It is the component that performs all the operations on physical data of data base. It performs insert, update, select, maintaining index. It analyses the request and data of table and calls

buffer manager to satisfy the request.

3. Buffer manager: It is the component of db2 which is responsible for transferring of data from buffer pools to external storage. When the BM receives the request from DM it checks weather the data is in buffer pool and if it is not

In BP it accesses the data from the table and places it BP to send to DM.

Optimizer: It is the most important part of DB2 which analyses SQL statements and determines the best accessing path for satisfying the statement.DB2 accomplishes this by parsing the SQL statement to determine which tables and columns must be accessed. The DB2 optimizer then queries system information and statistics stored in the DB2 system catalog to determine the best method of accomplishing the tasks necessary to satisfy the SQL request. The steps the optimizer does:

1. Receive and verify the syntax of the SQL statement.

2. Analyze the environment and optimize the method of satisfying the SQL statement.

3. Create machine-readable instructions to execute the optimized SQL.

4. Execute the instructions or store them for future execution.

It chooses the access path by evaluating the following costs: 1. CPU cost

2. I-O cost

3. Statistical information in DB2 catalog

4. Actual SQL statement

Physical data independence: the optimizer selects the access path regardless how the data is stored actually. The criterion of access path does not depend up on physical storage characteristics. This is called as physical data independence.SQL:It is a high level and standard language used to maintain and queering the relational database.It is used to define data structures, insert, update and deletion of data.

The first language that was developed for querying is SEQUEL by Donald Chamberlain at IBM labs.

In 1976 the refined version of SEQUEL was release as SEQUEL-XRM. Later it was changed to structured query language.

In 1981 - SQL/DS

In 1992 SQL/92

Types of SQL statements:Data definition language:

It is used to create, drop, and alter the DB2 objects.CREATE TABLE, CREATE VIEW, CREATE ALIAS, CREATE SYNONYM, CREATE INDEX

ALTER TABLE, DROP TABLE, DROP VIEW, DROP VIEW

Data manipulation language:

It is used to retrieve, select and modify the data.INSERT, DELETE, UPDATE, SELECT

Data control language:

It is used to provide the security to the data.

GRANT, REVOKEData types:SMALL INT: it occupies 2 bytes. Value will be stored in 15 bits and sign is stored in I bitThe range of values is -32768 to +32767

INT: it occupies 4 bytes. The value is stored in 31 bits and sign is stored in 1 bit.

The range of values is -2147483648 to +2147483648

BIGINT: occupies 8 bytes. The value is stored in 63 bits and sign is stored in 1 bit.

DECIMAL (a, b):

It is stored in packed decimal format.

B is the integral par of A which represents value after decimal

NUMERIC (A):

It is stored in packed decimal format.

Where a is the length of the column ranges between 1-10^31 to 10^31-1

DATE: it is 8 unsigned digits stores in packed decimal format which occupies 4 bytes. YYYYMMDD

TIME: it is 6 unsigned packed decimal digits stores in packed decimal format occupying 3 bytes. HHMMSS.

TIMESTAMP: It is combination of date and time which is a 20 unsigned packed decimal format and occupies 10 bytes. Format: YYYYMMDDHHMMSSNNNNNN

CREATE TABLE:CREATE TABLE (COLUMN NAME DATATYPE (LENGTH) DATA CONSTRAINTS

DEFAULT ) IN DATABASE NAME. TABLE SPACE NAME;The maximum length of a table name is 18.

DATA CONSTRAINTS: these are the rules that are given to a table to prevent the invalid data being entered.DB2 checks the data that is being entered in the column against the constraints given for the column.

1. Not null constraint

2. Unique key constraint

3. Primary key constraint

4. Foreign key constraint

5. Check constraintNot null constraint: No column should contain null value. That is the data should be entered in the column while inserting. COLUMN NAME DATATYPE (SIZE) NOT NULL

NOT NULL WITH DEFAULT: When the value is not known default value can be stored.

NOT NULL WITH DEFAULT When value is not specified spaces will be stored for CHAR or VATCHAR data columns and 0s for

NUMERIC or DECIMAL data columns.Check constraint: It is a logical expression that evaluates true or false in order to implement business rule validations. CHECK (ENO BETWEEN 1 AND 1000) CHECK (ENO > 1 AND ENO 0); ALTER TABLE MAINTEC DROP PRIMARY KEY; ALTER TABLE MAINTEC DROP CHECK ES;

ALTER TABLE MAINTEC DROP FOREIGN KEY ED; ALTER TABLE MAINTEC ADD PRIMARY KEY (EN);

ALTER TABLE MAINTEC ADD FOREIGN KEY(ED) REFERENCES MAINDEP (DEP);