Adsense

Wednesday, April 17, 2013

db2move command with example


db2move : It is used to move the database tables across different plat forms. This utility retrieves list of all user tables in a database from the system catalog and exports these tables in PC/IXF ( Integration Exchange Format ) format. The PC/IXF files can be imported or loaded to another local DB2 database on the same system , or different system with diffent platform.

The syntax of the db2move command is
db2move <dbname > <action > [ <option > <value >]
- dbname is the data base whose tables you want to move
- actions are export, import, or load which is to be performed on the data base.
- Options may be like to limit the operation to certain tables (-tn), table spaces (-ts), table creators (-tc), or schema names (-sn). Specifying a subset of tables, table spaces, or table creators is valid with the export action only. If multiple values are specified, they must be separated by commas; no blanks are allowed between items in the list of values.

eq. db2move employee export
- exports all table
db2move employee export -tn employee,desig
- exports only employee , desig tables

db2move employee import
- imports all the tables listed in the db2move.lst to the database employee.

Some of files which are generated during Export, Import or Load as follows

EXPORT.out , IMPORT.out , LOAD.out - Stores summary of the completed action
db2move.lst - Contains a list of table names, their corresponding PC/IXF file names, and message file names
tabn.ixf - Contains exported data from a user table (where n is 1, 2 , .... (tab1.ixf, tab2.ixf, ....))
tabn.msg - Contains messages about the requested action against a user table , where is 1,2, .....
tabna.nnn - Contains Large Object Date (LOB) identified by n
system.msg - system messages which is created only if the action is export, and a LOB path has been specified

For Step by Step example , Please Visit post How to move db2 database across different platforms / clone database with cross-platform in db2

How to enable Incremental Backup in db2 ? Incremental / Delta Backup Commands and steps


          This tutorial covers how to take incremental / delta backup and also covers advantages over incremental backup. As database sizes are growing larger ( even up to terabyte and petabyte range) , cost of taking full backups every time will be high , in terms of storage for the backup images and time required to take the backups . It is not advisable to back up the entire database every time, when a small percentage of the data changes happen. The solution to the above problem is taking incremental backup , it allows the user to backup only the changes that have been made since the last backup , instead of having to backup the entire table space or entire database . Incremental backup image contains only pages that have been updated since the previous backup was taken also contains all of the initial database meta data (such as database configuration, table space definitions, database history, and so on) similar to full backup images.
DB2 supports two types of incremental backup

1.Incremental :  An incremental backup image contains database data that has changed since the last, successful, full backup operation. This is cumulative backup image, because last incremental backup image will have the contents of the previous incremental backup image plus changes after that.

2.Delta : A delta, or incremental delta, backup image contains database data that has changed since the last successful backup ( it may be full, incremental, or delta) of the table space. This is also known as a differential, or non-cumulative, backup image .

Now let us see how to enable incremental backup and how to take full incremental backup and Delta .
In our example , i have used the database SALES and involved two tables Sales_Master , Sales_TR
To check wheather the incremental backup is enabled or not , give the command db2 get db cfg for salesIf the output contains the below line, then incremental backup is not enabled that means we can not use the incremental backup functionality .
  Track modified pages     (TRACKMOD) = OFF

To enable incremental backup, turn the database configuration parameter TRACKMOD on . Give the following command

db2 connect to sales
db2 update db configuration for sales using TRACKMOD ON
  SQL1363W. One or more of the parameters submitted for immediate modification were not changed dynamically. For these configuration parameters, all applications must disconnect from this database before the changes become effective. 

If the above message is displayed then issue the following commands
db2 force applications all
db2 terminate
         When TRACKMOD is on , db2 database keeps track of table spaces that have been modified. So when the incremental backup command is issued , the tables spaces that have not been modified since last backup , are skipped.
Incremental backups require a one time full backup to make as a reference point for incremental changes.
db2 connect to sales
db2 backup db sales online to d:\db2bkp

