1. Open the command line window
# Db2cmd
2. Open the Control Center
# Db2cmd db2cc
3. Open the command editor.
Db2cmd db2ce
===== Operation database commands ====
4. Start a database instance
# Db2start
5. Stop a database instance
# Db2stop
If you cannot stop the database because of the activated connection, you can run DB2 force application all before running db2stop force.
6. Create a database
# DB2 create DB [dbname]
7. Connect to the database
# DB2 connect to [dbname] user [username] using [Password]
8. Disconnect the database
# DB2 connect Reset
9. list all databases
# DB2 list dB directory
10. list all activated Databases
# DB2 list active databases
11. list all database configurations
# DB2 get dB cfg
12. delete a database
# DB2 drop database [dbname]
(Be careful when performing this operation)
If it cannot be deleted, disconnect all databases or restart DB2
========= operation data table command ==============< br> 13. list all user tables
# DB2 list tables
14. list all system tables
# DB2 list tables for System
15. list all tables
# DB2 list tables for all
16 list System tables
# DB2 list tables for System
17. List user tables
# DB2 list tables for user
18. list specific user tables
# DB2 list tables for schema [user]
19. Create a table in the database (T2) new table with the same structure (T1)
# DB2 create table T1 like t2
20. import data from one table T1 to another table t 2
# DB2 "insert into T1 select * From t2"
21. query the table
# DB2 "select * from table name where... "
22. display table structure
# DB2 describe table tablename
23. Modify columns
# DB2 alter table [tablename] alter column [columname] Set Data type varchar (24)
===== Script File Operation Command ==========< br> 24. Run the script file
# DB2-tvf scripts. SQL
25. Help Command
* view Command help
# DB2? Db2start
* view error code information
# DB2? 22001
* memo: Use "DB2? .
====================================< br> 26. Back up the database
# DB2 backup dB
note: disconnect the database before executing the preceding command
27. Online Database Backup
# DB2-V "backup database <Database Name> online to <path> with 2 buffers buffer 1024 include logs without prompting"
28. Restore the database
# DB2 restore dB <source dB Name>
29. Online database recovery
# DB2 "Restore dB <Database Name> to <dB path> logtarget <logpath> without prompting"
# DB2 "rollforward dB <Database Name> to end of logs and stop "...
30. Export data files
# Db2move <dB Name> Export
[-Sn <mode name, usually db2admin>]
[-Tn <table names, separated by commas>]
31. import data files
# Db2move <dB Name> Import
32. obtain information about the DB2 database management configuration Environment
# DB2 get dBm cfg
33. obtain information about the management and configuration environment of a database in DB2
# DB2 get dB CFG for <dB Name>
Or: Execute DB2 get dB CFG after connecting to a database
34. Change the size of DB2 log Space
Note: The following command is used only for DB2 on the developer's own machine to prevent the DB2 database from using hard disk space excessively. If it is a server, the parameter needs to be modified.
# DB2 update dB CFG for <dB Name> using logretain off logprimary 3 logsecond 2 logfilsiz 25600;
If the page size is 4 kb, the preceding command creates 3 MB log files, occupying MB of hard disk space. 25600 * 4kb = 102400kb.
35. Create a temporary tablespace
# DB2 create user temporary tablespace stmaspace pagesize 32 K managed by database using (File 'd: \ db2_tab \ stmaspace. f1' 10000)
Extentsize 256
36. Obtain snapshot data of the Database Manager
# DB2-V get snapshot for dBm
37. Enter the itinerary number
# DB2 list applications show detail
========================================================== ==============
1. load data:
1. load with the default separator. The default value is ",".
DB2 "import from btpoper.txt of del insert into btpoper"
2. load with the specified separator "|"
DB2 "import from btpoper.txt of del modified by coldel | insert into btpoper"
Ii. Uninstall data:
1. unmount all data in a table
DB2 "export to btpoper.txt of del select * From btpoper"
DB2 "export to btpoper.txt of del modified by coldel | select * From btpoper"
2. unmount data from a table with conditions
DB2 "export to btpoper.txt of del select * From btpoper where brhid = '20140901 '"
DB2 "export to cmcode.txt of del select * From cmcodewhere codtp = '01 '"
DB2 "export to cmkcode.txt of del modified by coldel | select * From cmkcode where codtp = '01 '"
Iii. query data structures and data:
DB2 "select * From btpoper"
DB2 "select * From btpoper where brhid = '000000' and oprid = '000000 '"
DB2 "select oprid, oprnm, brhid, passwd from btpoper"
4. Delete table data:
DB2 "delete from btpoper"
DB2 "delete from btpoper where brhid = '000000' or brhid = '000000 '"
5. Modify Table data:
DB2 "Update svmmst set prtlines = 0 where brhid = '000000' and jobtp = '02 '"
DB2 "Update svmmst set prtlines = 0 where jobtp = '02 'or jobtp = '03 '"
6. Database Connection
DB2 connect to btpdbs
7. Clear database connections
DB2 connect reset disconnects the database
DB2 terminate disconnect Database
DB2 Force applications all disconnect all databases
8. Back up databases
1. DB2 backup dB btpdbs
2. db2move btpdbs Export
Db2look-D btpdbs-e-x [-A]-O crttbl. SQL
9. Restore the database
1. DB2 restore dB btpdbs without rolling forward
2. DB2-tvf crtdb. SQL
Crtdb. SQL file content: Create dB btpdbs on/db2catalog
DB2-STVF crttbl. SQL
Db2move btpdbs Import
10. DB2 help commands:
DB2?
DB2? Restroe
DB2? Sqlcode (for example: DB2? Sql0803) Note: The code must be 4-digit, not 4-digit, followed by 0.
11. Bind command: ApplyProgramBind the database. We recommend that you bind the database every time it is restored.
(1) DB2 bind br8200.bnd
(2)/BTP/bin/bndall/BTP/BND
/BTP/bin/bndall/BTP/TRAN/BND
12. view database parameters:
DB2 get dBm cfg
DB2 get dB CFG for btpdbs
13. modify Database parameters:
DB2 update dB CFG for btpdbs using logbufsz 20
DB2 update dB CFG for btpdbs using logfilsiz 5120
After modification, execute the following command to make it take effect:
DB2 stop
DB2 start
Supplement:
DB2 set schema BTP modify current mode to "BTP"
DB2 list tablespaces show detail
DB2 list tablespace containers for 2 show Detail View tablespace id = 2 use the directory where the container is located
DB2 List Application
DB2 list dB directory list all databases
DB2 list active databases list all active databases
DB2 list tables for all list all tables in the current database
DB2 list tables for schema BTP list tables whose schema is BTP in the current database
DB2 list tablespaces show detail display database space usage
DB2 list packages for all
DB2 "import from tab76.ixf of ixf commitcount 5000 insert into achact"
DB2 "create table achact_t like achact"
DB2 "rename table achact_t to achact"
DB2 "insert into achact_t select * From achact where txndt> = (select lstpgdt from
Acmact where actno = achact. actno )"
DB2 get snapshot for dynaimic SQL on Jining
Delete an instance:
# Cd/usr/LPP/db2_07_01/instance
#./Db2idrop instname
List all DB2 instances:
# Cd/usr/LPP/db2_07_01/bin
#./Db2ilist
Create a catalog for a database
$ DB2 catalog dB btpdbs on/db2catalog
Cancel the compiled destination database btpdbs
$ DB2 uncatalog dB btpdbs
View version
# Db2level
Displays the current database management instance
$ DB2 get instance
Set whether the instance is automatically started when the system starts.
$ Db2iauto-on automatic start
$ Db2iauto-off does not start automatically
Database optimization command:
Reorg, runstats
When the database is used for a period of time, the data space will become larger and larger. Delete some
The data is still stored in the database, occupying data space and affecting system performance. Therefore, regular
Run the reorg and runstats commands to clear Deleted Data and optimize the data structure.
DB2 reorg table name
DB2 runstats on table name with distribution and indexes all
Because there are many tables to be optimized, a sh program runsall is provided in the/BTP/bin directory,
Runsall can be run after the business ends on the current day to optimize the database.
During the development of DB2, database maintenance is another important part throughout the development process. It is necessary to maintain a large information system; leave a simple maintenance manual for emergency purposes. Some of the following maintenance commands are collected to assist our maintenance engineers and project managers.
========================================================== ======================================
38. Change the size of the DB2 log Space
Note: The following command is used only for DB2 on the developer's own machine to prevent the DB2 database from using hard disk space excessively. If it is a server, the parameter needs to be modified.
# DB2 update dB CFG for <dB Name> using logretain off logprimary 3 logsecond 2 logfilsiz 25600;
If the page size is 4 kb, the preceding command creates 3 MB log files, occupying MB of hard disk space. 25600 * 4kb = 102400kb.
39. Create a temporary tablespace
# DB2 create user temporary tablespace stmaspace pagesize 32 K managed by database using (File 'd: \ db2_tab \ stmaspace. F1 '10000) extentsize 256
40. Create a tablespace
Rem creates a buffer pool space of 8 K
# DB2 connect to gather
# DB2 create bufferpool extends abmp immediate size 25000 pagesize 8 K
Rem creates a tablespace: stma
REM must confirm that the path is correct
Rem D: \ db2container \ stma
# DB2 drop tablespace stma
# DB2 create regular tablespace stma pagesize 8 K managed by system using ('d: \ db2container \ stma ') extentsize 8 overhead 10.5 prefetchsize 8 transferrate 0.14 bufferpool limit abmp dropped table recovery off
# DB2 connect Reset
41. Recover temporary data to the roll-forward status
# DB2 rollforward database testdb to end of logs and complete noretrieve
42. Backup tablespace
# backup database yndc tablespace (userspace1) to "D: \ Temp "with 2 buffers buffer 1024 parallelism 1 without prompting
43. Create a DB2 tool database
# DB2 create tools catalog ooools create new database toolsdb
44. incremental/differential backup
incremental: data added between the last full backup and the current backup.
delta: Since the last backup (may be full backup, Incremental backup, or differential backup) the data added between the current backup;
45. update statistics of all tables
# DB2-V connect to db_name
# DB2-V "select tbname, nleaf, nlevels, stats_timefrom sysibm. sysindexes "
# DB2-V reorgchkupdate statistics on table all
# DB2-V" select tbname, nleaf, nlevels, stats_timefrom sysibm. sysindexes "
# DB2-V terminate
46. Run statistics on a table
# DB2-V runstatson table tab_nameand indexes all
47. Check whether runstats are executed on the database.
# DB2 -V "select tbname, nleaf, nlevels, stats_timefrom sysibm. sysindexes "
48. Change the buffer pool size
In the buffer pool, when syscat. when the npages of bufferpools is-1, the buffer pool size is controlled by the Database Configuration Parameter bufferpage.
change the value of npages to-1:
# DB2-V connect to db_name
# DB2-V select * From syscat. bufferpools
# DB2-V alter bufferpoolibmdefabp BP size-1
# DB2-V connect reset
# DB2-V terminate
command for changing the Database Configuration Parameter bufferpages as follows:
# DB2-V update dB cfgfor dbnameusing buffpage bigger_value
# DB2-V terminate
49. view the database monitoring content list
# DB2-V get monitor switches
50. Open a database monitoring content
# DB2-V update monitor switches using bufferpoolon
51. Obtain database snapshots
# DB2-V get Snapshot for all databases> snap. out
# DB2-V get snapshot for dBm> snap. out
# DB2-V get snapshot for all bufferpools> snap. out
# DB2-V terminate
52. Resetting database snapshots
# DB2-V reset monitor all
53. Calculate the buffer pool hit rate
Ideally, the buffer pool hit rate is above 95%. The formula is as follows:
(1-(buffer pool data physical reads + buffer pool index physical reads)
/(Buffer pool data logical reads + pool index logical reads) * 100%
========= Database instance ==================================
54. Create a DB2 instance
# Db2icrt <Instance name>
55. delete a DB2 instance
# Db2idrop <Instance name>
56. Set the current DB2 instance
# Set db2intance = DB2
57. Display DB2 instances
# Db2ilist
58. commands for restoring the offline Incremental backup database
# DB2 Restore database yndc incremental automation from D: \ backup \ autobak \ DB2 taken at 20060314232015
59. Create a sample database
On UNIX platforms, use:
# Sqllib/bin/db2sampl <path>
On Windows, OS/2, run: db2sampl E, E is an optional parameter, specifying the drive to create the database
60. Set the apsaradb to available (the default apsaradb is unavailable)
# DB2 update dBm CFG using federated Yes
61. list all tables in the database
# DB2 list tables
62. data migration method 1
export script example
# DB2 connect to testdb user Test password test
# DB2 "export to aa1.ixf of ixf select * From Table1"
# DB2 "export to aa2.ixf of ixf select * From Table2"
# DB2 connect reset
Import script example
# DB2 connect to testdb user Test password test
# DB2 "load from aa1.ixf of ixf replace into Table1 copy no without prompting"
# DB2 "load from aa2.ixf of ixf replace into Table2 copy no without prompting"
# DB2 connect reset