34
第第第 第第第第第 oracle 第第第第第 第第 3.1 第第第第第第第第第 3.2 第第第第 3.3 oracle 第第第第第第第第

第三章 关系模型及 oracle 对象关系数据库

  • Upload
    kioko

  • View
    208

  • Download
    0

Embed Size (px)

DESCRIPTION

第三章 关系模型及 oracle 对象关系数据库. 3.1 关系模型和基本概念. 3.2 关系代数. 3.3 oracle 对象关系数据库系统. 关系数据库系统是支持关系模型的数据库系统。 1970 年初, IBM 公司的高级研究员 Dr.E.F.Codd 发表论文提出了关系模型,奠定了关系数据库的理论基础。 Dr.E.F.Codd proposed the relational model for database systems in 1970s. - PowerPoint PPT Presentation

Citation preview

Page 1: 第三章  关系模型及 oracle 对象关系数据库

第三章 关系模型及 oracle 对象关系数据库

3.1 关系模型和基本概念

3.2 关系代数

3.3 oracle对象关系数据库系统

Page 2: 第三章  关系模型及 oracle 对象关系数据库

关系数据库系统是支持关系模型的数据库系统。关系数据库系统是支持关系模型的数据库系统。 19701970 年初,年初, IBMIBM 公司的高级研究员公司的高级研究员 Dr.E.F.Dr.E.F.CoddCodd 发表论文提出了关系模型,奠定了关系发表论文提出了关系模型,奠定了关系数据库的理论基础。数据库的理论基础。 Dr.E.F.Codd proposed Dr.E.F.Codd proposed the relational model for database systems the relational model for database systems in 1970s.in 1970s. Vendors: ORACLE(9i ),IBM(DB2) , MicrVendors: ORACLE(9i ),IBM(DB2) , Microsoft (ACESS and SQLServer), Foxbase(Foosoft (ACESS and SQLServer), Foxbase(Foxpro), Sybasexpro), Sybase

Page 3: 第三章  关系模型及 oracle 对象关系数据库

关系的定义关系( relation) : 一个关系对应一张二维表 元组( tuple):表中的一行即为一个元组属性( attribute):表中的一列为一个属性,给每个属性

起一个名字即属性名( ID , NAME , PHONE , SALE

S-REP-ID )

Page 4: 第三章  关系模型及 oracle 对象关系数据库

主码( primary key) : 表中的某个属性组,它可以唯一确定元组。 each row of data in a table is uniquely identified by a primary key. It must contain a value

外码( foreign key): you can logically relate information from multiple tables using foreign keys.

Page 5: 第三章  关系模型及 oracle 对象关系数据库

关系模型 关系模型由关系数据结构,关系操作和关系完整性约束三

部分组成。数据结构 ——关系(二维表),它由行和列组成。 关系操作——常见的关系操作包括:选择 select ,投影 project ,连接 join ,除 divide ,并 union ,交 intersection ,差 difference 等查询 query 操作和增加 insert ,删除 delete ,修改 update 操作两大部分。 完整性——关系的三类完整性约束

实体完整性 、参照完整性、用户定义的完整性

返回

Page 6: 第三章  关系模型及 oracle 对象关系数据库

关系代数

The relational algebra( 关系代数 ) is the basis of SQL.

Basic Structure ——Cartesian product( 笛卡儿积 )

Formally, given sets D1, D2, …. Dn a relation r is a subset of D1 x D2 x … x Dn , Thus a relation is a set of n-tuples (a1, a2, …, an) where each ai Di

Page 7: 第三章  关系模型及 oracle 对象关系数据库

例如, D1= 导师集合 = { 张清玫,刘逸 } , D2 = 专业集合 = { 计算机,信息 } , D3 = 研究生集合 ={ 李勇,刘晨,王名 }

则 D1 X D2 X D3 ={ ( 张清玫,计算机,李勇 ) , ( 张清玫,计算机,刘晨 ) ,

( 张清玫,计算机,王名 ) ,

( 张清玫,信 息,李勇 ) ,

( 张清玫,信 息,刘晨 ) ,

( 张清玫,信 息,王名 ) ,

( 刘 逸,计算机,李勇 ) ,

( 刘 逸,计算机,刘晨 ) ,

( 刘 逸,计算机,王名 ) ,

( 刘 逸,信 息,李勇 ) ,

( 刘 逸,信 息,刘晨 ) ,

( 刘 逸,信 息,王名 ) },

共 12个元组。笛卡儿积可对应一张二维表。

