DB2初学者学习

来源:互联网 发布:网上商城java源码 编辑:程序博客网 时间:2024/05/19 12:40

我是一名新学习DB2的学员,本文档供初学者借鉴!

 

1.下载地址:(我是在一个博客上看到的,很详细,直接提供他博客地址)

http://happyqing.iteye.com/blog/2082305

(备注:windows10不适用这个链接下的版本!)

 

2.安装完成,打开方式

window+R(运行)输入db2cc 或者db2cmd就可以db2的控制中心

 

3.写SQL语句

选择数据库-右键-查询:弹出【命令编辑器的窗口】,在这个窗口你可以写sql语句命令

下面直接用例子操作,以便于更加明白

a.创建tstudent学生表并实现id自增

create table tstudent(
    id numeric not null primary key
      generated always as identity(start with 1 increment by 1 minvalue 1 maxvalue 9999 no cycle cache 20 no order) ,
    name varchar(30) ,
    sex varchar(10),
    classcode varchar(30)
);

 

b.为字段添加注释

comment on table tstudent is   '测试学生表';
comment on column tstudent.id is '学生编号';

comment on column tstudent.name is'学生姓名';

 

c.增加一列
alter table tstudent  add column hobby varchar(200);

alter table tstudent add column birthdate date;

 

d.建立索引

create index l_TSTUDENT_CLASSCODE on tstudent(classcode);

 

e.插入数据

insert into tstudent(name,sex,classcode,hobby) values('小明','男','C131','唱歌,游泳');
insert into tstudent(name,sex,classcode,hobby) values('小张','男','C132','读书,听音乐');

 

f.修改数据

update tstudent set sex='女' where name='小张';

 

g.删除数据

1).delete from tstudent;

2).alter table tstudent activate not logged initially with empty table;(删除不记录日志)

3)drop table tstudent;(把数据删了连同表结构一起删除)

 

h.分页查询

select * from (
select row_number() over (order by id asc ) as row_number ,t.* from   tstudent  t  )  a

  where a.row_number > 10

  fetch first 10 rows only

(备注:第一页,row_number>0每页查询10条记录,第二页,row_number>10 以此类推)

 

 

 

 

 

 

 

 

 

 

0 0
原创粉丝点击