建立学生表
create table Student
(
Sno char(20) primary key,
Sname char(10) not null,
Sage int,
Ssex char(5),
Sdept char(20)
);
建立课程表
create table Course
(
Cno char(10) primary key,
Cname char(20) not null,
Cpno char(10),
Ccredit int,
foreign key(Cpno)references Course(Cno)
);
建立学生选课表
create table SC
(
Sno char(20),
Cno char(10),
Grade numeric(5,2),
foreign key(Sno) references Student(Sno),
foreign key(Cno) references Course(Cno)
);
4. 运行下列语句,为基本表添加数据;
--以下为学生表的初始数据
insert into Student(sname,ssex,sno, sage, sdept) values('李勇','男','200215121',20,'CS');
insert into Student(sname,ssex,sno, sage, sdept) values('刘晨','女','200215122',19,'CS');
insert into Student(sname,ssex,sno, sage, sdept) values('王敏','女','200215123',18,'MA');
insert into Student(sname,ssex,sno, sage, sdept) values('张立','男','200215125',19,'IS');
--以下为课程表的初始数据
insert into course values('6','数据处理',null,2); insert into course values('2','数学',null,2);
insert into course values('7','PASCAL 语言','6',4); insert into course values('5','数据结构','7',4);
insert into course values('1','数据库','5',4); insert into course values('3','信息系统','1',4);
insert into course values('4','操作系统','6',3);
--以下为选修表的初始数据
insert into sc values('200215121','1',92);
insert into sc values('200215121','2',85);
insert into sc values('200215121','3',88);
insert into sc values('200215122','2',90);
insert into sc values('200215122','3',80);
commit;
5. 修改 Student 表,为 Student 表格添加一个“入学时间”属性,属性名为 Senrollment;
alter table student
add senrollment date;
6. 修改 Student 表,把 Ssex 列的宽度设置为 6 个字节;
alter table student modify ssex char(6);
7. 修改 Student 表,删除 Senrollment 列;
alter table student drop column senrollment;
8. 创建视图 ds,该视图包含所有选修了“数据库原理”的学生信息;
create view ds as select * from student where sno in(select sno from sc where cno in(select cno from course where cname='数据库原理'));
9. 创建视图 malestudent,该视图包含男学生所有信息,通过视图 malestudent 更新基本表数据时必须保证学生性别为男;
create view malestudent as select * from student where ssex='男' with check option;
10. 删除视图 malestudent;
drop view malestudent;
11. 为 Course 表的 CName 列建立唯一索引,索引名称为 uniqueCname;
create unique index uniquecname on course(cname);
12. 试着为 Course 表的 Cpno 列建立唯一索引,索引名为 indexCpno1,如果发生错误, 请说明普通索引和唯一索引有何区别;
create unique index indexcpno1 on course(cpno);
ORA-01452: 无法 CREATE UNIQUE INDEX; 找到重复的关键字
唯一索引只能对应唯一数据记录.
13. 为 Course 表的 Cpno 列建立普通索引,索引名称为 indexCpno2
create index indexcpno2 on course(cpno);
14. 删除索引 indexCpno2;
drop index indexcpno2;
15. 删除基本表 Student,如果发生错误,请分析原因;
drop table student;
ORA-02449: 表中的唯一/主键被外键引用
因为主键被course引用.
16. 删除基本表 SC;
drop table sc;