An Image/Link below is provided (as is) to download presentation
Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.Content is provided to you AS IS for your information and personal use only.Download presentation by click this link.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.During download, if you can't get a presentation, the file might be deleted by the publisher.
§3. 2 数据定义---修改基本表 例4:向Student 表增加“入学时间”列,其数据类型为日期型 ALTER TABLE Student ADD Scome DATE; 例5:将年龄的数据类型改为半字长整数 ALTER TABLE Student ALTER COLUMN Sage SMALLINT; 例6:删除学号必须取唯一值的约束 ALTER TABLE Student DROP UNIQUE(Sno);
§3. 2 数据定义---删除基本表 DROP TABLE <表名>; 例7:删除基本表 Student. DROP TABLE Student ;
§3. 2数据定义---定义索引 CREATE [UNIQUE] [CLUSTER] INDEX <索引名> ON <表名> (<列名>[<次序>][,<列名>[<次序>]]…); 其中:Unique 表示此索引的每一个索引值只对应唯一的数据记录。 Cluster表示要建立的索引是聚簇索引。 表名:要创建索引的基本表的名字。 索引可以建立在该表的一列或多列上,各列名之间用都好分隔。 每个列名后面还可以用次序指定索引值的排列次序,包括ASC 和、DESC两种,默认值为ASC.
§3. 2 数据定义---定义索引 • 例8:为 Student, Course, SC三个表建立索引。其中 Student • 表按学号升序建立唯一聚簇索引,Course 表按课程号升序建立唯一索引,SC表按学号升序和课程号降序建立唯一索引。 • CREATE Unique Cluster INDEX Stud ON Student (Sno); • CREATE Unique INDEX Cnoc ON Course(Cno); • CREATE Unique INDEX ON SC (Sno ASC,Cno DESC);
§3. 2 数据定义----删除索引 • 索引一经建立,就由系统使用和维护它,不需用户干预。建立索引是为了减少查询操作时间,但如果数据增删改频繁。系统会花费许多时间来维护索引。这时可以删除一些不必要的索引。 • 删除索引语句格式: • DROP INDEX <索引名> • 例9:删除Student表的Stusno索引。 • DROP INDEX Stusno;
例5 查询全体学生的姓名,出生年份和所有系,要求用小写字母表示所有系名 • SELECT Sname ,`Year of Birth` ,2008-Sage, • LOWER(Sdept) • FROM Student ;
结果为: • Sname ‘Year of Birth:’ 2003-Sage ISLOWER(Sdept) • ………. ……………… …………. …………………. • 李勇 Year of Birth: 1988 cs • 刘晨 Year of Birth: 1989 is • 王名 Year of Birth: 1990 ma • 张立 Year of Birth: 1989 is
用户可以通过指定列名来改变查询结果标题。例如, 对于上例,可以如下定义列别名 • SELECT Sname NAME, ‘Year of Birth:’ BIRTH, • 2003-Sage BIRTHDAY, • LOWER(Sdept) DEPARTMENT • FROM Student;
结果为: • NAME BIRTH BIRTHDAY DEPARTMENT • ……….. …..….. ….………… …..…………. • 李勇 Year of Birth: 1988 cs • 刘晨 Year of Birth: 1989 is • 王名 Year of Birth: 1990 ma • 张立 Year of Birth: 1989 is
(2) 确定范围 • 谓词Between…. And …和 Not Between …And …可以用来查找属性值在(或不在)指定范围内的元组 • 其中 Between后是范围的下限,and 后是范围的上限
例10. 查询年龄在20~23岁(包括20岁和23岁)之间的学生的姓名、系别和年龄 • Select Sname,Sdept, Sage • From Student • Where Sage Between 20 and 23; • 例11.查询年龄不在20~23岁(包括20岁和23岁)之间的学生的姓名、系别和年龄 • Select Sname,Sdept, Sage • From Student • Where Sage not Between 20 and 23
(3) 确定集合 • 谓词IN可以用来查找属性值属于指定集合的元组 • 例12:查询计算机科学系(CS),数学系(MA)和信息系(IS)学生的姓名和性别 • Select Sname, Ssex • From Student • Where Sdept in (‘CS’,’MA’,’IS’);
例13. 查询既不是计算机科学系、数学系,也不是信息系的学生的姓名和性别 • Select Sname,Ssex • From Student • Where Sdept not in (‘CS’,’MA’,’IS’);