数据库系统概论 知识点整理2

第二章 关系数据库

关系的三种类型:基本表(实际存在的表)、查询表(查询结果对相应的表)、视图表(由基本表和其他表导出)

ps:由于该系列主要针对数据库基础知识和sql基础,所以该章的关系操作、关系演算不做具体整理。

        初次学习者需认真看书,去深刻理解关系数据库的”关系”。

第三章  关系数据库标准语言 SQL

S  Q  L   :结构化查询语言

SQL特点:综合统一;

                 高度非过程化:无需存储过程

                 面向集合的操作方式

                 同一种语法结构提供多种使用方式

                 语言简洁,易学易用

          注:SQL中,一个关系对应一个基本表;一个基本表对应一个存储文件。

视       图:从一个或多个基本表导出的表。

         不独立存储在数据库中,数据库只存放视图的定义,不存放视图对应的数据。该数据仍存在基本表中,

          因此,视图就是一个虚表。

         注 :SQL标准通常不提供修改模式定义、修改视图定义、修改索引定义,若想修改,只能删除重建。


模式的定义与删除

         模式定义:CREATE SCHEMA <模式名> AUTHORIZATION <用户名>

                   注:不指定模式名,就隐含为用户名;创建模式,则该用户必须拥有DBA权限或者 

                          被DBA授予的CREATE SCHEMA   定义模式时也可以定义表                         

                 例1:定义一个学生-课程模式 S-T,用户名为wang

                         create schema “S-T”authorization  wang;

        模式删除 :DROP SCHEMA <模式名><CASCADE| RESTRICT>

                    注:cascade:级联 ,删除模式中的所有数据库对象

                            restrict:限制 ,若该模式定义了下属数据库对象,就会拒绝执行该操作,反之可以执行。


基本表的定义、删除、修改

         表的定义:create  table 表名(字段名 数据类型 列级完整性约束)表级完整性约束

                    注:由于大写字母不易观察学习,以下英文单词均小写

                  例2:建立一个学生表 student

                         create table student(Sno char(9) primary key,  //设置主键

                                                             Sname char(20) unique,  // 取唯一值

                                                             Sage char(2)

                                                            );      

                  例3:建立一个课程表 Course

                         create table Course(  Cno char(4) primary key,

                                                            Cname char(40),

                                                             foreign key Cpno references Course (Cno)   // 设置外键

                                                            );

       修改基本表: alter table 表名 add 列名 数据类型 完整性约束  //增加列和新的完整约束 

                            alter table 表名 drop 完整性约束                      // 删除制定的完整性约束

                            alter table 表名 alter column 列名 数据类型    //修改原有列的名字和数据类型

                  例4:向student表增加入学时间列,数据类型为date

                            alter table student add time date

                  例5:将年龄的数据类型由字符型改为整数

                            alter table student alter column age int

                  例6:增加课程名称必须取唯一值的约束条件

                            alter table student add unique cname

      删除基本表: drop table <表名><cascade| restrict>

                    注:cascade:没有限制条件,删除基本表时,相关依赖对象,视图等都将被删除

                           restrict  :  有条件限制,删除的基本表不能被其他表约束,不能存在依赖对象,

                                          以及有视图、触发器、存储过程或函数

                  例7:删除 student表

                           drop table student  cascade


索引的建立与删除

        索引的建立:create unique |cluster  index <索引名> on <表名>(<列名>[<次序>].....)

                             unique: 每一个索引对应唯一的一个数据记录

                             cluster:聚簇索引

                    例8:在student表的sname上建立一个聚簇索引,并且按照sname的值升序排列

                             create cluster index aaa on student sname 

                    例9:给表student、course、sc 建立索引,student按学号升序建立唯一索引,course按照课程号升序

                             建立唯一索引,sc按照学号升序,课程号降序建立唯一索引

                             create unique index on student sno,

                             create unique index on course cno,

                             craete unique index on sc (sno ,cno desc)

        索引的删除:drop index 索引名

                 例10: 删除student表的aaa索引

                            drop index aaa


数据查询:

                注:我们就直接用例子来学习有关数据查询的知识

