SQL(DB2)学习20170913

来源:互联网 发布:我是歌手网络直播地址 编辑:程序博客网 时间:2024/05/01 17:56

窗口命令:
1.列出出现次数>=2的column1
select column1 from A group by column1 having count(1) >=2;

2.复制表B结构,但不复制主键、约束、索引等
create table A as (select * from B) definition only;

3.按column1顺序,取前10行数据
select * from A order by column1 fetch first 10 row only;

4.创建表
create table A
(
column1 varchar(30) not null,
column2 date,

column10 varchar(10),
constraint PK_A primary key (column1))
In tablespacesA index in tablespacesB
comment on table A is ‘表A备注’;
comment on column A.column1 is ‘列column1备注’;

Comment on column A.column10 is ‘列column1备注’;

5.修改表结构
alter table A add column column1 varchar(10) add column column2 decimal(20,2);
Alter table A drop column column1 drop column column2;

6.重组表结构(表结构变更后需要重组)
call sysproc.admin_cmd(‘reorg table A’);

7.在tabname表column列上创建索引
create index index_a on tabname(column);

命令行命令:
1.链接数据库(用用户名:username,密码passwd,数据库名:dbname)
db2 “connecto to dbname user username using passwd”

2.断开数据库(数据库名:dbname)
db2 terminate
db2 disconnect dbname

3.列出数据库实例
db2ilist

4.获取实例的配置参数
db2 get dbm cfg

5.列出数据库目录
db2 list db directory

6.获取dbname的配置信息
db2 get db cfg for dbname

7.查看数据库编目
db2 list db directory

8.查看本地节点
db2 list node directory

9.列出表空间信息
db2 list tablespaces
db2 list tablespaces show detail

10.列出表、视图
db2 list tables

11.查看表A结构
db2 “describe table A”

12.导出数据到.del文件
db2 “export to A.del of del select * from A”

13.导入数据到库表
db2 “load from A.del of del replace into A nonrecoverable”
db2 “import from A.del of del replace into A”

14.清空表数据
db2 “load from /dev/null of del replace into A nonrecoverable”

15.查看表A结构
db2 “describe table A”

16.重组表结构
db2 “reorg table A”

17.更新存储过程
db2 -td@ -vf procedure_name

18.执行.sql文件
db2 -tvf A.sql