--创建名为student的表
create table student(
--设置学号为主键(主键约束primary key)并且不能为空(非空约束not null)
studentID number primary key not null,
studentName varchar2(20) not null,
studentSex varchar2(10) not null,
--设置电话号码为唯一约束unique
phone number(11) unique)
create table subject(
subjectID number primary key not null,
subjectName varchar2(20) not null)
create table mark(
markID number primary key not null,
studentID number,
subjectID number,
dates date,
markValue number(5,2),
--设置外键连接
constraint fid_student foreign key(studentID) references student(studentID),
constraint fid_subject foreign key(subjectID) references subject(subjectID))
--给各表插入内容
insert into student values(1,'张三','男',90909701)
insert into student values(2,'李思','女',74741748)
insert into student values(3,'王五','男',88888888)
insert into student values(4,'野人','男',52013143)
insert into student values(5,'龙女','女',66666666)
insert into student values(6,'二六','男',10101003)
insert into student values(7,'死神','女',44444444)
insert into subject values(1,'语文')
insert into subject values(2,'数学')
insert into subject values(3,'英语')
insert into mark values(1,1,1,date'2005-04-03',90)
insert into mark values(2,1,2,date'2005-05-03',99)
insert into mark values(3,1,3,date'2005-04-13',69)
insert into mark values(4,2,1,date'2007-04-03',80)
insert into mark values(5,2,2,date'2005-05-03',91)
insert into mark values(6,2,3,date'2006-04-13',75)
insert into mark values(7,3,1,date'2005-04-03',50)
insert into mark values(8,3,2,date'2006-08-08',99)
insert into mark values(9,3,3,date'2006-03-03',88)
insert into mark values(10,4,1,date'2007-07-07',91)
insert into mark values(11,4,2,date'2005-04-03',60)
insert into mark values(12,4,3,date'2007-11-09',59)
insert into mark values(13,5,1,date'2005-09-09',60)
insert into mark values(14,5,2,date'2005-04-03',97)
insert into mark values(15,5,3,date'2006-12-13',94)
insert into mark values(16,6,1,date'2005-04-03',30)
insert into mark values(17,6,2,date'2005-04-03',55)
insert into mark values(18,6,3,date'2007-06-23',29)
insert into mark values(19,7,1,date'2006-05-05',null)
insert into mark values(20,7,2,date'2005-12-12',null)
insert into mark values(21,7,3,date'2007-01-01',null)
--问题1:增加学生商人
insert into student values(8,'商人','男',010101011)
--问题2:把商人的名字改为“七郎”
update student set studentName='七郎' where studentID=8
--问题3:删除杨七郎记录
--目标:使用delete语句
--语法:delete表名where条件(删除那些记录)
--不加条件:删除全部记录
delete from student where studentID=7
--问题4:单表查询
--查询:查询姓名不为空的同学(学号,姓名)
select studentID,studentName from student where studentName is not null
--问题5:查询试图
--目标:创建试图,使用试图
--语法:create or replace view 试图名 as select语句
--说明:只能是查询语句,不能有insert语句
--什么是试图?试图可以理解为查询语句的别名
create view studentSummary as
select student.studentid,student.studentname,subject.subjectname,mark.markvalue
from student,subject,mark where student.studentid=mark.studentid
and subject.subjectid=mark.subjectid
--问题6:查询今天的日期以及计算99*98
--目录:使用 dual求日期以及表达式的值,字段名
--问题:今天日期99*98的结果都没有保存在一个表,所以要用虚表
select to_char(sysdate,'yyyy-mm-dd'),99*98 from dual
--问题7:查询各科的成绩(学科编号,学科名称,分数)
--目标:学习表间关联
select subject.subjectid,subject.subjectname,mark.markvalue from subject,mark
where subject.subjectid=mark.subjectid
--问题8:只显示语文成绩,并按从高到低排列
--目标:学习order by 语句
--语法:order by 字段(desc 可选)
select subject.subjectid,subject.subjectname,mark.markvalue from subject,mark
where subject.subjectid=mark.subjectid and subject.subjectname='语文'
order by mark.markvalue desc
--问题9:查询在2005-4-3和2007-7-13之间的考试记录
--目标:学习between语句和日期字段查询(to_char和to_date)
--between语法:(字段)between(开始日期)and (结束日期)
--to_char语法:to_char(字符串,格式)
--to_date语法:to_date(字符串,格式)
--问题:什么时候用to-char,需要字符类型的用to_char
--问题:需要date类型事用dto_date
select student.studentname,subject.subjectname,mark.dates,mark.markvalue
from student,subject,mark where student.studentid=mark.studentid and
subject.subjectid=mark.subjectid and mark.dates
between to_date('2005-4-4','yyyy-mm-dd') and to_date('2007-7-13','yyyy-mm-dd')
select to_char(sysdate,'yyyy-mm-dd') from dual
select to_date('2007-05-19','yyyy-mm-dd') from dual
--问题10:查询成绩在73到90之间的考试记录
--目标:学习between
select student.studentname,subject.subjectname,mark.dates,mark.markvalue
from student,subject,mark where student.studentid=mark.studentid and
subject.subjectid=mark.subjectid and mark.markvalue between 73 and 90
--问题11:查询在2005年5月份(或5季度)的考试记录
--目标:灵活使用to_char
select student.studentname,subject.subjectname,mark.dates,mark.markvalue
from student,subject,mark where student.studentid=mark.studentid and
subject.subjectid=mark.subjectid and
to_char(mark.dates,'Q')='5' 或者 to_char(mark.dates,'yyyy-mm')='2005-05'
--问题12:查询分数是99或为空的学科记录
--目标:学习in和nvl
--in语法:(字段) in (值列表)
--nvl语法:nvl(值1),字段中所有null值都用“值1”临时代替输出
select student.studentname,student.studentsex,subject.subjectname,mark.markvalue
from student,subject,mark where student.studentid=mark.studentid and
subject.subjectid=mark.subjectid and nvl(mark.markvalue,0) in(99,0)
--问题13:查询什么科目都没有考试的同学
--目标:学习not exists的用法
--语法:主表 where not exists(从表 where 对等连接条件)
select student.studentID,student.studentname from student where not exists(
select * from mark where
student.studentid=mark.studentid and mark.markvalue is not null)
--问题15:统计各科最高分,最低,平均,考试人数
--目标:学习group by 的基本用法
--语法:select(group by 字段列表1)(统计函数字段列表2)from group by字段列表1
--要点:一个字段要么在group by 后面,要么在统计函数中
--理解:有几个组则返回几条数组,不能多也不能少
select subject.subjectname,max(mark.markvalue),min(mark.markvalue),avg(mark.markvalue),
count(mark.markvalue) from subject,mark where subject.subjectid=mark.subjectid
group by subject.subjectname
回复Comments
{commenttime}{commentauthor}
{CommentUrl}
{commentcontent}