DB2 基本操作练习 关系型数据库

 

  

 

-- 查询 USER 下所有数据

select *from SQLJ.USER;

-- USER中插入数据

insert into SQLJ.USER(name, age, sex) values('005', 21, '1');

insert into SQLJ.USER(name, age, sex) values('006', 22, '0');

insert into SQLJ.USER(name, age, sex) values('007', 23, '1');

insert into SQLJ.USER(name, age, sex) values('008', 24, '0');

 

-- 删除BOOK表 【好像不适用,下面再学习】

DROP TABLE SQLJ.BOOK;

 

-- 创建BOOK   book 小写自动转大写

create table SQLJ.book (

    id Integer not null,

    name varchar(12),

    userId Integer,

    primary key (id)

);

 

-- 给 book添加外键

alter table SQLJ.book add foreign key(userId) references SQLJ.USER;

 

-- 查看book表数据

select *from book;

 

-- 查看表结构 【尝试未成功,待研究】

describe table SQLJ.USER;

describe table SQLJ.BOOK;

 

 

-- 给book添加两条数据 

-- 添加书本id为1,名称为语文,外键userId为1 ,由于关联表user中无主键为1的记录,所以该条插入失败, SQLCODE=503

insert into SQLJ.book(id, name, userId) values(1, '语文', 1);

 

select *from SQLJ.USER;

-- 下面插入正常的语句

insert into SQLJ.book(id, name, userId) values(1, '语文', 5);

insert into SQLJ.book(id, name, userId) values(2, '语文', 6);

insert into SQLJ.book(id, name, userId) values(3, '语文', 7);

insert into SQLJ.book(id, name, userId) values(4, '语文', 8);

 

-- 简单查询

 

-- 查询name 为001的user

select *from SQLJ.USER where name='001';

-- or 运算符 查询name为001 或者 004的user

select *from SQLJ.USER where name='001' or name='004';

-- and 运算符 查询name为 001 和 004 (这个是没有的)

select *from SQLJ.USER where name='001' and name='004';

-- 查询 age在22 到24 之间的user

select *from SQLJ.USER where age > 22 and age <24;

-- bettwen and 查询年龄在22 到 24 之间的

select *from SQLJ.USER where age between 22 and 24;

-- 查找性别为 0 的user

select *from SQLJ.USER where sex='0';

-- 删除一条记录 删除name 为007的user

delete  from SQLJ.USER where name='007';

 

-- 修改一条记录 USER 修改ID为10的name(006)为 (javaboy)

update SQLJ.USER set name = 'javaboy' where id =10;

-- 给每一个User 加一岁年龄

update SQLJ.USER set age = age + 1;

 

--  查询指定字段 并以别名显示 查询

select name as 姓名, sex as 性别 from SQLJ.USER;

-- 使用表的别名进行查询

select u.name as 姓名 from SQLJ.USER u;

-- 使用 distinct 查询sex种类

-- 其原理为 排序并去重

-- 另外可以参考group by ,我们一般通过group by 进行

-- 分组以后取每组中的一个就可以实现distinct方式 (麻烦)

select distinct(sex) from SQLJ.USER;

-- 条件查询  [< > = <= >= != <>]

 

-- 找出年龄小于22的user

select *from SQLJ.USER where age <= 22;

-- 查询年龄不等于25的user

select *from SQLJ.USER where age <> 25;

-- 查询年龄在22 到 23之间的user

select *from SQLJ.USER where age>=22 and age<= 23;

 

-- 模糊查询

-- like 、 rlike、 %、  _ 、 in 、  not in 、  between and 、 is null 、 not null

select *from SQLJ.USER where name like '%4';

select *from SQLJ.User where name like '00_';   -- _ 占位一个字符

select *from SQLJ.User where name like '0_';

select *from SQLJ.USER where name in ('004', '001', '003', '111')

select *from SQLJ.USER where name not in ('004', '001', '003', '111')

select *from SQLJ.USER where name between '004' and '006';

select *from SQLJ.USER where name is null;

select *from SQLJ.USER where name is not null;

 

 

-- 聚合查询

-- count 计算有多少个user

select count(name) 人数 from SQLJ.USER;

 

-- 统计男女人数分别有多少

select case when(sex=0) then '女' when(sex=1) then '男' else 'xx' end as 性别,  count(sex) 数量 from SQLJ.USER group by sex;

 

-- 找出年龄最大的user

select *from SQLJ.USER

    where age in (

        select max(age) 最大值 from SQLJ.USER

    );

-- 找出年龄最小的user

select  min(age) 最小值 from SQLJ.USER;

-- 计算平均年龄

select avg(age) 平均年龄 from SQLJ.USER;

-- 关联查询

-- 查询

select *from SQLJ.USER;

select *from SQLJ.BOOK;

 

 

select *from SQLJ.USER u join SQLJ.BOOK b on u.id=b.userId;

select *from SQLJ.USER u inner join SQLJ.BOOK b on u.id=b.userId;

select *from SQLJ.USER u left join SQLJ.BOOK b on u.id=b.userId;

select *from SQLJ.USER u right join SQLJ.BOOK b on u.id=b.userId;

 

-- 排序

select *from SQLJ.USER order by age desc;

select *from SQLJ.USER order by age asc;

 

-- 删除、更改列名无 ,通常通过删表重建或视图完成

 

 

 

1.导入

b2 “import from xxx.del of del modified by codel, codepage=1208 inSert into QHSLCP.STORE_INFO

2.导出

export to xxxx.del of del modified by codel! nochardel decplusblank striplzeros codepage=1386;

 

执行sql脚本:db2 -tvf xxxxx.sql

 

 

 

未完后续。。。。。

 

  • 3
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值