Example of Cartesian product

Page 8: 第三章  关系模型及 oracle 对象关系数据库

basic operators

Set intersection 交Union 并set difference 差(集合差)Cartesian product 笛卡儿积Select 选择Project 投影Natural join 自然连接Division 除

The operators take two or more relations as inputs and give a new relation as a result.

Page 9: 第三章  关系模型及 oracle 对象关系数据库

(1) Select Operation – Example

• Relation r A B C D

15

1223

773

10

A=B ^ D > 5 (r)A B C D

123

710

注:选择条件必须是针对同一元组中的相应属性值进行比较

Page 10: 第三章  关系模型及 oracle 对象关系数据库

Notation: p(r)

p is called the selection predicate

Defined as:

p(r) = {t | t r and p(t)}

Where p is a formula in propositional calculus consisting of terms connected by : (and), (or), (not)Each term is one of:

<attribute>op <attribute> or <constant>

where op is one of: =, , >, . <. Example of selection: branch-name=“Perryridge”(account)

Page 11: 第三章  关系模型及 oracle 对象关系数据库

(2) Project Operation – Example

Relation r:

A B C

10203040

1112

A C

1112

=

A C

112

A,C (r)

Page 12: 第三章  关系模型及 oracle 对象关系数据库

Notation:

A1, A2, …, Ak (r)

where A1, A2 are attribute names and r is a relation name.The result is defined as the relation of k columns obtained by erasing the columns that are not listedDuplicate rows removed from result, since relations are setsE.g. To eliminate the branch-name attribute of account account-number, balance (account)

Page 13: 第三章  关系模型及 oracle 对象关系数据库

(3) Union Operation – Example

Relations r, s:

r s:

A B

121

A B

23

rs

A B

1213

Page 14: 第三章  关系模型及 oracle 对象关系数据库

Notation: r s

Defined as:

r s = {t | t r or t s}

For r s to be valid :1. r, s must have the same arity ( 等目 , 同元 ,same number of attributes)

2. The attribute domains must be compatible (e.g., 2nd column of r deals with the same type of values as does the 2nd column of s)

E.g. to find all customers with either an account or a loan

customer-name (depositor) customer-name (borrower)

Page 15: 第三章  关系模型及 oracle 对象关系数据库

( 4 ) Set Difference Operation – Example

Relations r, s:

r – s:

A B

121

A B

23

rs

A B

11

Page 16: 第三章  关系模型及 oracle 对象关系数据库

Notation r – sDefined as:

r – s = {t | t r and t s}Set differences must be taken between compatible relations. r and s must have the same arityattribute domains of r and s must be comp

atible

Page 17: 第三章  关系模型及 oracle 对象关系数据库

( 5 ) Cartesian-Product Operation-Example (广义笛卡儿积)

Relations r, s:

r x s:

A B

12

A B

11112222

C D

1010201010102010

E

aabbaabb

C D

10102010

E

aabbr

s

Page 18: 第三章  关系模型及 oracle 对象关系数据库

(6) Set-Intersection Operation

Notation: r s

Defined as:

r s ={ t | t r and t s }

Assume: r, s have the same arity attributes of r and s are compatible

Note: r s = r - (r - s)

Page 19: 第三章  关系模型及 oracle 对象关系数据库

Set-Intersection Operation - Example

Relation r, s:

r s

A B

121

A B

23

r s

A B

2

Page 20: 第三章  关系模型及 oracle 对象关系数据库

Notation: r s

(7) Natural-Join Operation

Let r and s be relations on schemas R and S respectively. Then, r s is a relation on schema R S obtained as follows: Consider each pair of tuples tr from r and ts from s.

If tr and ts have the same value on each of the attributes in R S, add a tuple t to the result, where

t has the same value as tr on r

t has the same value as ts on s

Example:

R = (A, B, C, D)

S = (E, B, D) Result schema = (A, B, C, D, E) r s is defined as:

r.A, r.B, r.C, r.D, s.E (r.B = s.B r.D = s.D (r x s))

Page 21: 第三章  关系模型及 oracle 对象关系数据库

Natural Join Operation – ExampleRelations r, s:

A B

12412

C D

aabab

B

13123

D

aaabb

E

r

A B

11112

C D

aaaab

E

s

r s注:

(1)r,s 必须含有共同属性 ( 名 , 域对应相同 ),

(2) 连接二个关系中同名属性值相等的元组

(3) 结果属性是二者属性集的并集,但消去重名属性。

