数据插入
--将元组(学号: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;