第一题
a
创建以下 3 个表,要求所有的约束都要建立约束名
- 学生表:学号,姓名,所属系,年龄。其中学号为主键,姓名要求非空约束
- 课程表:课程号,课程名,先行课号。其中课程号为主键,课程名要求非空约束
- 学生成绩表:学号,课程号,成绩。其中学号与课程号为联合主键,学号为外键,参照学生表的学号属性;课程号为外键,参照课程表的课程号属性。分数要求在
0-100 之间
1 2 3 4 5 6
| create table s (s# varchar2(8), sn varchar2(8) , sd varchar2(20), sa number(3), constraint S_CHECK check (sn is not NULL), primary key(s#));
|
1 2 3 4 5
| create table c (c# varchar2(8), cn varchar2(10) , pc# varchar2(8), constraint C_CHECK check (cn is not NULL), primary key (c#));
|
1 2 3 4 5 6 7
| create table sc (s# varchar(8), c# varchar(8), g number(3), constraint SC_FK foreign key (s#) references s(s#), constraint SC_CHECK check (g between 0 and 100) );
|
使用sqlldr
导入数据
1 2 3
| sqlldr userid=scott/tiger control=s.ctl log=s.log sqlldr userid=scott/tiger control=c.ctl log=c.log sqlldr userid=scott/tiger control=sc.ctl log=sc.log
|
- 试往以上表中插入(或修改)各种违反约束条件的数据,观察所出现的出错提示
1
| insert into sc (s#,c#,g) values ('S1000','C100',10000);
|
1
| insert into c (c#,cn,pc#) values ('c1000','','pc100');
|
b
列出工资从高到低排列的第 3-5 名员工
若使用rownum between 3 and
5
会出现一行都检索不到的结果,这是因为第一条数据行号为1,不符合>=3的条件,所以第一行被去掉,之前的第二行变为新的第一行(即这个行号不是写死的,可以理解为是动态的),如此下去,一直到最后一行,条件始终没法满足,所以就一条数据也查不出来。
1 2 3
| create or replace view vtop as select * from emp order by sal desc; create view vtop1 as select vtop.*,rownum rn from vtop; select * from vtop1 where rn between 3 and 5;
|
第二题
暂时关闭学生成绩表上的外键约束,然后插入违反该约束的数据,再次打开它,观察会出现什么情况
1 2
| alter table sc disable constraint SC_CHECK; insert into sc (s#,c#,g) values ('S1','C1',-10);
|
第三题
把 EMP 表导出到 Excel 表单中,创建test.sql
脚本文件
1 2 3 4 5 6 7 8 9 10
| cd 'root/Database/HW4' spool on spool test.csv set colsep ","; set heading off; SET feedback off; SET echo off; select * from emp; spool off exit
|
1
| sqlplus scott/tiger @export.sql
|