实验四 视图、索引及数据更新

更新时间:2024-04-12 08:21:01 阅读量: 综合文库 文档下载

说明:文章内容仅供预览,部分内容可能不全。下载后的文档,内容与下面显示的完全一致。下载之前请确认下面内容是否您想要的,是否完整无缺。

实 验 四 视图、索引及数据更新

一、实验目的:

熟练掌握索引的建立与删除的方法,熟练掌握SQL的应用,熟练掌握数据插入、修改和删除的使用,为后继学习作准备。

二、实验属性(验证性)

1.了解并掌握SQL查询分析器及企业管理器的使用; 2.掌握基本表的定义、删除与修改。

三、实验仪器设备及器材

1.安装有windows操作系统计算机。

2.安装有Oracle11g和SQL Server的计算机。

3.安装有Visual Studio .net和Java编译器(eclipse、Netbean等)的编译器。

4.计算机具备网络环境。

四、实验要求(预习、实验前、实验中、实验后等要求)

1.预习教材第三章,熟悉SQL语句。 2.熟悉.net、Java和Delphi 开发环境。

3.能够熟练掌握.net、Java和Delphi环境下的数据库的编程。

4.掌握建立索引的二种方法,即在基本表中建立和用命令方式建立。 5.掌握删除索引的方法。

6.掌握sql视图建立、修改和删除; 7.掌握sql视图查询。

8.掌握sql数据插入、修改和删除语句的一般格式及使用方法。

五、 实验原理

SQL语言应用。

六、实验步骤:

(1) 启动Oracle的SQL Developer或者SQL Plus,或者SQL Server 查

询分析器;

(2) 对于Oracle11g的SQL Plus需要进行登录,对于Oracle11g的SQL

Developer需要进行建立连接。

如果选择SQL SERVER查询分析器,需要选择数据库;

1 建立索引 建立唯一索引:

例3.1 为学生选课数据库中的Students,Courses,SC三个表建立索引。其中Students表按Sname升序建唯一索引,Courses表按Cname升序建唯一索引,SC表按Sno(学号)升序和Cno(课程号)号降序建唯一索引。

create unique index turing.myIndex

on turing.Student(Sname);

查看自己建立的索引:

SQL>desc dba_indexes [或者SQL>desc all_indexes, SQL>desc user_indexes]

select index_name,index_type,table_name

From user_indexes

Where index_name=’YOUR_INDEX_NAME’;

select t.column_name,t.Index_name,i.index_type

from dba_ind_columns t, dba_indexes i where (t.index_name=i.index_name) and

(t.table_name=i.table_name ) and (t.table_name='STUDENT')

建立位图索引:(选做,理解位图索引的意义,什么情况下才使用位图索引?)

例3.2 为学生选课数据库中的Student的ssex列上建立位图索引。

create bitmap index turing.bm_ixdex_student on turing.student(ssex) tablespace turing.tbs_test

local(partition ix_p1 tablespace tbs_01 partition ix_p2,

partition ix_p3 tablespace tbs_02 partition ix_p4,

partition ix_p5 tablespace tbs_03 )

查看位图索引的创建情况。建立位图索引时,如果报以下错误:“ORA-00439: 未启用功能: Bit-mapped indexes”,是因为该Oracle版本不具有bitmap index功能或未装 bitmap index功能,使用以下查询可以确定:以dba身份连入Oracle,执行以下查询:

select *

from v$option

Where PARAMETER='Bit-mapped indexes';

如果value的值为true表示,安装了bitmap index功能;如果value值为false表示版本不具有bitmap index功能或未装bitmap index功能。使用oracle 的installer把bitmap index功能选上,安装就可以了。 2 删除索引

例3.2 删除基本表SC上的Rep_SCno索引。 drop index turing.index_name; 3 建立视图

例3.4 建立数学系学生的视图C_Student,并要求进行修改和插入操作时仍需保证该视图只有数学系的学生,视图的属性名为Sno,

Sname,Sage,Sdept。

Create view turing.c_student As

Select sno,sname,sage,sdept From turing.student Where sdept=’计科系’;

例3.5 建立学生的学号(Sno)、姓名(Sname)、选修课程名(Cname)及成绩(Grade)的视图Student_CR。

create view turing.student_cr

as

select student.sno,sname,cname,grade from turing.student,turing.course,turing.sc

where sc.sno=student.sno and sc.cno=course.cno;

定义一个反映学生出生年份的视图 student_birth(sno,sname,s_birth,ssex,sdept)。

create view turing.student_birth as

select sno,sname,2013-sage as s_birth,ssex,sdept from turing.student;

例3.6

视图建立后,使用命令查询自己创建的视图:

SQL> create view turing.myView

as

select *

from turing.student where sdept='计科系'

SQL> select view_name,text,view_type

from dba_views

where view_name='MYVIEW';

视图字典有:dba_views,all_views,user_views

4 查询视图

例3.7 在数学系的学生视图C_Student中找出年龄(Sage)小于20岁的学生姓名(Sname)和年龄(Sage)。

select sname,sage from turing.c_student where sage<20;

例3.8 在Student_CR视图中查询成绩在85分以上的学生学号(Sno)、姓名(Sname)和课程名称(Cname)。

select sno,sname,cname from turing.student_cr where grade>85;

例3.9 在视图student_birth(sno,sname,s_birth,ssex,sdept)中查询成绩1900年以后出生的学生信息。

select sno,sname,s_birth,ssex,sdept from turing.student_birth where s_birth>1900;

5 更新视图

例3.9 将数学系学生视图C_Student中学号为S05的学生姓名改为“黄海”。

update turing.c_student

set sname='黄海'

where sno='s05';

例3.7 向数学系学生视图C_Student中插入一个新的学生记录,其中学号为“S09”,姓名为“王海”,年龄为20岁。

insert

into turing.c_student

values('S09','王海',20,null);

例3.11 删除数学系学生视图C_Student中学号为“S09”的记录。

delete

from turing.c_student where sno='S09';

6 删除视图

例3.12 删除视图Student_CR。

drop view turing.student_cr;

7 插入数据

例3.13 设数据库中已有一个关系History_Student,其关系模式与Students完全一样,试将关系Students中的所有元组插入到关系History_Student中去。

insert into turing.History_student

select* from turing.student;

例3.14 在SC表中插入一条新记录,学号为“20050807223”,选的课程号为“C01123”,成绩为89。

insert

into turing.sc

values('20050807223','C01123',89);

8 修改数据

例3.14 将学号为“S03”的学生年龄改为22岁,即要修改满足条件的一个元组的属性值。

update turing.student set sage=22

where sno='S03';

例3.15 将所有学生的年龄增加1岁。即要修改多个元组的值。

update turing.student set sage=sage+1;

例3.16 将数学系所有学生的成绩都加5分。

Update turing.sc Set grade =grade+5

From turing.c_student,turing.sc Where c_student.sno=sc.sno;

where sno='S03';

例3.15 将所有学生的年龄增加1岁。即要修改多个元组的值。

update turing.student set sage=sage+1;

例3.16 将数学系所有学生的成绩都加5分。

Update turing.sc Set grade =grade+5

From turing.c_student,turing.sc Where c_student.sno=sc.sno;

本文来源:https://www.bwwdw.com/article/cyzp.html

Top