Oracle记事本03_05_数据插入_数据更新_数据删除_空值处理

数据插入

--将元组(学号:201215128 :姓名:陈东, 性别男 ,所在系IS,年龄18岁) 插入学生表中
select * from student;

insert  into student(sno,sname,ssex,sage,sdept) 
values('201215128','陈东','男','18','IS');

insert into student values('201215128','陈东','男','18','IS');

--插入一条选课记录('201215128','1')
insert into sc (sno,cno) 
values('201215128','1');

insert into sc values('201215128','1',null);

--对每一个系求学生的平均年龄,并将结果存入数据库

--方法1 先创建表,插入子查询结果
create table tavg (
  sdept char(20),
  avg_age int );
insert into tavg (sdept,avg_age)
select sdept,avg(sage) pjnl from student group by sdept;

--方法2 简写形式
create table tavg as 
select sdept,avg(sage) pjnl from student group by sdept;

select * from tavg;
drop table tavg;

数据更新

--将学生201215121的年龄改为22岁
update  student set sage = 22 where sno = '201215121';

--将所有学生的年龄增加1岁
update student set sage = sage +1 ;

--将计算机科学系的学生的成绩置为空
update sc set grade = null where sno in
(select sno from student where sdept = 'CS');

数据删除

--删除学号为201215128的学生的学生记录
delete from student where sno = '201215128'; --删除失败,因为选修表以学生表的学号为外码,违反了数据的参照完整性
--解决方法1 先删除选课记录,再删除学生记录
delete from sc where sno = '201215128';
delete from student where sno = '201215128'; 

--删除所有学生的选课记录
delete from  sc ;

--删除计算机科学系的所有学生的选课记录
--先查询出选修表中计算机科学系的学生的学号的集合,在筛选选修表的中的记录进行删除
delete from sc where sno in
( select sno from student where sdept = 'CS');

数据空值

–对于空值
1,判断空值用is null or is not null
2.空值和其他的值进行算术运算的值为空
3,空值与另外一个值的比较运算结果为unknown
4,select distinct 中,空值被为相同
5,聚集函数除了count(*)(运算值为0)外,所有的聚集函数都忽略输入集合中的空值

--将student表中学号为'201215123'的学生的所在系改为空值
update student set sdept = null where sno = '201215123';

--从学生表中找出漏填数据的学上的信息
select * from student where sno is null 
or sname is null
or sage is null
or ssex is null
or sdept is null;

--查询选修1号课程的不及格的学生
select * from sc where grade < 60 and cno = '1';

--查询选修1号课程的不及格的学生和缺考的学生
select * from sc where grade < 60 or grade is null;
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值