第1章Oracle11g数据库系统
Oracle数据库系统是世界领先的数据库管理系统,Oracle数据库以其功能强大和配置灵活而著称,同时也因有一定的操作难度,让很多初学者望而却步。本书将由浅入深,以简单易懂的示例带领读者拨开Oracle的神秘面纱。
目前在Oracle世界主要有两类人员:一类是Oracle数据库管理人员,简称DBA;一类是Oracle开发人员。OracleDBA主要的工作是负责日常的数据库维护和性能优化管理。由于Oracle系统较庞大、复杂,要成为一名合格的DBA,需要掌握的知识较多,比如要掌握Oracle体系结构和性能优化等方面的知识,相对其他数据库而言入门门槛较高,但是薪酬一般也较丰厚。Oracle开发人员的主要工作是使用Oracle提供的SQL语言和PL/SQL结构化程序设计语言操作数据库,主要职责是操纵Oracle数据库对象,不需要对Oracle系统结构有深入了解,入门较容易。当Oracle开发人员积累了一定的开发知识后,可以通过学习OracleDBA方面的知识向数据库管理员转型。
1.1关系型数据库系统介绍
1970年6月,IBM公司的研究员E.F.Codd博士(中文名:埃德加·弗兰克·科德),发表了名为“大型共享数据库的关系模型”的论文,受到了学术界和产业界的高度重视和广泛响应,使得关系型数据库系统很快成为数据库市场的主流。E.F.Codd博士被誉为“关系数据库之父”,其照片如图1.1所示。
图1.1关系数据库之父埃德加·弗兰克·科德
1.1.1什么是关系型数据模型
关系型数据库简而言之就是使用关系或二维表存储信息。以公司的人员信息管理为例,为了存储公司内部的员工信息,人事部门一般会建一份列表,在公司未引入信息化管理系统时,大多数人事职员会使用Excel来保存员工信息,例如图1.2是一份简单的人员信息列表的Excel文件。
这种Excel存储数据的方式,将人员的所有信息都包含在一张表中,随着Excel中的栏位和记录数越来越多,这份人员信息表会变得越来越繁杂,这种存储数据的方式称为平面文件数据模型。
为了简化修改与维护的复杂性,关系型数据库设计人员通过使用实体关系模型进行数据库建模,例如人员信息表可以分为员工表和部门表,通过部门编号进行关联,ER模型如图1.3所示。
图1.2Excel人员信息列表图1.3人员信息表ER关系模型
由图1.3中可以看到,通过将员工和部门分别存储在不同的二维表格中,使用主键(PK)和外键(FK)进行关联,使得获取和维护数据变得更容易,这就是关系型数据模型。上述ER图的3个关键组件分别如下所示。
*实体:需要了解的信息,比如部门和员工信息。
*属性:一般也称为列或字段,描述实体必须或可选的信息,比如员工表中的工号和姓名等。
*关系:实体之间指定的关联,比如员工的部门编号关联到了部门表的编号属性。
关系型数据模型还涉及一些较复杂的组成元素,涉及较多的数学知识,有兴趣的读者可以参考一些理论性的读物。
1.1.2数据库系统范式
为了规范化关系型数据模型,关系型数据库系统在设计时必须遵循一定的规则,这种规则称为关系型数据库系统范式。了解范式是每个数据库设计或开发人员必须具备的基本功,范式的主要目的是降低数据冗余,设计结构合理的数据库。目前较常用的范式有如下3种。
1.第一范式(1NF):字段必须具有单一属性特性,不可再拆分
如果字段中的值已经是无法再分割的值,则符合第一范式,即1NF。例如,在员工表中,姓名字段一般仅包含员工的正式姓名,这是符合第一范式的,但是如果要在姓名字段中包含中文名、英文名、昵称、别名等信息,就意味着姓名字段是可再拆分的。因此员工名的设计可以如图1.4所示。
图1.4修改后的员工表以匹配1NF范式
2.第二范式(2NF):表要具有唯一性的主键列
第二范式(2NF)要求数据库表中的每个实例或行必须可以被唯一地区分,为实现区分通常需要为表加上一个列,以存储各个实例的唯一标识。第二范式是在第一范式的基础上的进一步增强,在数据库设计时一般使用唯一性主键来唯一地标识行。比如在员工表中定义了以工号作为主键,因为公司员工的工号通常用来识别某个员工个体,不能进行重复;在部门表中通过部门编号作为主键,来唯一地区分一个部门。
3.第三范式(3NF):表中的字段不能包含在其他表中已出现的非主键字段
第三范式(3NF)是在前两个范式的基础上的进一步增强,主要用来降低数据的冗余。比如,员工表中包含了部门编号,它引用到部门表中的部门编号这个主键,符合第三范式。如果在员工表中又包含一个部门名称,那么表中的字段就包含了其他表中已出现的非主键字段,造成了数据的冗余,不符合第三范式。
范式主要用来规范数据库的设计,使得设计出来的数据库结构清晰,简洁易懂,避免了数据冗余和操作的异常。在设计数据库模型时,灵活地应用范式是创建一个优秀的数据库系统的基石。
1.1.3关系型数据库管理系统
关系型数据库管理系统,简称DBMS,是基于关系型数据库理论而开发的软件系统。目前比较热门的关系型数据管理系统有:Oracle、MicrosoftSQLServer、Access、MySQL及PostgreSQL等。数据库管理系统是用于建立、使用和维护数据库,对数据库进行统一的管理和控制,保证数据库的安全性和完整性的一套大型的电脑程序。数据库管理系统功能结构示意图如图1.5所示。
图1.5数据库管理系统功能结构示意图
如图1.5所示,一个数据库管理系统通常要提供如下所示的几项功能。
*定义数据库结构:DBMS提供数据定义语言来定义(DDL)数据库结构,用来搭建数据库框架,并被保存在数据字典中。
*存取数据库内容:DBMS提供数据操纵语言(DML),实现对数据库数据的基本存取操作,即检索、插入、修改和删除等。
*数据库的运行管理:DBMS提供数据控制功能,即数据的安全性、完整性和并发控制等,对数据库运行进行有效的控制和管理,以确保数据正确有效。
*数据库的建立和维护:包括数据库初始数据的装入,数据库的转储、恢复、重组织,系统性能监视、分析等功能。
*数据库的传输:DBMS提供处理数据的传输,实现用户程序与DBMS之间的通信,通常与操作系统协调完成。
有了关系型数据库管理系统,开发人员就可以在数据库中创建数据库、创建表、存取数据库内容、对数据库进行备份和管理,只需要理解常用的系统相关的操作,而不用去研究关系型数据库系统内部深奥难懂的数据方面的理论知识。
1.1.4使用SQL语句与数据库管理系统通信
关系型数据库管理系统提供了SQL语言,允许用户操纵数据库。SQL语言的全称是结构化查询语言(StructuredQueryLanguage),它是高级的非过程化编程语言,允许用户在高层数据结构上工作。它不要求用户指定对数据的存放方法,也不需要用户了解其具体的数据存放方式,其操作示意图如图1.6所示。
尽管SQL语言已经被ISO组织定义了具有国际标准的SQL规范,但是各种数据库系统厂商在其数据库管理系统中都对SQL规范做了某些编改和扩充。所以,实际上不同数据库系统之间的SQL不能完全相互通用,目前比较流行的两大类SQL语言分别是微软的T-SQL和Oracle的PL/SQL,这两类SQL既有相似之处又有不同之处。
图1.6使用SQL操作数据库管理系统
SQL语言主要又分为如下两大类。
*DML数据操纵语言,主要是完成数据的增、删、改和查询的操作。
*DDL数据定义语言,主要用来创建或修改表、视图、存储过程及用户等。
除此之外,还包含称为DCL的数据控制语言。数据库管理员,即DBA,通常使用DDL来管理数据库的对象,而数据操纵语言DML则主要由数据库开发人员使用来操纵数据。
举个例子,如果想要在数据库中创建如图1.4所示的关系模型,可以使用代码1.1的DDL语句来实现。
代码1.1使用DDL语句创建列和键
--创建员工表
CREATETABLE员工表
(--定义员工表列
工号INTNOTNULL,
中文姓名NVARCHAR2(20)NOTNULL,
英文姓名VARCHAR2(20)NULL,
别名VARCHAR2(20)NULL,
年龄INTDEFAULT18,
入职日期DATENULL,
部门编号INTNULL,
--定义员工表主键
CONSTRAINTPK_员工表PRIMARYKEY(工号)
……
Reviews{{'('+ commentList.posts_count + ')'}}
Have your say. Be the first to help other guests.
Write a review{{i}} star
{{i}} stars
{{ parseInt(commentRatingList[i]) }}%
{{ showTranslate(comment) }}Show Less
{{ strLimit(comment,800) }}Show more
Show Original{{ comment.content }}
{{ formatTime(comment.in_dtm) }} VERIFIED PURCHASE {{groupData}}
{{ showTranslate(comment) }}Show Less
{{ strLimit(comment,800) }}Show more
Show Original{{ comment.content }}
{{ formatTime(comment.in_dtm) }} VERIFIED PURCHASE {{groupData}}
No related comment~
Review
{{commentDetails.user_name}}
{{ showTranslate(commentDetails) }}Show Less
{{ strLimit(commentDetails,800) }}Show more
Show Original{{ commentDetails.content }}
{{ formatTime(commentDetails.in_dtm) }} VERIFIED PURCHASE {{groupData}}
Comments{{'(' + replyList.length + ')'}}
{{ reply.reply_user_name }}reply to{{ reply.parent_user_name }}
{{ showTranslate(reply) }}Show Less
{{ strLimit(reply,800) }}Show more
Show Original{{ reply.reply_content }}
{{ formatTime(reply.reply_in_dtm) }}
That’s all the comments so far!
Please add your comment.
Report
If you find this content inappropriate and think it should be removed from the Yami.com site, let us know please.
Are you sure to delete your review?
Cancel