Incremental
Now make some changes in tables.
db2 "update sales_master set price=price*0.20 where cat_code =4"
db2 "insert into sales_tr (item_code, category,quantity, Sales_date) values ('1027', '7', 30, '2011-12-06')
db2 "insert into sales_tr (item_code, category,quantity, Sales_date) values ('1014', '4', 20, '2011-12-06')

Now issue the following to take incremental backup
db2 backup db sales online incremental to d:\db2bkp
The size of the incremental backup may be less than the complete backup size because it contains only changes since last full backup. You can check the backup size by issuing the dos or linux command
dir D:\db2bkp\SALES.0\DB2\NODE0000\CATN0000\20111206 or ls -ltr /home/db2inst1/db2bkp

Incremental Delta
Now make some more changes in the tables.
db2 "update sales_master set price=price*0.10 where cat_code =7"
db2 "insert into sales_tr (item_code, category,quantity, Sales_date) values ('1056', '3', 50, '2011-12-07')

To take incremental delta backup , issue
db2 backup db sales online incremental delta to d:\db2bkp
As i already said , delta backup contains all pages changed since the last backup ( delta, an incremental, or a full backup image).

To take only incremental backups at table space level issue
db2 backup db sales tablespace(ts_sales) online incremental delta to d:\db2bkp

Now again make some more changes in the tables and issue
db2 backup db sales tablespace(ts_sales) online incremental to d:\db2bkp
Note the timestamp of above backup. Let us say TS4
The above incremental backup image (TS4) contains all the changes happened since the full backup in the tablespace ts_sales, because it is not a delta backup.

How to Recover records deleted by mistake in db2 using Roll forward to a Point in Time recovery

          The following tutorial explains about how to roll forward a database to a point in time. In one of my earlier postings , i have covered how to restore database from online backup and to roll forward to the end of logs.  Once the database is restored from the online backup, it is required to do a Roll Forward operation to a point in time or end of logs depending upon the situation before the database will be made available. Now let us see, when to use "rollforward to end of logs " and when to use "rollforward to point in time" .

End of logs option is useful when a database is lost, and a recovery is needed through all available logs after the online backup has been made to ensure all transactions have been recovered. For example , last online backup has been taken on 10/25/2011, database is corrupted on 10/27/2011. To recover the database, first restore the last backup taken on 10/25/2011 then to apply the transactions happened after the online backup(i.e. using active logs), we have to roll forward to End of logs

Point in Time option is useful when the following situation occurs. Suppose by mistake, lot of records are deleted from the database by a user. Now how to recover the deleted records?. The answer is rolling forward the logs to a Point in Time before the deletion took place. Now let me explain Point in Time recovery option with example.

Please note the number of rows in the sales_trans table
select count(*) from sales_trans
Suppose the records in the sales_trans are 21000

Now let us take Online backup of a database Sales.
db2 connect to sales
db2 backup db sales online to d:\db2onlinebkp
Backup successful. The timestamp for this backup image is : 20111105224708 , where 20111105224708 is timestamp for the last online backup.

After online backup , some transactions are committed
Again note the number of rows in the sales_trans table by executing the query Select count(*) from sales_trans
Suppose now the records in the sales_trans are 21050
Suppose one user has deleted some of the records by mistakenly by issuing the statement delete from sales_trans where sales_date <=date('01/03/2011') . Now how to recover those records ?
Please note the date & time before issuing the delete statement . This date & time will be required later for point in time recovery. For example : date is 11/05/2011 and time is 23.11.10 .
Note the count by issuing the statement Select count(*) from sales_trans which is now 11000 . Almost 10050 records are deleted.

Steps to recover:

db2 force applications all
db2 terminate
db2 restore db sales from d:\db2onlinebkp taken at 20111105224708 without prompting


           The above command restores the database from the last online backup made. Now we need to roll forward to a point in time (in our example : 2011-11-05-23.11.10 ) before the delete statement was issued .The statement to rollforward to a point in time.