一、单表查询

      例11:查询全体学生的学号和姓名

                 select sno,sname from student;

      例12:查询全体学生的姓名、出生年月、所在院系,小写字母表示系名

                 select sname,‘year of brith:’,'2004-sage',LOWER(sdept);

      例13:查询选修了课程的去重后的学生学号

                select distinct sno from sc;

       例14:查询所有年龄在20岁以下的学生姓名及年龄

                  select sname,sage from student where age <20;

       例15:查询考试不及格的学生学号

                  select cno from student where grade<60;

     

       例16:查询年龄在20-23之间的学生姓名、系别和年龄(包括20、23)

                 select sname,department,sage from student where sage between 20 and 23 ;

       例17:查询年龄不在20-23之间的学生姓名、系别和年龄(包括20、23)

                 select sname,department,sage from student where sage not between 20 and 23 ;

       例18:查询计算机系(CS)、信息系(IS)的学生姓名和性别

                  select sname,ssex from student where sdept in('CS','IS');

       例19: 查询所有姓刘的学生的学号、姓名

                   select sno,sname  from student where sname like'刘%';

       例20: 查询所有姓刘,全名为三个字的学生的学号、姓名

                   select sno,sname  from student where sname like'刘_ _';

       例21: 查询所有第二个字为阳的学生的学号、姓名

                   select sno,sname  from student where sname like'_ _阳%'

       例22: 查询DB_Design课程的课程号和学分

                   select cno,ccredit from student where cname like 'DB\_Design' escape '\' ;    //转义字符的使用

       例23: 查询全体学生的情况,查询结果所在的系的系号升序排列,学生年龄降序排列

                    select * from student orderby sdept,sage desc;   

       例24: 查询选修了课程的学生人数

                   select count(distint sno) from sc;

       例25: 查询选修了一号课程的学生最高分                       

                   select MAX(grade) from sc where cno = '1';   //聚集函数不能用在where子句作为条件表达

                    注:聚集函数包括:count(),max(),min(  ) ,sum(    ) ,avg (     )

       例26:  求各个课程号相对应的选课人数

                    select cno,count(sno) from sc groupby cno;


二、多表查询

        例27:查询选修了2号课程,成绩在90分之上的所有学生

                    select * from student,sc where cno='2' and grade>90 and student.sno = sc.sno ;

        例28:查询和刘晨在同一个系学习的学生

                   select sno,sname,sdept from student

                  where sdept in(

                   select sdept from student 

                   where sname='刘晨'

                                );

                   //子查询的条件不依赖父查询,叫不相关子查询。先执行子查询,再执行父查询

       例29: 查询每个学生超过自己选修课平均成绩的课程号

                    select sno,cno from sc x where grade>(select  avg(grade) from sc y where x.sno = y.sno )  

                    //例29为相关子查询


带有any、all 的子查询:

        

      例30:查询其他系中比计算机系的某一个学生年龄小的学生学号和姓名

                  select sno,sname from student 

                            where sage<any(

                                                        select sage from student where sdept='CS' 

                                                       ) and sdept<>'CS'

带有exists 为谓词的子查询:

         注:不返回任何数据,只产生逻辑真值“true”或 逻辑假值“false”

                若内层的结果非空,外层返回真值,反之为假。

                exists引出的子查询,目标列表达式通常以‘ *’,子查询返回true、false,给出列名无意义

      例31:查询所有选修了一号课程的学生姓名

                 select sname from student where exists(  select * from sc where cno='1' and sc.sno=student.sno);

                 //此处仔细回味,为相关子查询

集合查询

      并操作:union    交操作:intersect    差操作:except

           注:使用集合操作的各查询列数必须相同,对应项数据类型必须相同

       例32:查询计算机系学生以及年龄不大于19岁的学生

                  select * from student where sdept = 'CS'

                  union

                  select * from student where sage<='19'

                  //union 操作会自动给合并后的元组去重,union all  保留重复元组

   

             数据查询终于到此结束啦,下一节  数据更新  继续,加油!偷笑

猜你喜欢

转载自blog.csdn.net/qq_39581184/article/details/80375060