# 資料庫期中
# 第一大題 名詞解釋
三角形記號△ 表示未曾於考古題出現過...
或是有考古我們沒看過:D
## Ch1 - Basic Definitions:
### △ Database
a collection of related data
一群相關資料的集合
### △ Mini-World
some part of the real world about which data is stored in database
### DBMS (Database Management System)
一個方便創建、維持 computerized database 的系統/軟體
> 利於創造或管理電腦中database的軟體或系統。
### △ Database System
The DBMS software together with the data itself.
> DBMS 和 data 的合稱
### △ Queries
Access data and formulate the result of a request.
> 對於data的retrieval request
### △ Transactions
1.may read data
2.update value
3.generate new data and store it in database
### △ OLTP
Online Transaction Processing. Allow hundreds of concurrent transaction to execute per sec
## Ch2 - Models & Schema
### △ Data Model
用以描述資料庫結構(structure)觀念的集合
> 用於描述database的:
> 1.structure
> 2.operations
> 3.constraints
> 之概念集合
### △ Data Model Operation
用以維持資料庫結構,包括 retrieval 和 update
又分 basic model operation (insert, delete) 與 user-defined operation (自定義函數)
### Internal (Database) Schema
描述 physical storage structure and access path
database 中,每個 entity type 中的內部構造
> 描述了database的實體儲存結構、索引或路徑。
### △ Conceptual Schema
描述 structure and constraint for users
### △ Exterval Schema
描述 various user views
### DDL
Data Definition Language。用於明確定義概念綱要(conceptual schema)及內部綱要(internal schema)
> 全稱Data Definition Language,
> 用於定義database的:conceptual, internal, external schemas.
### Network Data Model
一種靈活彈性表現以表現 entity 與 relationship 的方式。該模型常被視作 graph,其中 entity 為 node,relationship 為 arc (edge)
> 優點:
> 適合複雜的relationships
> 可表示對relationship新增/刪除的語義
> 可應對大部分使用record types和relationship types的情況
>
> 缺點:
> Database中包含複雜的pointer結構
### Hierarchical Data Model
entity間有階層式的上下關係。ex. tree-like structure
> Data為樹狀(階層)結構的model.
> 優點:
> 易於建構與操作
> 可對應至許多階層組織化的領域
>
> 缺點:
> 視覺化後為線型排列
> Query的優化空間小
### △ Relational Model
> 基於數學中"集合"概念的model.
### △ Object-Oriented Model
> 包含OOP語言(如:C++, Smalltalk)的model.
### Object-Relational Model
> 包含object database概念的relational systems的model.
## Ch3 - ER Model:
### Entity
A single unique object in the real world that is being mastered
A thing in a real-world with independent existence
> 代表在database的mini-world中特定的物件,
> 如:EMPLOYEE的John Smith, PROJECT的ProductX.
### △ Entity Type
指特定實體所屬的類別
### Entity Set
所有屬於同一 entity type 的 entities 的集合
A set of all entities of a particular entity type
> 又稱entity collection。
> Database中,屬於一entity type的許多entities.
### Weak Entity (Type)
沒有 primary key 的 entity type,需要借用自其他 entity 的 attribute 建構一個 unique identifier
entity's key is partial key
those that need an attribute from another entity (i.e. a foreign key) to constitute a unique identifier
> 沒有primary key的entity,需藉由與其他entity間的relation才可辨識。
### Attribute
trait of an entity type that describes the entity
> 用於描述entity的屬性們,如:某EMPLOYEE entity的Name, Age,
> 擁有各自的data type (e.g., string, int).
### Atomic/Simple Attribute
一個不可再分割的 Attribute
> 此attribute的值為單一且不可分割,如:SSN, Sex.
### Composite Attributes
可由其他 attribute 構成、組合出的 attribute
> 由多個attributes組成的attribute,如:Name(FirstName, LastName).
### Relationship
關聯,兩實體屬性相關
> 兩個以上不同的entities間,有著特定意涵的關聯。
### 1:N Relationship
(畫圖表示)
□---1--◇--N---□
### Cardinality Ration (Constraint)
兩個 entity 參與的比例。
通常有三種。一對一(1:1)、一對多(1:N)、多對多(N:M)
> 定義兩entities間,relationship的最大參與量,
> 如:一對一(1:1)、一對多(1:N)、多對多(N:M).
### Total Participation
該 entity type 下的每個 entity 皆須藉relationship 對應到另一個 entity type,不可沒有對應到的 entity type存在。
Each in the entity set is involved in at least one relationship in a relationship set
> 對於一entity set E和一relationship set R,
> E中的每一個entity皆參與了R中至少一個relationship。
## Ch4 - EER Model:
### Specialization
將一個高層級的 entity 分成更細類別的過程
> 根據superclass中的不同特徵而特化出許多subclasses的過程。
### Generalization
將兩個(以上) entity 的共同屬性合併,形成新實體的過程
ex. Truck, Vehicle 整合為 Car
> 根據許多subclasses中的相同特徵而普遍化出一個superclass的過程。
## Ch5 - Formal Definitions & Constraints in Relational Data Model:
### Domain
the set of legal values that can be assigned to an attribute
一群合法且可被指派為attribute的值的集合
一個attribute的資料定義域
> 一個attribute的定義域(合法value的範圍)
### Relation State
在某一時間點下存在的state的情況
relation在某一時間點的資料狀態
> 一個relation之所有attributes的domains中,
> 所有possible values的卡氏積(Cartesian product、笛卡爾積、直積)之子集合。
### Relation Instance
同relation state
> Relation在某個時間點的state.
### △ Key Constraint
> 在一relation state中,任兩個tuples不能有相同的key value.
### Entity Integrity (Constraint)
用以限制 entity 的 primary key 為 unique 且不可為 NULL
> Entity中primary key的值不可為null.
### Referential Integrity Constraint
A constraint involving 2 relations for specifing a relationship among tuple in 2 relations
> 為兩個relations間的constraints.
> Referencing relation中,foreign key的值:
> (1) 存在referenced relation中primary key的值與之對應
> (2) 為null
### Implicit/Inherent Constraint
> Data model 與生俱來的限制,如:list不可為任何attribute中的值。
### Explicit/Schema-Based Constraint
> They are expressed in the schema by using the facilities provided from the model. (e.g., cardinality ratio constraint in the ER model)
### Application-Based/Semantic Constraint
用於確保資料庫在結構、內容上準確反映現實世界
> 基於應用情境而無法直接於data model schema中表達的constraints.
## Ch8 - Relational Algebra & Calculus:
### Union Compatible
聯集相容的。指兩關系的結構必須相同,包括擁有相同的屬性名稱、屬性數量和屬性資料型別
2 relation must be type compatible, which have same number of attributes.
Each pair of corresponding attributes must have same or compatible domain
> 又稱type compatible。
> 兩個relations的attribute數量相同,且每一對attribute有相同或相容的domain.
>
### Unsafe Query
query 出來的結果不可預期,譬如可能是無限集合
> 可能導致不可預期結果的查詢,如:無限tuple.
# 第二大題 Expression的結果是否相同
## r1\*r2≡r2\*r1
**r1**
| id | 姓名 | 地址 |
| -- | --- | --- |
| 3 | 張三 | 台北 |
| 4 | 李四 | 台中 |
| 5 | 王五 | 台南 |
**r2**
| 品名 | 數量 | c_id |
| --- | --- | ---- |
| 香蕉 | 15 | 5 |
| 西瓜 | 14 | 4 |
| 芭樂 | 25 | 5 |
| 蓮霧 | 1 | 99 |
**r1\*r2**
| id | 姓名 | 地址 | 品名 | 數量 |
| -- | --- | --- | ---- | --- |
| 4 | 李四 | 台中 | 西瓜 | 14 |
| 5 | 王五 | 台南 | 香蕉 | 15 |
| 5 | 王五 | 台南 | 芭樂 | 25 |
**r2\*r1**
| 品名 | 數量 | c_id | 姓名 | 地址 |
| --- | --- | ---- | ---- | --- |
| 香蕉 | 15 | 5 | 王五 | 台南 |
| 西瓜 | 14 | 4 | 李四 | 台中 |
| 芭樂 | 25 | 5 | 王五 | 台南 |
**結論:**
內容相同,但tuple順序、attribute順序、key attribute的名稱可能不同。
## Fcount(∏a,b(r1)-∏a,b(r2))≡Fcount ∏b,a(r1-r2)
I give up :)
# 第五大題
Consider the following schema:
movie (mid, title, year, rating)
actor (aid, name, gender, age)
cast (mid, ald, leading)
The key fields are underlined. The cast relation indicates that an actor with id aid performs in movie with id mid. In the cast relation, the leading field is 'Y' for actors who play leading roles in a movie, and 'N' otherwise. If the query cannot be formulated, briefly explain why.
b
Write the query "For each year, retrieve the number of movies made that year and the total number of male actors who perform in these movie" in relational algebra. You may use grouping and/or aggregate functions for the query, if necessary.
Ans
from chatGPT 所以有問題不要找我
1. R1 ← movie ⨝ cast
2. R2 ← R1 ⨝ actor
3. R3 ← π year, mid, aid, leading (R2)
4. R4 ← σ gender='M' (R3)
5. R5 ← γ year; count(mid) (R4)