db2 rollforward db sales to 2011-11-05-23.11.10 using local time


In the above output, Roll forward status is DB Working. Please Issue the statement db2 rollforward db sales stop which will make the roll forward status to not pending.

Now you can check the number of rows in the sales_trans which is 21050.

Now the deleted records have been recovered by doing Point in Time recovery. Roll forward has been done upto the time before the delete statement was issued. Delete transactions are recorded after this point in time. So delete statements will not be repeated when we do roll forward. But if you do roll forward to end of logs , the delete statements will also be repeated which will again delete the records.

Db2 Incremental Restore steps and commands


This is one of the important database tutorial that explains how to recover / restore data from the incremental backup . Take full , incremental and incremental delta backups before doing incremental restore. Please go through my earlier post How to enable and take incremental , delta backup ? . The following commands are used to take backup offline (full , incremental , delta)
   
1. db2 backup db employee to d:\db2onlinebkp  .  The timestamp for this backup image is : 20120124001028
2. db2 backup db employee incremental to d:\db2onlinebkp .  The timestamp for this backup image is : 20120124001347
3. db2 backup db employee incremental delta to d:\b2onlinebkp. The timestamp for this backup image is : 20120124003554
4. db2 backup db employee incremental to d:\db2onlinebkp.   The timestamp for this backup image is : 20120124004124 

Now you have the following backups
1. Full Backup : Timestamp 20120124001028
2. Incremental backup : Timestamp 20120124001347
3. Incremental Delta backup : Timestamp 20120124003554
4. Incremental backup : Timestamp 20120124004124
Suppose your database is corrupted after the last backup Timestamp 20120124004124. Now you have to recover your data by using the available backups. You may be confused that what backup images are to be restored in which order. When restoring from incremental backups , you have to apply the right sequence of full , incremental and incremental delta backups . This may be tough in real time environment. For this reason , Db2 provides you two ways to restore incremental backup images.
1. Automatic : When you issue restore command with the option automatic , DB2 uses the database backup history to figure out the right sequence for applying backup images and restores them. The RESTORE command needs to be issued only once . Automatic option is recommended
2. Manual : Decide the right sequence of the backup images that needs to restored and issue restore command once for each image
Before doing Automatic or Manual incremental restore , let us the see command db2ckrst (Incremental Restore Image Sequence Command) . This utility is used to see what backup images will be applied in which order, prior to the automatic / manual restore. It generates a list of restore syntax with timestamps for the backup images that are required for an incremental restore .
Syntax : db2ckrst -d dbname -t -r database or tablespace tbs_name
where Timestamp is the last timestamp to be restored
Example :
For database : db2ckrst -d employee -t 20120124004124 -r database
For tablespace of a database - db2ckrst -d employee -t 20120124004124 -r tablespace emptbs
Suggested restore order of images using timestamp 20120124004124 for database employee.
====================================================================
restore db employee incremental taken at 20120124004124
restore db employee incremental taken at 20120124001028
restore db employee incremental taken at 20120124004124
====================================================================
The output shows that restore command with last icremental backup image needs to be run first to read the control and header information only . Then the database will be restored from the full backup . Lastly , the incremental backup will be read again to apply the data in the image. No need to run restore command for incremental backup with timestamp 20120124001347 . Because the last incremental backup cotains database data that has changed since the last, successful, full backup operation.

Automatic Incremental Restore command
To restore a set of incremental backup images using automatic incremental restore, specify the TAKEN AT timestamp option on the RESTORE DATABASE command. Use the time stamp for the last image that you want to restore. For example , you have the backup image with timestamp 20120124004124 is the final backup (incremental)
db2 restore db employee incremental automatic from d:\db2onlinebkp taken at 20120124004124
when you run the above command , you will get the following message ...
SQL2539W Warning! Restoring to an existing database that is the same as the backup image database. The database files will be deleted.
Do you want to continue ? (y/n) y
DB20000I The RESTORE DATABASE command completed successfully.
For restoring tablespace (emptbs) backup image in online
db2 "restore db employee tablespace(emptbs) online incremental automatic from d:\db2onlinebkp taken at 20120124004124"
db2 "rollforward db employee to end of logs tablespace(emptbs) online"

