/**链接数据库**/
获取权限
sudo su db2inst1
列出所有数据库
db2 list db directory
链接到指定数据库
db2 connect to [db2name]
回到根目录
cd ~
打开输入语句
db2
查询模式名
select * from syscat.schemata
查询模式下所有表名
select [tabname] from syscat.tables where tabschema = '[schema]'
/**基本语句**/
插入
#db2 insert into schema.table (a,b,c) values ('a','b','c')
删除
#db2 delete from schema.table where condition = condition
修改
#db2 update schema.table set a = 'a' where condition = condition
查询
#db2 select * from schema.table where condition = condition
修改字段值为当前字段值+'字符串'
#db2 update from schema.table set [column] = [column] || '[str]' where
condition = condition
修改字段值小写字母变为大写
#db2 update from schema.table set [column] = Upper(
[column]
) where
condition = condition
只读查询
#db2 sql + for read only with ur
/**常用操作语句**/
打开命令行窗口
#db2cmd
打开控制中心
# db2cmd db2cc
打开命令编辑器
db2cmd db2ce
启动数据库实例
#db2start
停止数据库实例
#db2stop
如果你不能停止数据库由于激活的连接,在运行db2stop前执行db2 force application all就可以了 /db2stop force
创建数据库
#db2 create database 数据库名 using codeset GBK territory CN COLLATE USING SYSTEM
创建数据库指定位置
#db2 db2 create database ghzj on /home/db2inst1/db2data using codeset GBK territory CN COLLATE USING SYSTEM
连接到数据库
#db2 connect to [dbname] user [username] using [password]
断开数据库连接
#db2 connect reset
列出所有数据库
#db2 list db directory
列出所有激活的数据库
#db2 list active databases
列出所有数据库配置
#db2 get db cfg
删除数据库
#db2 drop database [dbname]
(执行此操作要小心)
如果不能删除,断开所有数据库连接或者重启db2
列出所有用户表
#db2 list tables
出所有系统表
#db2 list tables for system
列出所有表
#db2 list tables for all
列出系统表
#db2 list tables for system
列出用户表
#db2 list tables for user
列出特定用户表
#db2 list tables for schema [user]
创建一个与数据库中某个表(t2)结构相同的新表(t1)
#db2 create table t1 like t2
将一个表t1的数据导入到另一个表t2
#db2 "insert into t1 select * from t2"
显示表结构
#db2 describe table tablename
修改列
#db2 alter table [tablename] alter column [columname] set data type varchar(24)
插入列
#db2 alter table [tablename] add column [columname] varchar(100)
/**序列**/
新建序列
create sequence [schema].[tablename] start with 1 increment by 1 nomaxvalue nocycle;
查看下一个序列值
#db2 values nextval for schema.table_name
重置序列值
#db2 alter sequence schema.table_name restart with [nextval]
导出数据
#db2 export to /home/db2inst1/db2work/process_dw/dt_org.ixf of ixf select * from ghzj.dt_org;
导入数据
#db2 import from "/home/db2inst1/hebei_org.ixf" of ixf insert into ghzj.dt_org
导入数据指定表空间
#db2 import from "/home/db2inst1/db2work/area_2017.ixf" of ixf create into iris.ct_area_2017 IN "TS_DW_TBL" INDEX IN "TS_DW_IDX"
列出数据库配置信息
#db2 db2 get db cfg for [db2name]
列出所有表空间
select substr(tbsp_name,1,20) as TABLESPACE_NAME,substr(tbsp_content_type,1,10) as TABLESPACE_TYPE,sum(tbsp_total_size_kb)/1024 as TOTAL_MB,sum(tbsp_used_size_kb)/1024 as USED_MB,sum(tbsp_free_size_kb)/1024 as FREE_MB,tbsp_page_size AS PAGE_SIZE from SYSIBMADM.TBSP_UTILIZATION group by tbsp_name,tbsp_content_type,tbsp_page_size order by 1
设置表空间
CREATE BUFFERPOOL BP_CT_IDX IMMEDIATE SIZE 1000 AUTOMATIC PAGESIZE 4K;
CREATE BUFFERPOOL BP_CT_TBL IMMEDIATE SIZE 1000 AUTOMATIC PAGESIZE 4K;
CREATE BUFFERPOOL BP_DT_IDX IMMEDIATE SIZE 1000 AUTOMATIC PAGESIZE 4K;
CREATE BUFFERPOOL BP_DT_TBL IMMEDIATE SIZE 1000 AUTOMATIC PAGESIZE 4K;
CREATE BUFFERPOOL BP_USER_IDX IMMEDIATE SIZE 1000 AUTOMATIC PAGESIZE 4K ;
CREATE BUFFERPOOL BP_USER_TBL IMMEDIATE SIZE 1000 AUTOMATIC PAGESIZE 4K ;
CREATE BUFFERPOOL BP_DATA_IDX IMMEDIATE SIZE 1000 AUTOMATIC PAGESIZE 4K;
CREATE BUFFERPOOL BP_DATA_TBL IMMEDIATE SIZE 1000 AUTOMATIC PAGESIZE 4K;
CREATE BUFFERPOOL BP_LOG_IDX IMMEDIATE SIZE 1000 AUTOMATIC PAGESIZE 4K;
CREATE BUFFERPOOL BP_LOG_TBL IMMEDIATE SIZE 1000 PAGESIZE 4K;
CREATE BUFFERPOOL BP_DW_IDX IMMEDIATE SIZE 1000 AUTOMATIC PAGESIZE 4K;
CREATE BUFFERPOOL BP_DW_TBL IMMEDIATE SIZE 1000 PAGESIZE 4K;
CREATE BUFFERPOOL BP_TEMP IMMEDIATE SIZE 1000 AUTOMATIC PAGESIZE 4K;
CREATE REGULAR TABLESPACE TS_CT_IDX PAGESIZE 4K MANAGED BY AUTOMATIC STORAGE EXTENTSIZE 8 PREFETCHSIZE AUTOMATIC BUFFERPOOL BP_CT_IDX;
CREATE REGULAR TABLESPACE TS_CT_TBL PAGESIZE 4K MANAGED BY AUTOMATIC STORAGE EXTENTSIZE 8 PREFETCHSIZE AUTOMATIC BUFFERPOOL BP_CT_TBL;
CREATE REGULAR TABLESPACE TS_DT_IDX PAGESIZE 4K MANAGED BY AUTOMATIC STORAGE EXTENTSIZE 8 PREFETCHSIZE AUTOMATIC BUFFERPOOL BP_DT_IDX;
CREATE REGULAR TABLESPACE TS_DT_TBL PAGESIZE 4K MANAGED BY AUTOMATIC STORAGE EXTENTSIZE 16 PREFETCHSIZE AUTOMATIC BUFFERPOOL BP_DT_TBL;
CREATE REGULAR TABLESPACE TS_USER_IDX PAGESIZE 4K MANAGED BY AUTOMATIC STORAGE EXTENTSIZE 8 PREFETCHSIZE AUTOMATIC BUFFERPOOL BP_USER_IDX;
CREATE REGULAR TABLESPACE TS_USER_TBL PAGESIZE 4K MANAGED BY AUTOMATIC STORAGE EXTENTSIZE 16 PREFETCHSIZE AUTOMATIC BUFFERPOOL BP_USER_TBL;
CREATE REGULAR TABLESPACE TS_DATA_IDX PAGESIZE 4K MANAGED BY AUTOMATIC STORAGE EXTENTSIZE 16 PREFETCHSIZE AUTOMATIC BUFFERPOOL BP_DATA_IDX;
CREATE LARGE TABLESPACE TS_DATA_TBL PAGESIZE 4K MANAGED BY AUTOMATIC STORAGE EXTENTSIZE 32 PREFETCHSIZE AUTOMATIC BUFFERPOOL BP_DATA_TBL;
CREATE REGULAR TABLESPACE TS_LOG_IDX PAGESIZE 4K MANAGED BY AUTOMATIC STORAGE EXTENTSIZE 16 PREFETCHSIZE AUTOMATIC BUFFERPOOL BP_LOG_IDX;
CREATE LARGE TABLESPACE TS_LOG_TBL PAGESIZE 4K MANAGED BY AUTOMATIC STORAGE EXTENTSIZE 32 PREFETCHSIZE AUTOMATIC BUFFERPOOL BP_LOG_TBL;
CREATE REGULAR TABLESPACE TS_DW_IDX PAGESIZE 4K MANAGED BY AUTOMATIC STORAGE EXTENTSIZE 16 PREFETCHSIZE AUTOMATIC BUFFERPOOL BP_DW_IDX;
CREATE LARGE TABLESPACE TS_DW_TBL PAGESIZE 4K MANAGED BY AUTOMATIC STORAGE EXTENTSIZE 64 PREFETCHSIZE AUTOMATIC BUFFERPOOL BP_DW_TBL;
CREATE USER TEMPORARY TABLESPACE TS_TEMPUSER PAGESIZE 4K MANAGED BY AUTOMATIC STORAGE EXTENTSIZE 16 PREFETCHSIZE AUTOMATIC BUFFERPOOL BP_TEMP;
CREATE SYSTEM TEMPORARY TABLESPACE TS_TEMPSYS PAGESIZE 4K MANAGED BY AUTOMATIC STORAGE EXTENTSIZE 16 PREFETCHSIZE AUTOMATIC BUFFERPOOL BP_TEMP;
CREATE BUFFERPOOL BP_8K IMMEDIATE SIZE 1000 AUTOMATIC PAGESIZE 8K;
CREATE LARGE TABLESPACE TS_DATA_8K PAGESIZE 8K MANAGED BY AUTOMATIC STORAGE EXTENTSIZE 32 PREFETCHSIZE AUTOMATIC BUFFERPOOL BP_8K;
CREATE SYSTEM TEMPORARY TABLESPACE TS_TEMPSYS_8K PAGESIZE 8K MANAGED BY AUTOMATIC STORAGE EXTENTSIZE 16 PREFETCHSIZE AUTOMATIC BUFFERPOOL BP_8K;
CREATE BUFFERPOOL BP_16K IMMEDIATE SIZE 1000 AUTOMATIC PAGESIZE 16K;
CREATE LARGE TABLESPACE TS_DATA_16K PAGESIZE 16K MANAGED BY AUTOMATIC STORAGE EXTENTSIZE 32 PREFETCHSIZE AUTOMATIC BUFFERPOOL BP_16K;