=======DB2基础指令=======

1、打开数据库
db2 connect to 数据库名;
2、查看数据库中有哪些表
db2 list tables ;
3、查看数据库中的表结构
db2 describe table 表名;
db2 describe table 表名 show detail ;(详细的结构)
4、查看表的索引
db2 describe indexes for table表名 (显示所有索引的标识,但是不会显示字段)
db2 describe indexes for table 表名 show detail (显示各种索引的字段名)
5、查询语句 select
(1)基本查询语句:
db2 "select * from table_name" 显示所有的字段
db2 "select * from table_name where 字段名 = ' ' "
db2 "select 列名1,列名from 表名" 显示指定的字段
db2 "select distinct 列名 ,列名……from 表名 不显示重复行
6、describe的用法
db2 ? describe 查看帮助文档
7、函数in的用法
db2 "select * from 表名 where 列名 =值1 or 列名=值2"
|| 等价于
db2 "select * from 表名 where 列名 in (值1,值2)"
错误 :select * from 表名 where in(列名1, 列名2)
8、统计(单函数只能统计一列)
(1)简单的
select count(列名1)from 表名 where 列名1 is (not) null
(2)对多表进行查询且统计
db2 "select b.bank_id b.sup_bank_id,
(select count ( sys_mer_num ) from m_mer ),
(select count ( term_id ) from m_term )
from m_bank b ,m_mer m .m_term t
Where b.bank_id =m.bank_id and b.bank_id = t.bankid
group by b.bank_id b.sup_bank_id "
9、Order by
select 列名1,列名2 from 表名 order by 列名2 asc /desc (降序)
select 列名1,列名2 from 表名 order by 2 (按第2个字段排序)
10、group by 使用原则
select 后面所有的列 除了没有聚合函数的 必须在添在group by 后面
错误: select 列名1,列名2 from 表名1,表名2 group by 列名1
正确: select 列名1,sum(列名2) from 表名1,表名2 group by 列名1
正确: select 列名1,列名2 from 表名1,表名2 group by 列名1 , 列名2
11、select where group order综合使用语法:
Select distinct 列名
from 表名
where 行表达式
group by 列名 having
order by 列名asc./desc
12、数据的导入导出
.导出数据
Ixf 是16进制的 可以不加messages msg
db2 "export to myfile of ixf messages msg select * from tb1"
db2 "export to myfile of ixf messages msg select * from l_pclbz "
db2 "export to myfile of del select * from 表名 "

.导入数据
Replace 全部替换掉 insert from 插入到表中
db2" import from myfile of ixf messages msg replace into tb1"
db2 " import from myfile of ixf messages msg replace into l_pclbz"
db2 " import from myfile of del replace into l_pclkz"
db2 " import from myfile of del replace into m_mer"

13、数据库delete 的用法
" delete from 表名 where 条件"
只保留某个值
Delete from 表名 where 列名 != 某值
14、修改数据update
"update 表名 from set 列名 = 值 where 条件"
15、修改表结构
(1)Db2不可以 修改字段的数据类型 只可以改变它的长度,删掉表重建
(2)alter table 表名 ALTER 字段 SET DATA TYPE varchar(13);
alter table 表名alter 字段 set data type int; (待验证貌似不可以)
16、插入数据 insert
db2 "insert into 表名 (字段)values ('值)"; (注意值得类型)
db2 "INSERT INTO l_xthjcs (csbs,csmc,csqz,cssm)
values('5008','助农取款手续费及','100.00,1.00,1.5,1.0','最高,最低,扣率,优惠(0.8~1.0)')";
插入全部字段时
db2 "insert into 表名 values ('值)"; (注意值得类型)
17、删除表drop
drop table 表名
18、创建索引
db2 "create unique index l_jgjytjb_undx1 on l_jgjytjb
( bank_id,trade_kind,pos_type,data_type,recode_date,com_type,com_date,trade_type)";
19、数据库中遇到的错误
SQLCA = 0成功执行
100 没有找到满足条件的行
204 命名对象未在数据库中定义
-180 DATE、TIME、TIMESTAMP值的字符串表示法的语法不对
-181 不是有效的DATE、TIME、TIMESTAMP值
-204没有定义的对象名
-303 因为数据类型不兼容,不能分配数值
解决办法:检查数据跟游标是否对应
-501 在试图获取数据或关闭一个游标前必须打开一个游标
解决办法:检查游标的定义
-803不能插入行,因为这将违反唯一索引的约束
解决办法:检查是否重复
-805 在计划中没有发现DBRM或程序包名
解决办法:重新绑定
-818计划<->载入组件的时间戳不匹配,在执行计划中没有从同一个预编译中建立DBRM,该预编]译是作为组件载入的
解决办法:重新编译
20、计数
db2 "drop sequence SEQ_KLCSB_ID" 删除 sequence
db2 "create sequence SEQ_KLCSB_ID 建立sequence
AS bigint
Start with 1
Incremernt by 1
No MAXVALUE
CYCLE
NO CACHE"
db2 " alter sequence seq_mysyb_pwdkey resdart with 5 " 重新计数sequence 从5开始
21、将这些记录锁死并且不允许我修改的同时别人也修改
select …… from …… WHERE ORDER BY jyrq FOR UPDATE
22、约束
(1)唯一约束: 某个字段不允许出现重复值
(2)主键约束: 某一行不可以重复出现
23、substring 的用法
(1)Substring(字符,从第几个字符开始,取多少个字符)
Substring(’abcdef’,2,3)
结果为: bcd
(2)substring ( 字段from从第几个字符开始for 取多少个字符)
Substring(namef from 2 for 3)

O(∩_∩)O~遇到的Sql语句集合:
(1)db2 "select 类型, sum(case when 状态 = 0 then 1 else 0 end) as one,
sum(case when 状态 = 0 then 1 else 0 end) as two,
sum(case when 状态 = 0 then 1 else 0 end) as three
from tab
group by 类型"
(2)db2 "select distinct 列名 ,列名……from 表名"
(3)db2 "INSERT INTO l_xthjcs (csbs,csmc,csqz,cssm)
values('5008','助农取款手续费','100.00,15,1.0','最高,最低,扣率,优惠(0.8~1.0)')";
(4)db2 "create unique index l_jgjytjb_undx1 on l_jgjytjb
( bank_id,trade_kind,pos_type,data_type,recode_date,com_type,com_date,trade_type)";
(5)db2 "select b.bank_id b.sup_bank_id,
(select count ( sys_mer_num ) from m_mer ),
(select count ( term_id ) from m_term )
from m_bank b , m_mer m . m_term t
Where b.bank_id =m.bank_id and b.bank_id = t.bankid
group by b.bank_id b.sup_bank_id "

posted on 2015-10-20 16:40  山月艾草  阅读(790)  评论(0编辑  收藏  举报