Manual Incremental Restore Steps with example
1. Decide the right sequence of the backup images that needs to restored .
2. Identify the last final backup image to be restored, and issue incremental restore command with the timestamp of the last backup image. This image is known as the target image of the incremental restore, because it will be the last image to be restored.
3. Restore last full database or table space image to establish a baseline against which each of the subsequent incremental backup images can be applied.
4. Restore each of the incremental backup images, in the order in which they were produced, on top of the baseline image restored .
You can use the utility to decide the sequence order .
db2ckrst -d employee -t 20120124003554 -r database
4. Repeat Step 4 until the target image from Step 2 is read second time. The target image is accessed twice . First time , the control and header information is read from the target image . Second time data of the image is read and applied. The target image of the incremental restore operation must be read two times to ensure that the database is initially configured with the correct history, database configuration, and table space definitions for the database that will be created during the restore operation.
Suppose , backup image with timestamp 20120124003554 is the final backup (Incremental delta). The following commands needs to be run.
   
 restore db employee incremental taken at 20120124003554   - Ist time  (incremental delta)
 restore db employee incremental taken at 20120124001028   - full
 restore db employee incremental taken at 20120124001347   - incremental 
 restore db employee incremental taken at 20120124003554   - IInd time (incremental delta)


Note : For an automatic incremental restore, the RESTORE command is issued only once . DB2 then uses the db history to determine the remaining required backup images and restores them. For a manual incremental restore, the RESTORE command is issued once for each backup image .

How to convert db2 date to timestamp example

DB2 has many functions related to timestamp . The following situation may arise .
   1. Inserting Timestamp value into a Timestamp field using insert query
   2. Updating Timestamp field with Timestamp value using update query
  3. Updating Timestamp field with the existing Date values by converting the date to timestamp values; that is, convert date to timestamp using the db2 timestamp functions.

           As we know , timestamp consists of year, month, day, hour, minute, second, and microsecond. The internal representation of a timestamp is a string of 10 bytes. First 4 bytes represent the date, the next 3 bytes the time, and the last 3 bytes the microseconds.

To return the current timestamp , run db2 values current timestamp, on the db2 command prompt after connecting to a database. The output will be timestamp having current date , time and fractional time element ; eg. 2012-02-17-00.12.05.671002

Now let us see the , some of the timestamp functions which are used to convert date to timestamp , a string representation of the timestamp to timestamp and more.

TIMESTAMP function - returns a timestamp from a timestamp string OR from a date , time values.
Syntax : TIMESTAMP (exp ,[exp] ) :

         a) If you specify only one argument , it should be a timestamp or timestamp string having length of 14 charaters which represents a valid date and time in the form of yyyyxxddhhmmss, where yyyy is the year, xx is the month, dd is the day, hh is the hour, mm is the minute, and ss is the seconds. The result of the is the timestamp represented by the specified string. The microsecond part of the timestamp is zero

          b). If you specify both arguments , the 1st argument must be a date or valid date string and the 2nd second argument should be time or a valid time string .The result is a timestamp which is the combination of date (1st arg) and time (2nd arg). The microsecond part is zero.

        If the database was created using territory=US , so the default date format will be MM/DD/YYYY .
              To see the current format, issue the command, db2 values current date

           To change the default format to ISO (YYYY-MM-DD), run db2 bind @db2ubind.lst datetime ISO blocking all grant public on db2 command prompt, after changing the current directory to c:\program files\IBM\sqllib\bnd on Windows and /home/db2inst1/sqllib/bnd on UNIX.
      For more details about changing date format in db2 , please visit Change db2 date format

         timestamp('20120101230420') returns 2012-01-01 23:04:20.000000
        timestamp('2012-01-01', '23.04.20') returns 2012-01-01 23:04:20.000000

        if dob is 1977-01-02 and tob is 17.03.50 , timestamp(dob, tob) returns 1977-01-02 17:03:50.000000

         To convert current date with time 12.00.00 into timestamp value , TIMESTAMP(char(current date) ||' 12:00:00') returns 2012-02-02 12:00:00.000000

      Run the above functions using values in db2 command prompt . for example values timestamp('2012-01-01', '23.04.20') OR run with with select query . For example select timestamp ('2012-01-01', '23.04.20') from employee , where employee is table name.

