Converting ER to DBMS Tables
Problem: This entity relationship does not have 1-1 direct mapping. So we can’t convert it directly
그래서 Relational Data model
이 등장하게 된 것이다.
A Relation
주의할 점은 Relationship와 relation은 구분해야한다는 것이다.
- Relationship: Entity relationship model에서 나온 concept
- relation: Entity / DBMS table
- Attributes name: DBMS Column
Characteristics of Relations
- Each value in a tuple must be from the domain of the attribute
Foreign Key
A relation uses another relation’s primary key
as an attribute
Note that foreign keys can have duplicated values.
Formal Definitions
- The Schema: description of a Relation
- R is the name of the relation
- A1, A2, …, An are the attributes of the relation
- Example : STUDENT(Name, SSN, Home_phone, …, GPA)
- tuple:
ordered
set of values of attributes- Example: <”Benjamin Bayer”, “305-61-2435”, “373-1616”, 2918 Bluebonnet Lane, …>
- r(R)
- Table / set of tuples/rows
- r(R) = {t1, t2, …, tn} where each ti is an n-tuple
- Here, ti = <v1, v2, …, vn> where each vj element of dom(Aj)
- A specific state/value/population of relation R
Terminology Summary
Relational Integrity Constraints
Basic operations for changing the database
- INSERT : a new tuple
- DELETE : an existing tuple
- MODIFY : an existing tuple
해당 operation에 의해서 database의 state가 변화가 된다는 점을 기억해야 한다.
예를 들어 employee의 address가 달라지면 supervisor가 달라지게 된다.
Basic Idea : Constraints are conditions that must hold on all valid states (after every INSERT, UPDATE, DELETE)
- 3 types of constraints
Entity integrity
constraints
Referential integrity (Foreign Key)
constraints
Key Constraints
If a relation has several candidate keys, one is chosen arbitrarily as a primary key
→ The primary-key value is used to uniquely identify
each tuple in a relation
예를 들어 Texas ABC-739를 또 추가하려는 query를 날린 경우 해당 query를 reject하는 것이다.
- General rule : Choose as primary key the
smallest
of the candidate keys (in terms of size)
Entity Integrity Constraints
The primary key attributes can’t be null
- Primary key values are used to identify the individual tuples
- If PK has several attributes (i.e. composite PK), null is not allowed in any of these attributes
Referential Integrity Constraints
Referential Integrity/Foreign key constraints: the value in the FK column can be either
- a value of an existing PK value from another relation
- a
null
→ 아직 대응되지 않은 것으로 이해하면 된다.
Other types of Constraints
- Constraints can be defined using SQL-99
- CREATE TABLE statement in SQL allows
→ Defining keys/candidate-keys, restrict NULL values, FKs, etc
→ varchar : 크기를 제한시켜두지 않은 것
INSERT on Relations
INSERT can violate:
- Domain constraint : outside the domain
- Key constraint : if value already exists
- Referential integrity : Foreign key not present
- Entity integrity : Primary key value is null
DELETE on Relations
DELETE may violate referential integrity on FK : when the PK value is deleted from the original table : there are 3 Solutions:
- RESTRICT: reject the deletion
- CASCADE: propagate into the foreign keys
- SET NULL: set the foreign key keys value to NULL
UPDATE on Relations
UPDATE may violate the following Constraints:
- Primary key : Similar to DELETE + INSERT
- Foreign key : Similar to DELETE
- An ordinary attribute (neither PK nor FK):
- can violate domain or NULL value constraints
How the Data is Stored in a Table?