Db2 IMP
Db2 IMP
Precompiled set of SQL statements and procedural logic stored in the database
Advantages:
• Performance Improvement – Reduces SQL query execution time by precompiling queries.
• Reduced Network Traffic – Only procedure calls are transmitted instead of multiple queries.
Syntax:
CREATE PROCEDURE procedure_name (parameters)
LANGUAGE SQL
SPECIFIC procedure_specific_name
MODIFIES SQL DATA
BEGIN
-- SQL Statements
END
Example:
CREATE PROCEDURE UPDATE_EMP_SALARY (
IN P_EMP_ID INT,
IN P_NEW_SALARY DECIMAL(10,2)
)
LANGUAGE SQL
SPECIFIC UPDATE_EMP_SALARY_SP
MODIFIES SQL DATA
BEGIN
DECLARE V_OLD_SALARY DECIMAL(10,2);
In COBOL program,
EXEC SQL
CALL UPDATE_EMP_SALARY(:EMP-ID, :NEW-SALARY)
END-EXEC.
DB2 Page 1
automatically executes when an INSERT, UPDATE, or DELETE operation occurs on a specified table.
Triggers It is event-driven specialized procedures
11 February 2025 11:38 PM Advantages:
Enforce Business Rules – Ensures data validation without external programs.
Automate Auditing – Maintains historical data for compliance.
DB2 Page 2
Db2 utilites • Which utility is used for bulk data loading? → LOAD
11 February 2025 11:56 PM • How do you optimize DB2 table performance? → REORG & RUNSTATS
• How do you back up a DB2 table? → COPY
• How do you recover data after accidental deletion? → RECOVER
• How do you verify data consistency after loading? → CHECK DATA
. LOAD Utility
Purpose: Used to bulk insert large amounts of data into a DB2 table.
Why is it important?: It is faster than INSERT because it bypasses logging for performance.
Example Scenario: Loading employee records from a file into a DB2 table.
UNLOAD Utility
Purpose: Extracts (exports) data from DB2 tables into a file.
Why is it important?: Useful for backup, migration, or data transfer between environments.
Example Scenario: Extracting all employee records for reporting.
Example Command
UNLOAD TABLE EMPLOYEES
TO 'EMPLOYEE_BACKUP.DAT';
Real-World Use: Used when moving data from production to test environments.
RUNSTATS Utility
Purpose: Collects statistics about table data to help the DB2 optimizer choose the best query
execution plan.
Why is it important?: Ensures efficient indexing and query performance.
Example Scenario: After loading new data, run RUNSTATS so that queries execute faster.
Example Command
RUNSTATS TABLESPACE EMP_TBSP;
Real-World Use: Used in database tuning and performance optimization.
DB2 Page 3
Example Command
CHECK DATA TABLESPACE EMP_TBSP;
Real-World Use: Used after data migrations or bulk loads to validate correctness.
QUIESCE Utility
Purpose: Temporarily pauses database activity to ensure consistent backups.
Why is it important?: Prevents conflicts when taking snapshots of critical data.
Example Scenario: Before copying employee payroll data for backup.
Example Command
QUIESCE TABLESPACE EMP_TBSP;
Real-World Use: Used in highly transactional databases where data consistency is critical.
DB2 Page 4
Concurrency ,Isolation Locking and Deadlocks
12 February 2025 12:10 AM
✅ How does DB2 handle multiple users accessing the same data? → Using Locks & Isolation Levels
✅ What is the safest isolation level? → RR (Repeatable Read)
✅ What is the fastest isolation level? → UR (Uncommitted Read)
✅ How do you avoid dirty reads? → Use CS, RS, or RR
✅ What is the default isolation level in DB2? → CS (Cursor Stability).
✅ How do you prevent lost updates? → Use Exclusive Locks (X)
What is locking in DB2? → Prevents data conflicts when multiple users access the same data.
✅ What are the types of locks in DB2? → Shared (S), Exclusive (X), Update (U), and Intent Locks (IS,
IX, SIX).
✅ What is the difference between row-level and table-level locking?
• Row-level: Locks a single row (better concurrency).
• Table-level: Locks the entire table (better for batch jobs).
✅ What is a deadlock in DB2? → A situation where two transactions wait for each other, causing a
system freeze.
✅ How do you prevent deadlocks? → Use short transactions(COMMIT), access tables in the same
order, and set LOCK TIMEOUT.
Concurrency:
Multiple users accessing the same database at the same time without conflicts.
When multiple users or transactions access the same data, issues may arise:
– Two users upda ng the same record one update gets lost.
– Reading uncommi ed (temporary) changes of another transac on.
– hen a transac on reads the same uery mul ple mes and results are
not same each me.
– A transac on sees new rows when re reading data.
Locking:
Prevents multiple users from modifying the same data at the same time, ensuring data consistency and
avoiding conflicts
Types of Locks in DB2
Lock Type What It Does Who Can Access?
Shared (S) Lock Allows reading but prevents Multiple users can read, but no one can
modifications. update.
Exclusive (X) Lock Prevents both reading and updating Only one user can access.
by others.
Update (U) Lock Prevents deadlocks when modifying Only one user can modify, others can read
data. but not update.
Intent Locks (IS, Signals intent to read or write at Controls locking strategy efficiently.
IX, SIX) table/page level.
DB2 Page 5
Why Are Intent Locks Needed?
Intent locks prevent deadlocks and improve performance by allowing different types of access at the
same time.
Scenario: Multiple Users Accessing the Same Table
• User A is viewing a product (SELECT query).
• User B is updating the stock quantity (UPDATE query).
Without Intent Locks (Problem):
DB2 Page 6
SET CURRENT LOCK TIMEOUT 5;
(Wait 5 seconds, then stop if lock is not available.)
Isolation:
how transactions interact with each other when accessing the same data.It helps prevent issues like
dirty reads, lost updates, and inconsistent data when multiple users are working at the same time.
.
Scenario: A user sees 5 items in stock and tries to buy 3 items while another user buys at the same
time.
DB2 Page 7
Integrity ,Primary key,Foreign Key
12 February 2025 01:23 AM
Data Integrity ensures that the data in a database is accurate, consistent, and reliable.
It prevents duplicate, missing, or incorrect data from being stored.
Primary Key:
A Primary Key is a column (or a set of columns) in a database table that uniquely identifies
each record in that table. It ensures that no two records in the table have the same value for
the primary key column(s). The primary key must always contain unique values, and it cannot
contain NULL values.
Simple Example:
Imagine a table called Students, where each student has a unique Student ID. Here, the
Student ID column would be the Primary Key because it uniquely identifies each student.
In this example:
- Student ID is the Primary Key because it uniquely identifies each student.
Foreign Key:
A Foreign Key is a column (or a set of columns) in one table that links to the Primary Key in
another table. The foreign key establishes a relationship between two tables by referring to
the primary key of another table. The foreign key can contain duplicate values and may allow
NULL values.
Simple Example:
Now, let's say we have another table called Courses, where each course is assigned to a
specific student. The Student ID in the Courses table would be a Foreign Key because it refers
to the Student ID in the Students table (which is the Primary Key in the Students table).
In this example:
- Student ID in the Courses table is the Foreign Key because it references the Primary Key
Student ID in the Students table.
Key Differences:
- Primary Key: Uniquely identifies a record in a table and cannot contain NULL values.
- Foreign Key: Establishes a relationship between two tables by referencing a Primary Key in
another table, and can contain NULL values.
DB2 Page 8
another table, and can contain NULL values.
DB2 Page 9
System catalog tables
System catalog tables are special tables in DB2 that store metadata (information about the
12 February 2025 01:38 AM
database structure).
These tables do not store user data but contain details about tables, columns, indexes, constraints,
stored procedures, and more.
They help DBAs and developers manage and query database objects efficiently.
Important System Catalog Tables in DB2
System Table What It Stores Example Query
SYSIBM.SYSTABL List of all tables in the database. SELECT NAME, TYPE FROM SYSIBM.SYSTABLES;
ES
SYSIBM.SYSCOLU Details of table columns (name, SELECT NAME, COLTYPE FROM
MNS data type, length). SYSIBM.SYSCOLUMNS WHERE TBNAME =
'EMPLOYEES';
SYSIBM.SYSINDE Information about indexes on SELECT NAME, TBNAME FROM
XES tables. SYSIBM.SYSINDEXES;
SYSIBM.SYSCONS Stores constraints (Primary Key, SELECT CONSTNAME, TBNAME FROM
TRAINTS Foreign Key, etc.). SYSIBM.SYSCONSTRAINTS;
SYSIBM.SYSROUT Details of stored procedures and SELECT ROUTINENAME, ROUTINETYPE FROM
INES functions. SYSIBM.SYSROUTINES;
SYSIBM.SYSUSER Information about database SELECT GRANTEE, PRIVILEGE FROM
S users and their privileges. SYSIBM.SYSUSERS;
sql
CopyEdit
SELECT NAME, CREATOR, TYPE
FROM SYSIBM.SYSTABLES
WHERE TYPE = 'T';
Explanation:
• NAME → Table name.
• CREATOR → The owner of the table.
• TYPE = 'T' → Filters only user tables (ignores system tables).
sql
CopyEdit
SELECT NAME, TBNAME, UNIQUERULE
FROM SYSIBM.SYSINDEXES
WHERE TBNAME = 'EMPLOYEES';
Explanation:
• Shows index name, table name, and whether the index is unique or non-unique.
• Helps in query optimization by checking existing indexes.
sql
CopyEdit
SELECT CONSTNAME, TBNAME, TYPE
FROM SYSIBM.SYSCONSTRAINTS
WHERE TBNAME = 'EMPLOYEES';
Explanation:
• Lists primary keys (P), foreign keys (F), and check constraints (C) on the table.
• Helps ensure data integrity is properly enforced.
DB2 Page 10
✅ How to find all indexes on a table? → Query SYSIBM.SYSINDEXES.
DB2 Page 11