Problem A : Suppose Employee table has many fields with many records ; one of the field is dob of date type. Suppose you need to add a field dob_ts of timestamp type.

Now to update timestamp field dob_ts with date of birth with time 09.30.45. Run update employee set dob_ts= TIMESTAMP(dob, '09.30.45')

To insert current timestamp to a timestamp field (dob_ts) , run
insert into employee (empcode,empname, dob_ts) values ('5546','Kumar' , current timestamp)

To insert current timestamp to a timestamp field with microseconds , run
insert into employee (empcode , empname, salary, dob_ts) values ('5871', 'xyz', 344223, TIMESTAMP(char(current date) ||' 12:00:00.760000'))

To update timestamp field with date field and time with fractional time element (microseconds) , run
update employee set dob1=TIMESTAMP(char(current date) ||' 12:30:10.760700')

TIMESTAMP_FORMAT function / TO_DATE / TO_TIMESTAMP :- returns a timestamp from a character string
        Syntax : TIMESTAMP_FORMAT ( string-expression , format-string )

            String expression - returns or contains the components of a timestamp that correspond to the format specified by format-string . The return type is CHAR or VARCHAR with length not greater than 254
         Format-string - contains a timestamp format (template) of how string-expression is interpreted and then converted to a timestamp value . Format-string length is not greater than 254 bytes

Allowed string-format elements :
YYYY or YYY or YY or Y - Year (0000-9999 or 000-999 , 00-99 or 0-9 ) .
Use RR or RRRR to adjust the year based on current year. The following conditions are used
Let A be last two digits of the current year , B be two digit year in string-expression, C be 1st two digit of the year component of timestamp . Now
If A is 0-50 and B is 0-49 then C is first two digits of the current year
If A is 51-99 and B is 0-49 then C is first two digits of the current year + 1
If A is 0-50 and B is 50-99 then C is first two digits of the current year - 1
If A is 51-99 and B is 50-99 then C is irst two digits of the current year

Suppose you give, 99 which means 1999, but if the current year is 2012 means, it is adjusted to 2099.
MM - Month (01-12).
DD - day (01-31).
DDD - day of year (001-366).
HH or HH12 - hour
HH24 - 24 hour format (0-24)
MI - Minutes (0-59)
SS - seconds (0-59)
SSSSS - hours, minutes, and seconds (00000-86400)
NNNNNN or FF[1-n] where n is number of digits ( in earlier version n=6 and in Db2 9.7, n = 12 ) - microseconds (0-999999 ) ->equal to FF6

example format-string is YYYY-MM-DD HH24:MI:SS
Values TIMESTAMP_FORMAT('1999-12-31 23:59:59','YYYY-MM-DD HH24:MI:SS') , returns 1999-12-31 23:59:59.000000

In the problem A , current timestamp is inserted with time element zero. To insert current timestamp with time 23.59.59 , run the following command

insert into employee (empcode , empname, salary, dob,dob1) values ('5602', 'abc', 53223, '2012-01-01',TIMESTAMP_FORMAT(CHAR(current date)||' 23:59:59','YYYY-MM-DD HH24:MI:SS'))
     where CHAR(current date)||' 23:59:59' converts current date to string and concatenated with time.

