DB2数据库基础知识总结(6)

来源:百度文库 编辑:神马文学网 时间:2024/03/29 13:51:24
获取系统日期或系统时间
select current time into curtime from (values 1) as tmp;
select current date into curdate from (values 2) as tmp;
select year(current date) into curdate from (values 2) as tmp; --获取系统年份
select month(current date) into curdate from (values 2) as tmp; --获取系统月份
select day(current date) into curdate from (values 2) as tmp; --获取系统日份
(CURRENT TIMESTAMP 精度达到微秒)
如何预防空值
DB2的COALESCE函数返回()中表达式列表中第一个不为空的表达式,可以带多个表达式, 和oracle的isnull类似。语法格式如下:
CLEASCE(colname,default_value)
多表的关联更新方法
db2的update语法不支持“update table1 set t1.col1=t2.value1 from table1 t1,table2 t2 where …”的写法,但是可以通过如下方法解决:
update table1 t1 set t1.col1=(select t2.col1 from table2 t2 where …)
例:
update test t1 set (t1.username,t1.instcode) = (select t2.instcode,t2.instname from sysinsttb t2 where t2.instcode=t1.instcode);
获取操作(insert、update)的记录数
GET DIAGNOSTICS rcount=ROW_COUNT;
注:
get diagnostics rcount =ROW_COUNT;
只对update,insert,delete起作用.
不对select into 有效
如何执行RUNSTATS等优化命令
db2 runstats on table with distribution and detailed indexes all
db2 reorgchk update statistics on table all
查看什么时候进行了runstats
db2 "select name, stats_time from sysibm.systables"
完整执行如下:
db2 connect to ocrm1 user db2iocrm using db2iocrm
db2 runstats on table db2iocrm.eosoperator with distribution and indexes all
db2 reorg table db2iocrm.eosoperator allow read access
db2 reorg indexes all for table db2iocrm.eosoperator allow read access
db2 connect reset
如何获取结果集的前N行数据
Select * from tablename fetch first N rows only
安装DB2默认值?
在WINDOWS或OS/2中默认实例的是DB2
在LINUX或UNIX环境下默认实例的是DB2INST1
在WINDOWS或OS/2中默认帐户的是DB2ADMIN
在LINUX或UNIX环境下默认帐户的是DB2AS