对orcale数据库中表的创建以及对表中数据的基本操作

来源:互联网 发布:网络没问题lol延迟高 编辑:程序博客网 时间:2024/05/17 09:34
1.在自己创建的用户方案下用sql语句创建表student,存放在users表空间中,用sql语句添加如下记录;
 
01    要求的约束条件有:
02  主键是学号;入学日期必须大于出生日期;总分必须在0到700之间;学号和姓名不能为空
03  
04  
05create table student (
06    sid numbernot null primary key,
07    namenvarchar2(10) not null,
08    birthdaydate,
09    sdatedate,
10    address nvarchar2(20),
11    mark number,
12    constraintck_sdate check(sdate>birthday),
13    constraintck_mark check(mark>=0and mark<=700))tablespace users;
14  
15 insertinto 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  selectcount(*) as学生总数 from student;
03 
04查询出姓名中第二个字符为“三”而且不姓张的学生;
05  select* from student where namelike '_三%' and name notlike '张%';
06 
07 
08查询出在1982-1-1和1984-1-1之间出生的学生的姓名;
09  selectname,birthday fromstudent 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 birthdayin (selectmax(birthday) fromstudent);
14 
15查询出在学校待的时间最长的学生;
16  select* from student where sdatein (selectmin(sdate) fromstudent);
17 
18计算出所有学生总分的平均分;
19  selectavg(mark) as平均分 from student;
20 
21显示总分最高的学生的总分和姓名;
22  selectname,mark fromstudent where mark in (selectmax(mark) fromstudent);
23 
24删除总分在600以下的学生,然后进行回滚;
25  deletefrom student wheremark<600 ;rollback;
26 
27为表添加两列,一列是sex(性别),一列是speciality(专业),
28  其中,专业部分的默认值是“外语”;且有一个名为CK_SEX的约束条件:性别只能是“男”或“女;
29修改专业的默认值为“计算机”;
30  altertable student add(sex nvarchar2(4)check (sex='男'or sex='女'), speciality nvarchar2(10)default '外语');
31  altertable student modify(specialitydefault '计算机');

[代码] 3、创建成绩表score表,字段名(用英文)和示例数据如下:

 
01id(编号)  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      
09create table score
10(
11    id numbernot null,
12    sid numbernot 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    altertable score dropconstraint SCORE_FK21245050242859;
3    altertable score add(constraint fk_sid foreign key(sid) referencesstudent(sid));

[代码] 显示期末考试在590(含590)以上,且入学总分在610以上的学生的学生编号、姓名和家庭地址;

 
1select sid,name,address from student  wheremark>610 and sid in
2    (selectsid from score where testtype='期末'and score>=590);

[代码] 6、给入学总分在600(含600)以上的学生的期末考试增加10分;

 
1update score set score=score+10where testtype='期末'and sid in(select sid from student where mark>=600);

[代码] 7、显示没有参加考试的学生;

 
1select * from student where sid notin (selectsid from score);
原创粉丝点击