To update timestamp field with timestamp string (date of birth + time )
update employee set dob_ts = TIMESTAMP_FORMAT(CHAR(dob)||' 06:09:39','YYYY-MM-DD HH24:MI:SS')

TIMESTAMP_ISO Function : Returns a timestamp value based on a date, time, or timestamp
Syntax : TIMESTAMP_ISO ( exp ) , where the exp must be a date , time, or timestamp , or a valid string representation of a date, time or timestamp without time zone. If the argument is a date, then the time element is 00.00.00 and the fractional time element is zero . If the argument is a time, the date part is CURRENT DATE , and fractional time element is zero.

example : db2 values timestamp_iso (current date) - returns 2012-02-16 00:00:00.000000
select timestamp_iso(dob) from employee where empcode='1001' , where dob is date of birth a date field.

To return timestamp of having current date with time 17.30.25 , run the following command
db2 values timestamp_iso ('17.30.25') , returns 2012-02-16 17:30:25.000000 , where 2012-02-16 is current date.

To insert current timestamp with microseconds using TIMESTAMP_ISO Function , run
insert into employee (empcode , empname, salary, dob,dob_ts) values ('5870', 'abc', 32663, '2012-01-01',TIMESTAMP_ISO (char(current date) || '-12.00.00.045000'))

Suppose you want want to convert all date values to timestamp values (i.e. date field to timestamp field) having time element zero.
               update employee set dob_ts=timestamp_iso(dob)

TIMESTAMPDIFF Function : Used to calculate days, months , years , minutes , weeks, hours, seconds between two timestamps.

   Example : values TIMESTAMPDIFF(32,CHAR(TIMESTAMP('2012-02-05-12.07.58.000563')-TIMESTAMP('2012-01-19-11.25.42.473439'))) returns the number of weeks between two timestamps that is 2 . You can go through one of my tutorial about how to find difference between two timestamps for more on timestampdiff

Convert Timestamp to Date in db2

          To convert Timestamp to Date , use Date(exp) function which accepts date , timestamp and string representation of date and timestamp
      
       values DATE (current timestamp) , returns date value from current timestamp
      values date('2001-01-01-17.01.20.929866') , returns 2001-01-01
      select date(dob_ts) from employee , converts timestamp to date.

How to find difference between two timestamps , dates in db2.


The following tutorial explains about how to find the difference of two timestamps and also covers how to calculate the difference of two dates . In many situation , you may need to calculate the difference between two timestamps. For example , your application , may capture the access date and time of a user as timestamp in a user table. User account may be locked when a user makes 3 continues unsuccessful login attempts . The access date and time of the userid is stored. After 72 hours , we may need to unlock the locked accounts using query. In this situation , you have to calculate the difference between current timestamp and timestamp when the user account is locked. Another example , in company , IN_TIME and OUT_TIME may be captured as timestamps. Now you can calculate the duty hours by subtracting the OUT_TIME timestamp with IN_TIME timestamp. And date and timestamp difference calculation may also be required for age calculation , service left for retiredment , etc.

1. Calculate difference between two timestamps :

           Difference between two timestamps can be calculated in the following ways
1. timestamp(exp) - timestamp(exp1) OR timestamp(exp) -exp1 OR exp-timestamp(exp1) , where exp, exp1 are timestamp or valid string representation of timestamp. The result of subtracting one timestamp from another will be timestamp duration which is a decimal(20,6) number that represents the number of years, months, days, hours, minutes, seconds, and microseconds between the two timestamps . The result is in the format of YYYYMMDDHHMMSS.ZZZZZZ

2. date(exp) - date(exp1) where exp, exp1 are timestamp or valid string representation of timestamp. The result will be a decimal duration which is decimal(8,0) number represents the difference between two timestamp values as YYYYMMDD. Both exp1 & exp2 are casted to date
First one is more preferred than second way . IInd one used , when you calculate difference of timestamps , if don't want account the time element of timestamps.
examples :

