1、數(shù)據(jù)操作語(yǔ)言(DML:select,delete,insert,update)
<1>查詢數(shù)據(jù)庫(kù)目錄:
db2 list db directory
<2>查詢數(shù)據(jù)庫(kù)中表
db2 list tables 當(dāng)前用戶
db2 list tables for all 所有表
db2 list tables for schema schemaname 指定模式的表
<3>顯示表結(jié)構(gòu)
db2 describe table tablename
<4>插入數(shù)據(jù)
db2 insert into tablename(字段名,字段名...) values (與字段名一一對(duì)應(yīng)的值)
db2 insert into tablename1(字段1,字段2,字段3...)
select 字段1,字段2,字段3...from tablename2 + 查詢條件
<5>更改表或視圖數(shù)據(jù)
db2 update tablename/viewname set 字段名1='',字段2='',...+查詢條件
<6>刪除數(shù)據(jù)
db2 delete from tablename where + 條件
<7>導(dǎo)入數(shù)據(jù)
db2 "import from E:\name.txt of del insert into tableName"
db2 "import from E:\name.ixf of ixf commitcount 5000 insert /create/replace into tableName"
db2 "load client from D:\xx.txt of del insert/replace into tabName"(不需要寫日志,但插入前表必須存在;不能create table)
db2 "load client from D:\xx.txt of del restart/terminate into tabName" 當(dāng)導(dǎo)入數(shù)據(jù)出現(xiàn)問(wèn)題被強(qiáng)行中斷時(shí),此表會(huì)被加鎖,通過(guò)此命令可以解鎖
<8>導(dǎo)出數(shù)據(jù)
db2 "export to E:\name.txt of del select * from tableName"
db2 "export to E:\name.txt of del MODIFIED BY NOCHARDEL select * from tableName"(導(dǎo)出不帶分號(hào)的數(shù)據(jù))
導(dǎo)出表結(jié)構(gòu)和數(shù)據(jù)
db2 "export to E:\name.ixf of ixf MODIFIED BY NOCHARDEL select * from tableName"
db2 "export to E:\name.ixf of ixf MODIFIED BY NOCHARDEL select * from tableName fetch first (取數(shù)+UNM) rows only"(取固定條數(shù))
導(dǎo)出表結(jié)構(gòu)
db2look -d dbName -e -t tableName -o D:\xxx.sql(path) -i userName -w password
db2look -d dbName -z tabSchema -e -c -i userName -w password -o + 路徑名
導(dǎo)出存儲(chǔ)過(guò)程結(jié)構(gòu)
db2 "export to xxx.sql of del select text from syscat.procedures where procname='大寫存儲(chǔ)過(guò)程名'"
<9>查詢表狀態(tài)
db2 load query table + tableName
<10>查詢當(dāng)前表數(shù)據(jù)量(數(shù)據(jù)入庫(kù)時(shí))
db2 select count(1) from tab with ur
<11>修改當(dāng)前表名、模式名
db2 rename table tab1 to tab2
2、數(shù)據(jù)定義語(yǔ)言(DDL:create,alter)
<1>創(chuàng)建或刪除實(shí)例
db2icrt instance_name/db2idrop -f instance_name
linux:db2icrt -u user_id instance_name
<2>創(chuàng)建視圖、表、模式
db2 create view/table/schema
創(chuàng)建指定用戶的模式
db2 create schema schName AUTHORIZATION userName
db2 create schema AUTHORIZATION userName(沒(méi)有指定模式名時(shí),模式名隱含為用戶名userName)
定義含有缺省值的表
db2 create table tableName(column1 數(shù)據(jù)類型,column2 數(shù)據(jù)類型 default '缺省值')
基于已存在的表
db2 create table clone_tablename like tablename
db2 create table clone_tablename as (select * from tablename) definition only
創(chuàng)建物化查詢表(MQT)
create table new_table_name as (select * from table_name) data initially deferred refresh deferred;
refresh table new_table_name;
注意:物化表類似一個(gè)查詢,沒(méi)有真正形成表,類型顯示為Query。但它完全可以當(dāng)表來(lái)用。
創(chuàng)建表并指定其索引表空間
db2 create table(.....) in userspace1 INDEX in userspace2
(userspace1是表所在空間,userspace2是表上索引所在空間)
<3>創(chuàng)建視圖
db2 create view viewname
as select 字段名1,字段名2...from table where + 條件
with check option 規(guī)定一種約束:通過(guò)視圖插入或更新的每一行都必須符合視圖的定義,如:
create view emp_view2(empno,empname,deptno) as (select id,name,dept from employee where dept=10)with check option
當(dāng)此視圖用于更新數(shù)據(jù)或插入新值時(shí),with check option 限制了dept列的輸入值
<4>修改表(列,主鍵,唯一約束,檢查約束)
1)添加新列 alter table tablename ADD COLUMN columnname 數(shù)據(jù)類型
2)添加約束
3)修改表中字段 alter table tablename alter columnname set data type 數(shù)據(jù)類型
4) 添加主鍵 alter table tablename add primary key(c1,c2)
<5>刪除模式、表、視圖
drop schema schName <CASCADE|RESTRICT>
CASCADE(級(jí)聯(lián))表示刪除模式的同時(shí)刪除該模式中所有的數(shù)據(jù)庫(kù)對(duì)象
RESTRICT(限制)表示該模式下定義了數(shù)據(jù)庫(kù)對(duì)象時(shí),限制刪除;沒(méi)有任何數(shù)據(jù)庫(kù)對(duì)象時(shí)才能刪除
<6>重新組織表及其索引
重組表數(shù)據(jù) reorg table tableName index indexName(根據(jù)索引)
重組表索引 reorg indexes all for table tableName
<7>重新收集表及其索引統(tǒng)計(jì)信息
runstats on table tableName for indexes all(跑批前重新收集所用表信息可以提高效率)
<8>DB2自動(dòng)增長(zhǎng)主鍵方法
IDENTITY列
generated always as identity(start with 1,increment by 1)將一個(gè)字段指定為自增長(zhǎng)型字段,放在數(shù)據(jù)類型后。
SEQUENCE對(duì)象(序列)
3、數(shù)據(jù)控制語(yǔ)言(DCL:grant,revoke)
將表的特權(quán)授予用戶
grant select,update,delete on table tableName to user userName with grant option
將包特權(quán)授予同組
grant control on package packageName on group groupName with grant option
聯(lián)系客服