DB2命令

来源:互联网 发布:医疗网络咨询培训 编辑:程序博客网 时间:2024/05/21 13:56
//连接到数据库sample
connect to sample 用系统管理员用户登录
connect to sample user db2admin using db2admin

//创建结点
catalog tcpip node NODENAME remote IP-address server  50000
取消结点
uncatalog node NODE_NAME

//查看有哪些数据库
list db directory
哪些节点
list node directory

//查看实例名
get instance
查看当前连接数据库名
values current server或
get connecction state

//查看当前用户
values current user
当前时间
values current date
values current time
values current timestamp
select current timestamp from sysibm.dual

//查看用户权限
get authorizations   此函数已经失效

//db2默认自动commit,可以通过下面命令关闭
update command options using c off
打开
update command options using c on

//查看有多少schema
select * from sysibm.sqlschemas
select * from SYSCAT.SCHEMAAUTH

//创建表空间
create tablespace ts03
创建表
create table tb03(id int not null, name varchar(20)) in ts03
删除表
drop table
创建索引
create index idx03 on tb01(id)

//修改字段为主键
alter table tb03 add primary key(id)
删除主键
alter table tb03 drop primary key

//增加表字段
alter table tb03 add age int
修改表字段
alter table tb03 alter age set data type decimal(5,2)
添加字段非空属性
alter table tb03 alter name set not null
删除字段非空属性
alter table tb03 alter name drop not null
删除字段
alter table tb03 drop age
注意:
1:不允许修改字段的名称(只能先删除,再添加)。
2:不允许减小字段的长度。
3:不允许修改字段类型(如把 Integer 修改成 varchar)。


//reorg table
(新增字段或者修改字段后必须进行reorg 否则该表不可使用)
reorg table tb03


//表的备份
create table tb04 like tb03
insert into tb04 select * from tb03

4.     DB2 的主要数据类型
数值类型
整数: Smallint\int\bgint\
  Smallint : 2 bytes
  Integer: 4 bytes
  Bigint: 8 bytes
  Float:  decimal\double
  Decimal(n,m)  n/2 + 1
     字符串类型
       CHARACTER,VARCHAR
     日期时间型
   Date,timestamp    
     Timestamp: 10 bytes
     Date: 4 bytes
     Time: 4 bytes
    
----DB2分页
  select * from tb01 fetch first N rows only
 
  select * from (select 字段1,字段2,,rownumber() over(order by 排序字段 asc ) as rowid  from 表名 )as a where a.rowid >= startPage AND a.rowid <endPage

Another:
--> db2 catalog tcpip node db2node remote hostname server service_port
    db2 catalog database db_name as alias_name at node db2node.
---------------------
--注册节点
catalog tcpip node CQCRM remote 10.191.113.132 server 50000;
--注册数据库
catalog database CQCCDW at node CQCRM;
--删除注册节点
uncatalog node CQCRM;
--删除注册数据库
uncatalog database CQCCDW;
----------------
如何看查看本地Catalog信息
db2 list db directory 
#Db2



1、 打开命令行窗口
  #db2cmd
2、 打开控制中心
  # db2cmd db2cc
3、 打开命令编辑器
db2cmd db2ce


=====操作数据库命令=====
1、 打开命令行窗口
  db2cmd
2、 打开控制中心
   db2cmd db2cc
3、 打开命令编辑器
db2cmd db2ce

详细出处参考:http://www.jb51.net/article/21172.htm
4、 启动数据库实例
  db2start

5、 停止数据库实例
  #db2stop
   如果你不能停止数据库由于激活的连接,在运行db2stop前执行db2 force application all就可以了 /db2stop force
6、 创建数据库
  #db2 create db [dbname]
7、 连接到数据库
  #db2 connect to [dbname] user [username] using [password]
8、 断开数据库连接
  #db2 connect reset
9、 列出所有数据库
#db2 list db directory

10、 列出所有激活的数据库
  #db2 list active databases
11、 列出所有数据库配置
  #db2 get db cfg
12、 删除数据库
  #db2 drop database [dbname]
(执行此操作要小心)
如果不能删除,断开所有数据库连接或者重启db2

=========操作数据表命令==========
13、 列出所有用户表
  #db2 list tables
14、列出所有系统表
#db2 list tables for system
15、列出所有表
  #db2 list tables for all
16、 列出系统表
  #db2 list tables for system
17、列出用户表
  #db2 list tables for user
18、 列出特定用户表
  #db2 list tables for schema [user]
19、 创建一个与数据库中某个表(t2)结构相同的新表(t1)
  #db2 create table t1 like t2
20、 将一个表t1的数据导入到另一个表t2
#db2 "insert into t1 select * from t2" 

//修改端口
su - root
vi /etc/services
db2c_db2inst1 50001/tcp -> db2c_db2inst1 60000/tcp
db2stop [force]
db2start 

列出前几条记录
select * from db FETCH FIRST N ROWS ONLY

导出数据到csv文件
export to d:\tablename.csv of del select * from tablename;
原创粉丝点击