1.在自己创建的用户方案下用sql语句创建表student,存放在users表空间中,用sql语句添加如下记录;
01 | 要求的约束条件有: |
02 | 主键是学号;入学日期必须大于出生日期;总分必须在0到700之间;学号和姓名不能为空 |
03 | |
04 | |
05 | create table student ( |
06 | sid number not null primary key , |
07 | name nvarchar2(10) not null , |
08 | birthday date , |
09 | sdate date , |
10 | address nvarchar2(20), |
11 | mark number, |
12 | constraint ck_sdate check (sdate>birthday), |
13 | constraint ck_mark check (mark>=0 and mark<=700))tablespace users; |
14 | |
15 | insert into student values (1001, '张三' , '1-1月-1981' , '1-1月-1999' , '上海' ,600); |
16 | |
17 | |
18 | 用OEM再创建以上表student2,添加约束,并在数据输入窗口输入以下记录: |
19 | |
20 | 表字段名(英文部分)及部分数据如下: |
21 | sid(学号) name (姓名)birthday(出生日期)sdate(入学日期)address(家庭地址 ) mark(入学总分) |
22 | 1001 张三名 1981-1-1 1999-1-1 张三名的家庭地址 600 |
23 | 1002 李三名 1982-2-2 2000-1-1 李三名的家庭地址 620 |
24 | 1003 张四名 1983-3-3 2001-1-1 张四名的家庭地址 580 |
25 | 1004 李四名 1984-4-4 2002-1-1 李四名的家庭地址 592 |
[代码] 2.针对以上student表,用sql语句完成如下操作:
01 | 计算出学生总数; |
02 | select count (*) as 学生总数 from student; |
03 |
04 | 查询出姓名中第二个字符为“三”而且不姓张的学生; |
05 | select * from student where name like '_三%' and name not like '张%' ; |
06 |
07 |
08 | 查询出在1982-1-1和1984-1-1之间出生的学生的姓名; |
09 | select name ,birthday from student where birthday between to_date( '1982-1-1' , 'yyyy-MM-dd' ) and to_date( '1984-1-1' , 'yyyy-MM-dd' ); |
10 |
11 |
12 | 查询出年龄最小的学生; |
13 | select * from student where birthday in ( select max (birthday) from student); |
14 |
15 | 查询出在学校待的时间最长的学生; |
16 | select * from student where sdate in ( select min (sdate) from student); |
17 |
18 | 计算出所有学生总分的平均分; |
19 | select avg (mark) as 平均分 from student; |
20 |
21 | 显示总分最高的学生的总分和姓名; |
22 | select name ,mark from student where mark in ( select max (mark) from student); |
23 |
24 | 删除总分在600以下的学生,然后进行回滚; |
25 | delete from student where mark<600 ; rollback ; |
26 |
27 | 为表添加两列,一列是sex(性别),一列是speciality(专业), |
28 | 其中,专业部分的默认值是“外语”;且有一个名为CK_SEX的约束条件:性别只能是“男”或“女; |
29 | 修改专业的默认值为“计算机”; |
30 | alter table student add (sex nvarchar2(4) check (sex= '男' or sex= '女' ), speciality nvarchar2(10) default '外语' ); |
31 | alter table student modify (speciality default '计算机' ); |
[代码] 3、创建成绩表score表,字段名(用英文)和示例数据如下:
01 | id(编号) sid(学生编号) testtype(考试类型) score(分数) |
02 | 1 1001 期中 580 |
03 | 2 1001 期末 590 |
04 | 3 1002 期中 570 |
05 | 4 1002 期末 595 |
06 | 5 1003 期中 570 |
07 | 6 1003 期末 565 |
08 | |
09 | create table score |
10 | ( |
11 | id number not null , |
12 | sid number not null , |
13 | testtype nvarchar2(10), |
14 | score number |
15 | ); |
[代码] 用OEM界面给score创建一个外键,对应student的主键,然后在sql*plus中用sql语句删除此外键,
1 | 用OEM界面给score创建一个外键,对应student的主键,然后在sql*plus中用sql语句删除此外键,再用sql语句为score创建一个外键; |
2 | alter table score drop constraint SCORE_FK21245050242859; |
3 | alter table score add ( constraint fk_sid foreign key (sid) references student(sid)); |
[代码] 显示期末考试在590(含590)以上,且入学总分在610以上的学生的学生编号、姓名和家庭地址;
1 | select sid, name ,address from student where mark>610 and sid in |
2 | ( select sid from score where testtype= '期末' and score>=590); |
[代码] 6、给入学总分在600(含600)以上的学生的期末考试增加10分;
1 | update score set score=score+10 where testtype= '期末' and sid in ( select sid from student where mark>=600); |
[代码] 7、显示没有参加考试的学生;
1 | select * from student where sid not in ( select sid from score); |