db2資料庫常用語句
阿新 • • 發佈:2019-02-07
/**連結資料庫**/獲取許可權 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;