values timestamp('2012-01-05-12.00.00')-timestamp('2011-02-01-12.00.00') , returns 1104000000.000000 that means 11 months, 4 days
values date('2012-01-01-12.00.00')-date('2011-01-01-12.00.00') , returns 10000 , that means 1 year
values timestamp('2012-01-05-12.00.00')-timestamp_iso('2011-02-01') , returns 1104120000.000000   which is 11 months , 4 days , 12 hours
values current timestamp-timestamp(lastaccess)

To calculate the above results , in days , weeks, months , etc , db2 provides a function calledtimestampdiff()
Syntax : timestampdiff (n, char( timestamp(exp)- timestamp(exp1)))
where n can be 1,2,4,8,16,32,64,128 and 256 . 1 = Fractions of a second , 2 = Seconds , 4 = Minutes , 8 = Hours , 16 = Days , 32 = Weeks , 64 = Months , 128 = Quarters , 256 = Years ,
eg. values timestampdiff(16, char(timestamp('2012-01-05-12.00.00')-timestamp('2011-02-01-12.00.00'))) , returns 334 , which means 11 months and 4 days.
update user set login_failed_attempts=0 where timestampdiff(8,char(current timestamp-timestamp_iso(lastaccess))) >=72
select timestampdiff(8, char(timestamp(OUT_TIME)-timestamp(IN_TIME))) from employee , returns the duty hours
Note : The value returned by the above functions is an approximate value , because it does not account for leap years and assumes only 30 days per month.

1. Calculate difference between two Dates :
difference between two dates can be calculated using
date(exp) - date(exp1) OR date(exp) -exp1 OR exp-date(exp1) , where exp, exp1 are date or valid string representation of date. The result will be a decimal duration which is decimal(8,0) number represents the difference between two date values as YYYYMMDD.
Examples :
values date('2012-02-01') - date('2012-01-01') , returns 100 which is 1 month and zero days
values '2012-01-01' - date('2012-02-01')
values current date-DATE('1976-08-21') , returns 350515 which is 35 years , 5 months and 15 days.
select current date - date(doj) from employee , returns the duration of service in the organization

You can use timestampdiff() to calculate difference between two dates . Convert date to timestamp then calculate the difference using the function
eg. select timestampdiff(256,char(timestamp_iso(current date) -timestamp_iso (dob))) from employee , returns the number of years between current date and birthdate

Db2 SQL Replication Step by step with example

One of the very helpful and important feature in db2 is Replication technique. Replication technique allows you to copy data from one location to another location making the second location data identical to the first location.  Data can be copied either  in the local or  remote machine .
Replication is useful
                       1. To consolidate data from multiple sources in a  distributed environment
                       2.  to support basic load balancing when your server is running many more read queries (SELECT) than write queries (insert / update / delete) .
                       3. Makes a backup of data in the local / remote which helps  for disaster recovery
                       4.  to reduce delay and bring the data closer to the user.

DB2 Universal Database (UDB), supports two types of replication.
SQL Replication can be done using Control center and Scripting language called ansclp .SQL replication capability is included in the base product. From the Control Center you can access the Replication Center, a graphical interface for the setup of replication. There is also a scripting language for replication called ansclp which allows you to create scripts to automate replication setup.

This tutorial overs that how to setup replication using the the Replication Center .   You can access the Replication Center  using Control Center -> Tools -> Replication Center.

What happens when we do replication.
                   Two programs are involved.  Capture Program and Apply Program .Capture program capture the data changes in the source table to the CD Table .    Changes of  data in the  source tables are captured in a CD Tables  . When we insert new rows in the source table , new rows are captured in the CD Tables with flag "I" . When we update  data in the source table ,  the updated rows are captured in the CD Tables with the flag "U" .  SImilarly , When we delete   data in the source table ,  the deleted  rows are captured in the CD Tables with the flag "D"  .   Apply Program , replicate  the the data changes captured   in the CD table to the target tables.

Major Steps for replication :

1. Create  control tables for the Capture program

