oracle中创建选课表约束,数据库学习之实验⼀+Oracle数据库
的基本操作
三、实验内容:
(⼀) 数据定义
⼀、建⽴基本表
创建学⽣表(student)、学⽣选课表(SC)、课程表(course)
1)·学⽣表:Student_学号后四位 (Sno, Sname, Ssex, Sdept)其中学号Sno主码,其中sno为number,sname为
varchar2(10),ssex为char(2),sdept为varchar2(10)
CreatetableStudent_4128(
Snonumberconstraintpkk_snoprimarykey,
Snamevarchar(10),
Ssexvarchar(2),
Sdeptvarchar(10));
--注释
2)·课程表:Course_学号后四位(Cno, Cname, Cpno, Ccredit)其中课程号Cno主码;先⾏课为外码参照Course表中Cno字段。其中cno为number,cname为varchar2(10),cpno为number,ccredit为number(2)。
⽅法1:
createtableCourse_4128(
Cnonumberconstraintpk_cnoprimarykey,
Cnamevarchar(10),
cpnonumber,
ccreditvarchar(2),
constraintfk_cpnoforeignkey(Cpno)referencesCourse_4128(Cno)
);
⽅法2:
createtableCourse_4128(
Cnonumberconstraintpk_cnoprimarykey,
Cnamevarchar(10),
cpnonumber(10)constraintfk_cpnoreferencesCourse_4128(Cno),
ccreditvarchar(2)
);
3)·学⽣选课表:SC_学号后四位(Sno, Cno, Grade)其中(Sno、Cno)为主码;Sno为外码参照Student表中sno字段;Cno为外码参照Course表中cno字段。
createtableSC_4128(
Snonumber(10) ,
Cnonumber(10),
Gradenumber(10,2),
constraintpk_keyprimarykey(Sno,Cno),
constraintfk_snoforeignkey(Sno)referencesStudent_4128 (Sno), constraintfk_cnoforeignkey(Cno)referencesCourse_4128 (Cno)
);
⼆.修改基本表
1)在Student表中加⼊属性Sage(number型)。
altertableStudent_4128addSagenumber(10);
2)修改某个表的属性的数据类型。
修改Student 表中Sdept的数据类型varchar-àvarchar2
altertableStudent_4128modifySdeptvarchar2(10);
3)给表student的sex列添加⼀个⾃定义约束sex只能取’男’,’⼥’两个值。altertableStudent_4128addconstraintS_sexcheck(Ssexin('男','⼥'));
三、索引操作
1.建⽴索引
1)在Student表上建⽴关于Sname的唯⼀索引stusnam+学号后四位createuniqueindexstusnam_4128onStudent_4128 (Sname);
2)在SC表上建⽴关于Sno升序、Cno降序的唯⼀索引i_sc+学号后四位createuniqueindexi_4128onSC_4128(SnoASC,CnoDESC);
2.删除索引
1)删除Student表上的索引stusnam+学号后四位
dropindexstusnam_4128;
2)删除Course表上的索引i_sc+学号后四位
dropindexi_4128;
四.删除基本表
oracle数据库怎么查询表1) 删除基本表Student
drop table student_4128;
drop table student_4128
ORA-02449:表中的唯⼀/主键被外键引⽤
2)删除基本表SC
drop table sc_4128;
结果如何,先执⾏2),在执⾏1)结果如何。
drop table student_4128;
Table dropped
五、单表查询
运⾏如下sql代码:
Create table student as select * from scott.student;
Create table course as select * urse;
Create table sc as select * from scott.sc;
再执⾏如下的查询:
1.求数学系学⽣的学号和姓名。
select Sno,Sname from student where Sdept='MA';
SNO SNAME
--------- --------
20070001李佳
20070003王添
20070006张⼒
2.求选修了课程的学⽣学号。
select distinct Sno from SC where cno is not null;
SNO
---------
20070003
20070001
20070002
20070005
3.求选修课程号为‘2’的学⽣号和成绩,并要求对查询结果按成绩的降序排列,如果成绩相同按学号的升序排列。select sno,Grade from SC where cno='2' order by Grade DESC,Sno ASC;
SNO GRADE
--------- -----
20070002    90
20070003    90
20070001    85
4.求选修课程号为’2’且成绩在80~90之间的学⽣学号和成绩,并将成绩乘以0.8输出。
select sno,Grade from SC where cno='2' and Grade>=80 and Grade<=90;
20070001    85
20070002    90
20070003    90
select Sno,0.8*Grade from SC where cno='2' and Grade>=80 and Grade<=90;
SNO  0.8*GRADE
--------- ----------
20070001        68
20070002        72
20070003        72
5.求数学系或计算机系姓张的学⽣的信息。
select * from student where sname like '张%' and Sdept='MA' or sname like '张%' and Sdept='IS'; SNO SNAME    SSEX SAGE SDEPT
--------- -------- ---- ---- --------------------
20070004张⼒⼥21 IS
20070006张⼒男19 MA
6.求缺少了成绩的学⽣的学号和课程号。
select sno,cno from sc where grade is null;
SNO  CNO
--------- -----
7.查询各个课程号与相应的选课⼈数。
select cno,count(*) from sc group by cno;
CNO  COUNT(*)
----- ----------
1          3
6          1
2          3
4          3
5          2
3          3
7          1
7 rows selected
select cno,count(*) num from sc group by cno order by cno ASC;
1          3
2          3
3          3
4          3
5          2
6          1
7          1
7 rows selected