Page 22: 第三章  关系模型及 oracle 对象关系数据库

Theta join: r s = (r x s)

is the predicate on attributes in the

schema.

• Theta Join is the extension to the Nature

Join.

Page 23: 第三章  关系模型及 oracle 对象关系数据库

(8) Division Operation

Suited to queries that include the phrase “for all”.

Let r and s be relations on schemas R and S respectively where R = (A1, …, Am, B1, …, Bn)

S = (B1, …, Bn)

The result of r s is a relation on schema

R – S = (A1, …, Am)

r s = { t | t R-S(r) u s ( tu r

) }

注:商来自于 R-S(r) ,并且其元组 t 与 s 的拼接被 r 覆盖。

r s

Page 24: 第三章  关系模型及 oracle 对象关系数据库

Division Operation – Example

Relations r, s:

r s: A

B

12

A B

12311134612

r

s

返回

Page 25: 第三章  关系模型及 oracle 对象关系数据库

Oracle 体系结构(1) 物理结构 datafiles redo log files control files parameter file 数据文件 日志文件 控制文件 参数文件

data files redo log files control file parameter iles

*.dbf *.log Initoraid.ora*.ctl

Page 26: 第三章  关系模型及 oracle 对象关系数据库

PMON LCKn RECO RECO

shared Pool Database Buffer CacheRedo Log

Buffer

SGA

Data

Data

Data

Data

Data

Data

Data

Data

Data

Data

Datafiles

Redo Logfiles

ParameterFile

Controlfiles

Server

User

DBWR LGWR

CKPT

ARCH

Offlinestorage

Page 27: 第三章  关系模型及 oracle 对象关系数据库

(2) 内存结构 (SGA) 占 OS 内存的 60-70% ,大小可由参数文件内参数计算 shared pool( 共享池 ) , database buffer cache( 数据缓冲区 ), redo log buffer( 重做日志缓冲区 ) ( 如以下图所示 )SGA=share_pool_size+db_block_size*db_block_buffers+log_buffers

Page 28: 第三章  关系模型及 oracle 对象关系数据库

shared_pool database_buffer_cache redo_log buffer

Page 29: 第三章  关系模型及 oracle 对象关系数据库

SGA

background

process

(3)instance=SGA+background process 实例 = 内存分配 + 一组后台进程 如果把 Oracle 比作一部汽车, instance 相当于汽车的发动机一样 , 启动 oracle 前提应先启动 instance.

Page 30: 第三章  关系模型及 oracle 对象关系数据库

(4)session( 连接 ) Oracle 是多用户、多任务、可分布式管理的数据库,同时可有许 多个用户对数据库操作。

oracle

user user user

session

Page 31: 第三章  关系模型及 oracle 对象关系数据库

(5)transaction( 一组修改动作的集合 ) 交易事务Eg: 1 、 insert DDL ( 数据定义语句 ) delete 例如: create,alter,drop,conmit 等 commit 每两个 DDL 语句间是一个 transaction 2 、 update DML ( 数据控制语句 )

rollback 例如: Insert,Delete,Update

Page 32: 第三章  关系模型及 oracle 对象关系数据库

(6) 后台进程 PMON , LCLN , RECO , SMON , DBWR ,LGWR , CKPT , ARCHPMON 做程序的清洁工作,处理一些不正常退出的事件.SMON 做系统的清洁工作,执行系统出错后自动恢复工作.LCKN Oracle 系统表级或行级加锁的进程 .RECO 恢复进程.DBWR 数据写进程 LGWR 日志文件写的进程CKPT 检测点ARCH 归档方式备份进程

Page 33: 第三章  关系模型及 oracle 对象关系数据库

(7) 分析一个 SQL 语句是怎样在 Oracle 内部工作的。A 、用户发出 SQL 请求,打开游标;B 、把 SQL 语句语法分析,执行计划,数据字典等信息存入内

存中共享池内;C 、从数据文件中把相关数据块读入数据缓冲区;D 、做相应操作,若做修改,先加上行级锁,经确认后,把改

过前后记录内容存入重做日志缓冲区内;E 、返回结果给用户,关闭游标。 备注: SQL 语句大小写敏感的,同样的一个语句,若大小写不同, oracle 需分析执行两次,每句后必以“;”结束。

Page 34: 第三章  关系模型及 oracle 对象关系数据库

PMON LCKn RECO RECO

shared Pool Database Buffer CacheRedo Log

Buffer

SGA

返回