2. Enable the source database for replication (i.e. to enable logretain on for archival log)

3. Register source tables

4. Create control tables for the Apply program

5. Create a subscription set and  member

6.  Start the operation to capture and  apply

7.  Testing the Replication


1. Create  control tables for the Capture program
To  Create table space and control tables do the following step
            1. Right click on the Create Control Servers  -> Select Create Capture Control Tables -> Select Custom 

             2. Select Capture control server (Source database) to create Capture Control Tables
Give user name and password and select Run then OK.

                               
              Now Capture control tables are created

2. Enable the source database for replication
             Right click on the Source database name (Capture control server ) to enable database for replication .  that means to enable archive logging .


            Press ok button to set the LOGRETAIN value for the database to RECOVERY and initiate offline backup for the db    and take full backup of db


3.  Register a replication source

 a) Before registering source tables , specify the schema and table name and table space to be used  for the CD (Capture Data) tables

Right click on the Capture control server  and Source database name  and select Manage Source Object Profile

   You can specify here
i)  table schema and table name to be used as the defaults for the CD tables
ii) table space & its properties  for the CD tables and naming convention for table spaces
           iii)  schema and name for the CD table indexes & naming convention for the the index name.



b) To register source tables
         1.   Select  Capture control servers - > Source database -> capture schema -> schema name.
         2. Right click on the schema name and select Register tables
         3. Select Retrive All to retrive all  source tables and select the table you want to register.  Now CD (Capture Data) table name and table space details appeared automatically based on the settings in the previous steps (i.e. 3 a)

         4.  Then  Ok which will run the query .

4. Create  control tables for apply  program 

To create control tables for apply  program  , Right click on Apply Control Server -> Create Apply Control Tables -> custom


       Select the target database where the data to be replicated .  Then Ok.

5. Create Subscription Sets 
A subscription set defines a relationship between the source database (ORI_DB  in our example) and a target database (DUP_DB in our example).  A subscription-set member defines a relationship between the  source table (SALES) and one or more target tables (TGSALES).

Here you have to specify , set information ,  source to target mapping , schedule time to replicate data.

Steps to do  :
To create subscription sets , Expand  Apply control server -> Right click on the newly created Apply control server ( target data base) -> Subscription sets
Now you have to fill / select  all the details  like Apply control server , Set Name , Apply Qualifier , Capture control server and target server (Target database)
Check on the activate the subscription set

Now do the source to target mapping  , which maps the source colums to target columns to replicate the data .

Now Set the replication schedule  (Time based / event based)   , Then Ok.

Now let us   Add Members

Right click on the newly created set  , select the member information tab and add the member using retrive all option  .  Use change to do column mapping and etc... Please add the column for index for target table using target table table .   Then Ok.

Now the target table is created in the target server where the data to be replicated .

6. Finally Let us  start the operation to capture and  apply  

To start Capture , Expand the operations on SQL replication under Replication center

Right click on the capture control servers and select add  and give the capture control server already created (target database) ,  userid and password  which adds capture control servers for operations

Right click on the capture control servers  which is added now , then select start capture  , then select capture schema , then Ok.
Now give user name and password  for the database server to access

 Similarly you can stop , resume or suspend capture later.

To start Apply Program
             Similarly Expand the apply control servers under operations , Right click on  the apply control server -> Apply qualifiers , then refresh .

Now right click on the apply qualifiers already created and the select start Apply , give Host  Name or  IP Address  , then Ok.    Now give your target server details by clicking Add New System  ..


 Similarly you can stop apply , if you need ..


7. Now test the replication ....
Insert any record to the source table

Now the new record is captured in the  CD Table  (In our example : CDSALES)  with the flag (IBMSNAP_OPERATION) with following values

'I' - Insert operation tbe done  on the target table.
'U' - Update operation to be done
'D' - Delete Operation to be done

Finally the changes  captured in the CD table is applied  in the target table by the apply program. 



Note : I have created Capture control server and Apply control server on the same system.