Personal tools
You are here: Home DB2 How To's How to rebuild an entire database
Navigation
Log in


Forgot your password?
 
Document Actions

How to rebuild an entire database

When you cannot recover your database for any reason, you need to rebuild your database

To Rebuild an entire database you need to perform the following steps:



Extract DDL of a database using db2look

  • DB2 V8
db2look -d <dbname> -a -e -m -l -x -f -o <output>
  • DB2 V9
db2look -d <dbname> -a -e -m -l -x -f -xs -o <output>

Exporting all tables of a database

  • DB2 V8
db2 -x "select 'export to ' || rtrim(a.tabschema)||'.'|| rtrim(tabname) || '.ixf of ixf MODIFIED BY lobsinfile modify by identityignore MESSAGES ' || rtrim(tabname) \
 || '.msg select * from ' || rtrim(tabschema)||'.'||rtrim(tabname) ||';'from syscat.tables a where type = 'T' \
and tabschema not like 'SYS%' order by tabschema, tabname"

  • DB2 v9
db2 -x "select 'export to ' || rtrim(a.tabschema)||'.'|| rtrim(tabname) || '.ixf of ixf lobs to <pathlobs> MODIFIED BY lobsinfile MESSAGES ' || rtrim(tabname) \
 || '.msg select * from ' || rtrim(tabschema)||'.'||rtrim(tabname) ||';'from syscat.tables a where type = 'T' \
and tabschema not like 'SYS%' order by tabschema, tabname"



Loading into all tables

  • DB2 v8

    For tables without identity always columns
db2 -x "select 'load from ' || rtrim(a.tabschema)||'.'||rtrim(a.tabname) || '.ixf of ixf lobs from <pathlobs> \
SAVECOUNT 5000 MESSAGES l' || rtrim(a.tabname) || '.msg insert into ' || \
rtrim(a.tabschema)||'.'||rtrim(a.tabname) ||'nonrecoverable;'from syscat.tables a where not exists \
(select * from syscat.columns b where b.generated='A' and a.tabname=b.tabname \
and a.tabschema=b.tabschema) and a.tabschema not like 'SYS%' and a.type='T'"


For tables with identity always columns

db2 -x "select 'load from ' || rtrim(a.tabschema)||'.'||rtrim(a.tabname) || '.ixf of ixf lobs from <pathlobs> \
modified by identityoverride SAVECOUNT 5000 MESSAGES l' \
|| rtrim(a.tabname) || '.msg insert into ' || rtrim(a.tabschema)||'.'||rtrim(a.tabname) ||'nonrecoverable;'\
from syscat.tables a where exists (select * from syscat.columns b where b.generated='A' \
and a.tabname=b.tabname and a.tabschema=b.tabschema) and a.tabschema not like 'SYS%' and a.type='T'"
  • DB2 v9

For tables without identity always columns
db2 -x "select 'load from ' || rtrim(a.tabschema)||'.'||rtrim(a.tabname) || '.ixf of ixf lobs from <pathlobs> \
SAVECOUNT 5000 MESSAGES l' || rtrim(a.tabname) || '.msg insert into ' || \
rtrim(a.tabschema)||'.'||rtrim(a.tabname) ||'nonrecoverable;'from syscat.tables a where not exists \
(select * from syscat.columns b where b.generated='A' and a.tabname=b.tabname \
and a.tabschema=b.tabschema) and a.tabschema not like 'SYS%' and a.type='T'"

For tables with identity always columns
db2 -x "select 'load from ' || rtrim(a.tabschema)||'.'||rtrim(a.tabname) || '.ixf of ixf lobs from <pathlobs> \
modified by identityoverride SAVECOUNT 5000 MESSAGES l' \
|| rtrim(a.tabname) || '.msg insert into ' || rtrim(a.tabschema)||'.'||rtrim(a.tabname) ||'nonrecoverable;'\
from syscat.tables a where exists (select * from syscat.columns b where b.generated='A' \
and a.tabname=b.tabname and a.tabschema=b.tabschema) and a.tabschema not like 'SYS%' and a.type='T'"

Fix check pending

db2 -x "select 'set integrity for ',substr(rtrim(tabschema)||'.'||rtrim(tabname),1,40),'immediate checked;' from \
syscat.tables where status = 'C' order by tabschema, tabname"

Security Awareness
Would you like your company to implement gamification into your security awareness program?





Polls