52
ฐฐฐฐฐฐฐฐฐฐฐฐฐฐฐฐฐฐฐฐฐ ฐฐฐฐฐฐฐฐฐฐฐฐฐฐฐฐฐฐฐ ฐฐฐฐฐฐฐฐ ฐฐฐฐฐ 3 ฐ. ฐฐ. ฐฐฐฐ ฐฐฐฐ ฐฐฐฐ CS (204)321 รรรรรรรรรรรรร 1 (Database System I)

ฐานข้อมูลเชิงสัมพันธ์ และตัวแบบข้อมูลเชิงสัมพันธ์

  • Upload
    nash

  • View
    45

  • Download
    0

Embed Size (px)

DESCRIPTION

บทที่ 3. ฐานข้อมูลเชิงสัมพันธ์ และตัวแบบข้อมูลเชิงสัมพันธ์. อ. ดร. ชุรี เตชะวุฒิ. CS (204)321 ระบบฐานข้อมูล 1 (Database System I). Outlines. 1) ฐานข้อมูลเชิงสัมพันธ์ (Relational Database) 2) ตัวแบบข้อมูลเชิงสัมพันธ์ (Relational Data Model) - PowerPoint PPT Presentation

Citation preview

Page 1: ฐานข้อมูลเชิงสัมพันธ์ และตัวแบบข้อมูลเชิงสัมพันธ์

ฐานข้�อมู�ลเชิ�งสั�มูพั�นธ์� และตั�วแบบข้�อมู�ลเชิ�งสั�มูพั�นธ์�

บทท�� 3

อ. ดร. ชิ�ร� เตัชิะว�ฒิ�

CS (204)321 ระบบฐานข้�อมู�ล 1 (Database System I)

Page 2: ฐานข้อมูลเชิงสัมพันธ์ และตัวแบบข้อมูลเชิงสัมพันธ์

Outlines

1) ฐานข้�อมู�ลเชิ�งสั�มูพั�นธ์� (Relational Database)

2) ตั�วแบบข้�อมู�ลเชิ�งสั�มูพั�นธ์� (Relational Data Model)

3) Relational Database Constraints and Relational Database Schemas

4) Update Operations on Relations

5) Relational Algebra

Page 3: ฐานข้อมูลเชิงสัมพันธ์ และตัวแบบข้อมูลเชิงสัมพันธ์

Relational Database

Definition (by C.J. Date)

“ A relational database is a database that is perceived by its users as a collection of tables. It is not just a database in which the data is physically stored as tables. ”

Note: Relation is a mathematical term for a table.

Page 4: ฐานข้อมูลเชิงสัมพันธ์ และตัวแบบข้อมูลเชิงสัมพันธ์

Relational Data Model

ตั�วแบบข้�อมู�ลหร�อตั�วแบบฐานข้�อมู�ลเชิ�งสั�มูพั�นธ์� เป็�นตั�วแบบเชิ�งตัรรกะที่ !แสัดงโครงสัร�างข้�อมู�ลและความูสั�มูพั�นธ์�ข้องข้�อมู�ลแบบนามูธ์รรมู โดยมูองป็&ญหาในระด�บแนวค�ดที่ !ไมู*เจาะล,กลงไป็ถึ,งรายละเอ ยดในระด�บกายภาพั

ป็ระกอบด�วย สั*วนข้องโครงสัร�าง (Structural part) สั*วนข้อง

การจ�ดด/าเน�นการ (Manipulative part) และสั*วนข้องกฎความูคงสัภาพั (Set of integrity constraints)

ตั�วแบบข้�อมู�ลเชิ�งสั�มูพั�นธ์�มู หลายป็ระเภที่ แตั*ที่ !น�ยมูใชิ�ป็&จจ1บ�นค�อตั�วแบบฐานข้�อมู�ลเชิ�งสั�มูพั�นธ์�ที่ !มู โครงสัร�างข้�อมู�ลแบบตัาราง (Table) ที่ !เข้�าใจง*าย

Concepts

Page 5: ฐานข้อมูลเชิงสัมพันธ์ และตัวแบบข้อมูลเชิงสัมพันธ์

Relational Data Model

The relational model of data is based on the concept of a Relation. A relation is a mathematical concept based on the ideas of sets.

The strength of the relational approach to data management comes from the formal foundation provided by the theory of relations.

We review the essentials of the relational approach in this chapter.

Concepts

Page 6: ฐานข้อมูลเชิงสัมพันธ์ และตัวแบบข้อมูลเชิงสัมพันธ์

Relational Data Model

RELATION: A table of values

What is “Relation” ?

Each column typically is called by its column name or column header or attribute name.

Sometimes row-ids or sequential numbers are assigned to identify the rows in the table.

Each row has a value of an item or set of items that uniquely identifies that row in the table.

Each row represents a fact that corresponds to a real-world entity or relationship.

A relation may alternately be thought of as a set of columns.

A relation may be thought of as a set of rows.

Page 7: ฐานข้อมูลเชิงสัมพันธ์ และตัวแบบข้อมูลเชิงสัมพันธ์

Relational Data Model

Example of Relation

Student code Student name Major GPA

500510881 นฤมูล มูนตัร ภาพั

MATH 2.98

500510214 ป็ว ณ นาคสัมูบ�รณ�

BIO 2.75

500510654 lสัมูถึว�ล ป็ร ชิาชิาญ

CS 3.01

500510223 สัายใจ พั�นวล�ย

CS 2.50

STUDENT

Page 8: ฐานข้อมูลเชิงสัมพันธ์ และตัวแบบข้อมูลเชิงสัมพันธ์

Relational Data Model

A relation is a named, 2-dimentional table of data. Not all tables qualify as relation.

Relation properties

Every relation has a unique name.

Each column has a unique name.

No 2 rows are identical.

Ordering of rows is not significant.

Ordering of columns is not significant.

All attribute values are atomic. (There always exists precisely one value, never a set of values)

Column values are of the same kind.

Page 9: ฐานข้อมูลเชิงสัมพันธ์ และตัวแบบข้อมูลเชิงสัมพันธ์

Relational Data Model

For Example:

CUSTOMER (Cust-id, Cust-name, Address, Phone#)

Relation schema

R (A1, A2, .....An)

R is the name of relation.Degree of relation is the numbers of attributes of its relation schema.

Relation schema R is defined over attributes A1, A2, .....An

CUSTOMER is a relation defined over the four attributes Cust-id, Cust-name, Address, Phone#, each of which has a domain or a set of valid values.

Degree of relation is 4.

Page 10: ฐานข้อมูลเชิงสัมพันธ์ และตัวแบบข้อมูลเชิงสัมพันธ์

Relational Data Model

For Example:

Cust-id : The set of valid 6-digit numbers. Cust-name : The set of customer names. Address : The set of home address where customers live. Phone# : The set of 10-digit phone numbers valid in Thailand.

Domain

“ A domain D is a set of atomic values, all of the same type. ”

All values in a column come from the same domain.

Page 11: ฐานข้อมูลเชิงสัมพันธ์ และตัวแบบข้อมูลเชิงสัมพันธ์

Relational Data Model

A domain has a logical definition.e.g., “USA_phone_numbers” are the set of 10 digit phone numbers valid in the U.S.

A domain may have a data-type or a format defined for it. e.g., The USA_phone_numbers may have a format: (ddd)-ddd-dddd where each d is a decimal digit. e.g., Dates have various formats such as monthname, date, year or yyyy-mm-dd, or dd mm,yyyy etc.

An attribute designates the role played by the domain. e.g., The domain Date may be used to define attributes “Invoice-date” and “Payment-date”.

Domain (Continued)

Page 12: ฐานข้อมูลเชิงสัมพันธ์ และตัวแบบข้อมูลเชิงสัมพันธ์

Relational Data Model

Each attribute in the model should be assigned domain information which includes:

- Data type- Length- Data format (e.g., Date format is dd/mm/yy)- Range- Constraints (special restrictions on allowable values)- Null support- Default value (if any)

Domain (Continued)

Tillmann-95: Domain includes - Data type (e.g., tinyint represents numbers from -128 to 127.) - Range (e.g., GPA rages from 0.00 to 4.00) - Acceptable value (e.g., Gender has two values, male and female.)

Page 13: ฐานข้อมูลเชิงสัมพันธ์ และตัวแบบข้อมูลเชิงสัมพันธ์

Relational Data Model

A relation instance, r , of the relation schema

Relation instance

R (A1, A2, .....An),

denoted by r(R) is a mathematical relation of degree n on the domains dom(A1), dom(A2),…, dom(An), which is the subset of the Cartesian Product of the domains that define R.

))(...)()(()( 21 nAdomAdomAdomRr

R is also called the intension of a relation.

r is also called the extension of a relation.

Page 14: ฐานข้อมูลเชิงสัมพันธ์ และตัวแบบข้อมูลเชิงสัมพันธ์

Relational Data Model

Cartesian product

))(...)()(()( 21 nAdomAdomAdomRr

(2,5)}(2,4),(2,3),(1,5),(1,4),{(1,3),

dom(A2) dom(A1) r(R)

{3,4,5}dom(A2) and {1,2} dom(A1) Define

Page 15: ฐานข้อมูลเชิงสัมพันธ์ และตัวแบบข้อมูลเชิงสัมพันธ์

Relational Data Model

A tuple is an ordered set of values.

Tuple

Columns in a table are also called attributes of the relation.

A relation may be regarded as a set of tuples (rows).

t = <632895, "John Smith", "101 Main St. Atlanta, GA 30332", "(404) 894-2000"> is a tuple belonging to the CUSTOMER relation.

Each row in the CUSTOMER table may be referred to as a tuple in the table and would consist of four values.

Each value is derived from an appropriate domain.

nvvvtRr ,...,,)( 21

Page 16: ฐานข้อมูลเชิงสัมพันธ์ และตัวแบบข้อมูลเชิงสัมพันธ์

Relational Data Model

We refer to component values of a tuple t

by t[Ai] = vi (the value of attribute Ai for tuple t).

Similarly, t[Au, Av, ..., Aw] refers to the subtuple of t containing the values of attributes Au, Av, ..., Aw, respectively.

Tuple (Continued)

eg., t[name] = < "John Smith">

t[name, address] = < "John Smith” , "101 Main St. Atlanta, GA 30332">

Page 17: ฐานข้อมูลเชิงสัมพันธ์ และตัวแบบข้อมูลเชิงสัมพันธ์

Relational Data Model

การระบ1ว*า attribute มูาจาก relation ใด จะใชิ�สั�ญล�กษณ� dot

(.) ในร�ป็แบบ

เชิ*น CUSTOMER.name

CUSTOMER.address

Relation name . Attribute name

Page 18: ฐานข้อมูลเชิงสัมพันธ์ และตัวแบบข้อมูลเชิงสัมพันธ์

Relational Data Model

Let S1 = {0,1}

Let S2 = {a,b,c}

Let 21 SSR

Then for example: },1,,0,,0{)( cbaRr

is one possible ‘state’ or ‘population’ or ‘extension’ r of the relation R, defined over domains S1 and S2. It has three tuples.

Page 19: ฐานข้อมูลเชิงสัมพันธ์ และตัวแบบข้อมูลเชิงสัมพันธ์

Relational Data Model

Terminology

Formal Terms Informal Terms

Relation Table

Attribute/Domain Column

Tuple Row

Domain Values in a column or pool of legal values

Schema of a relation Table definition

Extension Populated table

Degree No. of columns

Cardinality No. of rows

Primary key Unique identifier

Page 20: ฐานข้อมูลเชิงสัมพันธ์ และตัวแบบข้อมูลเชิงสัมพันธ์

Relational Data Model

Example

Page 21: ฐานข้อมูลเชิงสัมพันธ์ และตัวแบบข้อมูลเชิงสัมพันธ์

Relational Data Model

Key fields

Keys are special fields that serve two main purposes:

- Primary keys are unique identifiers of the relation. Examples can use SSN as a primary key. This is how we can guarantee that all rows are unique.

- Foreign key is a column or columns whose values are the same as a primary key of another table.

Keys can be simple (a single field) or composite (more than one field)

Keys usually are used as indexes to speed up the response to user queries.

Page 22: ฐานข้อมูลเชิงสัมพันธ์ และตัวแบบข้อมูลเชิงสัมพันธ์

Relational Data Model

ประเภทข้องคี�ย์� Primary key หร%อ คี�ย์�หล�ก ค ย�ค�*แข้*งที่ !ถึ�กเล�อกมูาเพั�!อใชิ�

เจาะจงที่�พัเพั�ลข้องร เลชิ�น และค ย�ค�*แข้*งที่ !เหล�อจะเร ยกว*าคี�ย์�สั'ารอง (Alternate key)

เชิ*น ถึ�าเล�อก Student_code เป็�น ค ย�หล�ก แล�ว email จะเป็�น ค ย�สั/ารอง Student code Student name email

500510881 นฤมูล มูนตัร ภาพั

[email protected]

500510214 ป็ว ณ นาคสัมูบ�รณ�

[email protected]

500510654 lสัมูถึว�ล ป็ร ชิาชิาญ

[email protected]

500510223 สัายใจ พั�นวล�ย

[email protected]

Page 23: ฐานข้อมูลเชิงสัมพันธ์ และตัวแบบข้อมูลเชิงสัมพันธ์

Relational Data Model

ประเภทข้องคี�ย์� Primary key หร%อ คี�ย์�หล�กโดย ค ย�หล�ก จะแสัดงในเค�าร*างร เลชิ�นด�วย การข้ ดเสั�นใตั� เชิ*นSTUDENT(Student_code, Student_name, email)

ค1ณสัมูบ�ตั�ข้องค ย�หล�กค�อ1. Uniqueness มู ความูเป็�นหน,!งเด ยว ค�อ ค*าข้องค ย�หล�ก

ตั�องไมู*ซ้ำ/6าก�นเลย2. Minimality ค�อป็ระกอบด�วยจ/านวนแอที่ที่ร�บ�วตั�น�อย

ที่ !สั1ดที่ !สัามูารถึเจาะจงที่�พัเพั�ลหน,!งในร เลชิ�นได�

Page 24: ฐานข้อมูลเชิงสัมพันธ์ และตัวแบบข้อมูลเชิงสัมพันธ์

Relational Data Model

ประเภทข้องคี�ย์� Composite key หร%อ คี�ย์�ประกอบ ค ย�ที่ !ป็ระกอบด�วยแอที่

ที่ร�บ�วตั�มูากกว*า 1 ตั�ว เมู�!อน/าแอที่ที่ร�บ�วตั�ที่�6งหมูดมูาผสัมูก�น ที่/าให�ได�ค*าที่ !ไมู*ซ้ำ/6าก�นเลย เชิ*น

GRADE(Student_code, course, grade)

Student code Course Grade

500510881 204321 C

500510881 204111 B

500510881 204112 C

500510214 204361 A

500510214 204111 C

500510214 204112 C

GRADE

Page 25: ฐานข้อมูลเชิงสัมพันธ์ และตัวแบบข้อมูลเชิงสัมพันธ์

Relational Data Model

ประเภทข้องคี�ย์� Superkey หร%อ ซู�เปอร�คี�ย์� กล1*มูข้องแอที่ที่ร�บ�วตั�ข้องร เล

ชิ�นที่ !สัามูารถึเจาะจง (identify) ความูเป็�นหน,!งเด ยวข้องที่�พัเพั�ลที่ !แตักตั*างจากที่�พัเพั�ลอ�!นได�

ที่1กร เลชิ�!นจะมู ซ้ำ�เป็อร�ค ย� อย*างน�อย 1 ตั�วเสัมูอแอที่ที่ร�บ�วตั�ที่1กตั�วข้องร เลชิ�นจะเป็�นซ้ำ�เป็อร�ค ย�โดยป็ร�ยาย

Page 26: ฐานข้อมูลเชิงสัมพันธ์ และตัวแบบข้อมูลเชิงสัมพันธ์

Relational Data Model

ประเภทข้องคี�ย์� Superkey หร%อ ซู�เปอร�คี�ย์� จากตัาราง STUDENT ซ้ำ�เป็อร�

ค ย�อาจเป็�นได� ด�งน 6{Student_code, Student name, Major, GPA}

{Student_code, Student name}

{Student_code, Student name, Major}

Student code Student name Major GPA

500510881 นฤมูล มูนตัร ภาพั

MATH 2.98

500510214 ป็ว ณ นาคสัมูบ�รณ�

BIO 2.75

500510654 lสัมูถึว�ล ป็ร ชิาชิาญ

CS 3.01

500510223 สัายใจ พั�นวล�ย

CS 2.50

STUDENT

Page 27: ฐานข้อมูลเชิงสัมพันธ์ และตัวแบบข้อมูลเชิงสัมพันธ์

Relational Data Model

ประเภทข้องคี�ย์� Key หร%อ คี�ย์� ค�อซ้ำ�เป็อร�ค ย�ที่ !เล8กที่ !สั1ด (Minimal superkey)

ข้องร เลชิ�น ซ้ำ,!งไมู*มู เซ้ำ8ตัย*อยที่ !ตั�วมู�นเองเป็�นซ้ำ�เป็อร�ค ย� น�!นค�อ ค ย�เป็�นซ้ำ�เป็อร�ค ย�ที่ !ไมู*สัามูารถึตั�ดแอที่ที่ร�บ�วตั�ใดได�อ กโดยย�งคงค1ณสัมูบ�ตั�ข้องที่�พัเพั�ลไว�เหมู�อนเด�มู

บางร เลชิ�นอาจมู ค ย�ได�มูากกว*า 1 ตั�ว เชิ*นตัาราง STUDENT มู ค ย� 2 ตั�ว ค�อ student_code และ email

เราสัามูารถึใชิ�ตั�วใดตั�วหน,!งเป็�นค ย�ที่ !เจาะจงที่�พัเพั�ลได� ซ้ำ,!งค ย�แตั*ละตั�วน 6เร ยกว*า คี�ย์�คี�)แข้)ง (Candidate key)

Page 28: ฐานข้อมูลเชิงสัมพันธ์ และตัวแบบข้อมูลเชิงสัมพันธ์

Relational Constraints

There are three main types of constraints:

“ Constraints are conditions that must hold on all valid relation instances. ” (Elmasri&Navathe, 2000)

Key constraints

Entity integrity constraints

Referential integrity constraints

What are relational constraints?

“ Restrictions on data that can be specified on a relational database schema. ” (Date, 2000)

Domain constraints

Page 29: ฐานข้อมูลเชิงสัมพันธ์ และตัวแบบข้อมูลเชิงสัมพันธ์

Relational Constraints

Domain constraints

The value of each attribute A must be an atomic value from the domain dom(A).

The data types associated with domains typically includes: - Numeric: integer (short integer,long) or real number (float, double) - Character: fixed-length string or variable-length string - Date and time: e.g., dd/mm/yy hh:mm - Logical: e.g., true or false, yes or no - Timestamp - Currency

Page 30: ฐานข้อมูลเชิงสัมพันธ์ และตัวแบบข้อมูลเชิงสัมพันธ์

Relational Constraints

Entity integrity constraints

Relational Database Schema: A set S of relation schemas that belong to the same database. S is the name of the database. S = {R1, R2, ..., Rn}

Entity Integrity: The primary key attributes PK of each relation schema R in S cannot have null values in any tuple of r(R). This is because primary key values are used to identify the individual tuples. t[PK] null for any tuple t in r(R)

Note: Other attributes of R may be similarly constrained to disallow null values, even though they are not members of the primary key.

Page 31: ฐานข้อมูลเชิงสัมพันธ์ และตัวแบบข้อมูลเชิงสัมพันธ์

Relational Constraints

Entity integrity constraints (Continued)

A Null is created by making no entry at all, so a null denotes the absence of a value.

- The value does not exist.

- The value exists, but it is not known.

- The value is unknown, or it is not applicable.

A null can have any of the following meanings:

Page 32: ฐานข้อมูลเชิงสัมพันธ์ และตัวแบบข้อมูลเชิงสัมพันธ์

Relational Constraints

Referential integrity constraints

A constraint involving two relations (the previous constraints involve a single relation).

Used to specify a relationship among tuples in two relations: the referencing relation and the referenced relation.

Tuples in the referencing relation R1 have attributes FK (called foreign key attributes) that reference the primary key attributes PK of the referenced relation R2. A tuple t1 in R1 is said to reference a tuple t2 in R2 if t1[FK] = t2[PK].

A referential integrity constraint can be displayed in a relational database schema as a directed arc from R1. FK to R2.

Page 33: ฐานข้อมูลเชิงสัมพันธ์ และตัวแบบข้อมูลเชิงสัมพันธ์

Relational Constraints

Referential integrity constraints displayed on the COMPANY relational database schema

Page 34: ฐานข้อมูลเชิงสัมพันธ์ และตัวแบบข้อมูลเชิงสัมพันธ์

Relational Constraints

Domain constraints

Entity integrity constraints (rows)

Referential integrity constraints(between tables)

Summary of relational constraints

Page 35: ฐานข้อมูลเชิงสัมพันธ์ และตัวแบบข้อมูลเชิงสัมพันธ์

Relational Constraints

กฎความูคงสัภาพัจะถึ�กใชิ�ในการน�ยามูฐานข้�อมู�ลด�วยภาษาน�ยามูข้�อมู�ล (Data definition language) ซ้ำ,!งจะที่/าให�ระบบจ�ดการฐานข้�อมู�ลบ�งค�บใชิ�กลไกโดยอ�ตัโนมู�ตั� เชิ*น ภาษาน�ยามูข้�อมู�ล ข้อง MySQL

create table STUDENT{

student_code char(9) NOT NULL,

student _name varchar(30) NOT NULL,

major char(4) Default ‘CS’,

gpa float CHECK gpa>=0.00 AND gpa<=4.00,

Advisor_code char(3) REFERENCES ADVISOR (adv_code),

PRIMARY KEY (student_code)}

Domain integrity

Referential integrity

Entity integrity

Page 36: ฐานข้อมูลเชิงสัมพันธ์ และตัวแบบข้อมูลเชิงสัมพันธ์

Relational Constraints

กฎเกณฑ์�อ%�นๆได�แก) กฎเกณฑ์�ที่�!วไป็เพั�!อตัรวจสัอบความูถึ�กตั�องข้องข้�อมู�ล (Static

constraint or Semantic integrity constraint) เชิ*น เง�นเด�อนข้องพัน�กงาน ตั�องน�อยกว*าเง�นเด�อนข้องพัน�กงานที่ !เป็�น Supervisor

กฎเกณฑ์�ที่ !เก !ยวข้�องก�บการป็ร�บป็ร1งค*าข้องข้�อมู�ล (Dynamic

constraint or Transaction constraints) ซ้ำ,!งอาจใชิ� Trigger เป็�นตั�วด/าเน�นการ เชิ*น การป็ร�บค*าข้องเง�นเด�อนพัน�กงานตั�องป็ร�บเพั�!มูข้,6นเที่*าน�6น ไมู*สัามูารถึลดลงจากป็&จจ1บ�นได�

Page 37: ฐานข้อมูลเชิงสัมพันธ์ และตัวแบบข้อมูลเชิงสัมพันธ์

Update Operations on Relations

UPDATE operations consist of:

INSERT a tuple

DELETE a tuple

MODIFY a tuple

Integrity constraints should not be violated by the update operations. Several update operations may have to be grouped together.

Updates may propagate to cause other updates automatically. This may be necessary to maintain integrity constraints.

Page 38: ฐานข้อมูลเชิงสัมพันธ์ และตัวแบบข้อมูลเชิงสัมพันธ์

Update Operations on Relations

In case of integrity violation, several actions can be taken:

Cancel the operation that causes the violation (REJECT option)

Perform the operation but inform the user of the violation

Trigger additional updates so the violation is corrected (CASCADE option, SET NULL option)

Execute a user-specified error-correction routine

e

Page 39: ฐานข้อมูลเชิงสัมพันธ์ และตัวแบบข้อมูลเชิงสัมพันธ์

Update Operations on Relations

ในระหว*างการน�ยามูข้�อมู�ล ผ��ใชิ�หร�อผ��บร�หารฐานข้�อมู�ลจะเป็�นผ��ก/าหนดว*า ตั�องการให�ระบบฐานข้�อมู�ลแก�ไข้สัถึานการณ�ในล�กษณะใด จ,งควรที่/าความูเข้�าใจเพั�!อตัอบค/าถึามู 3 ข้�อตั*อไป็น 6

1. ค*าข้องค ย�นอกเป็�นค*าว*างได�หร�อไมู*

2. อน1ญาตัให�มู การลบที่�พัเพั�ลที่ !ถึ�กอ�างอ�งโดยค ย�นอกหร�อไมู* และถึ�ายอมูให�ลบแล�วจะที่/าอย*างไรให�ข้�อมู�ลย�งคงมู ความูคงสัภาพัด�งที่ !น�ยามูไว�

3. อน1ญาตัให�มู การแก�ไข้ค*าค ย�หล�กข้องที่�พัเพั�ลที่ !ถึ�กอ�างอ�งโดยค ย�นอกหร�อไมู* และถึ�ายอมูให�แก�ไข้แล�วจะที่/าอย*างไรให�ค ย�นอกย�งคงอ�างอ�งถึ,งค*าใหมู*ข้องค ย�หล�กได� e

Page 40: ฐานข้อมูลเชิงสัมพันธ์ และตัวแบบข้อมูลเชิงสัมพันธ์

Update Operations on Relations

Insert operation

Insert <‘Cecilia’, ‘F’, ‘Kolonsky’, null, ‘1960-04-05’, ‘6357 Windy Lane, Katy, TX’, F, 28000, null, 4> into EMPLOYEE.

This insertion violates the entity integrity constraint (null for the primary key SSN), so it is rejected.

กรณ ที่ ! 1: การเพั�!มูที่�พัเพั�ลโดยไมู*ระบ1ค*าข้องแอที่ที่ร�บ�วตั�ที่ !เป็�นค ย�หล�ก เชิ*น

Page 41: ฐานข้อมูลเชิงสัมพันธ์ และตัวแบบข้อมูลเชิงสัมพันธ์

Update Operations on Relations

Insert operation

Insert <‘Cecilia’, ‘F’, ‘Kolonsky’, 123456789, ‘1960-04-05’, ‘6357 Windy Lane, Katy, TX’, F, 28000, null, 4> into EMPLOYEE.

This insertion violates the entity integrity constraint (value repetition of the primary key SSN), so it is rejected.

กรณ ที่ ! 2: การเพั�!มูที่�พัเพั�ลโดยก/าหนดค*าข้องแอที่ที่ร�บ�วตั�ที่ !เป็�นค ย�หล�กซ้ำ/6าก�บค*าที่ !มู อย�*แล�ว เชิ*น

Page 42: ฐานข้อมูลเชิงสัมพันธ์ และตัวแบบข้อมูลเชิงสัมพันธ์

Update Operations on Relations

Insert operation

Insert <‘Cecilia’, ‘F’, ‘Kolonsky’, 677678989, ‘1960-04-05’, ‘6357 Windy Lane, Katy, TX’, F, 28000, null, 8> into EMPLOYEE.

This insertion violates the referential integrity constraint (DNUMBER in DEPARTMENT does not contain 8), so it is rejected.

กรณ ที่ ! 3: การเพั�!มูที่�พัเพั�ลที่ !ค*าค ย�นอกไมู*สัามูารถึอ�างอ�งถึ,งค ย�หล�กได� เชิ*น

Page 43: ฐานข้อมูลเชิงสัมพันธ์ และตัวแบบข้อมูลเชิงสัมพันธ์

Update Operations on Relations

Insert operation

กรณ ที่ ! 4: การเพั�!มูที่�พัเพั�ลที่ !สัอดคล�องก�บกฎความูคงสัภาพั เชิ*น

Insert <‘Cecilia’, ‘F’, ‘Kolonsky’, ‘677678989’, ‘1960-04-05’, ‘6357 Windy Lane, Katy, TX’, F, 28000, null, 4> into EMPLOYEE.

This insertion satisfies all constraints, so it is acceptable.

Page 44: ฐานข้อมูลเชิงสัมพันธ์ และตัวแบบข้อมูลเชิงสัมพันธ์

Update Operations on Relations

Delete operation

Delete the WORKS_ON tuple with ESSN = ‘999887777’ and PNO = 10.

This deletion is acceptable.

กรณ ที่ ! 1: การลบที่�พัเพั�ลที่ !สัอดคล�องก�บกฎความูคงสัภาพั เชิ*น

Page 45: ฐานข้อมูลเชิงสัมพันธ์ และตัวแบบข้อมูลเชิงสัมพันธ์

Update Operations on Relations

Delete operation

Delete the EMPLOYEE tuple with SSN = ‘999887777’

This deletion is not acceptable, because tuples in WORKS_ON refer to this tuple. Hence, if the tuple is deleted, referential integrity violations will result.

กรณ ที่ ! 2: ที่�พัเพั�ลที่ !ตั�องการลบย�งถึ�กอ�างอ�งจากค ย�นอก เชิ*น

Page 46: ฐานข้อมูลเชิงสัมพันธ์ และตัวแบบข้อมูลเชิงสัมพันธ์

Update Operations on Relations

Delete operation

เมู�!อการลบที่�พัเพั�ลที่/าให�เก�ดการละเมู�ดกฎความูคงสัภาพัข้องการอ�างอ�ง ระบบจ�ดการฐานข้�อมู�ลจะเล�อกป็ฏิ�บ�ตั�จากที่างเล�อก 4 ที่าง ด�งน 61. ปฏิ�เสัธ์ (Reject) คี'าสั��งถึ�ามู ค ย�นอกอ�างอ�งถึ,งที่�พัเพั�ลน�6น หร�อยอมูให�ลบที่�พัเพั�ลถึ�าไมู*มู การอ�างอ�งจากค ย�นอกใด ซ้ำ,!งเป็�นการลบแบบมู เง�!อนไข้ เร ยกว*า การลบแบบเข้�มูงวด (Restrict delete)

2. ลบที่1กๆที่�พัเพั�ลที่ !ค ย�นอกอ�างอ�งถึ,ง โดยจะลบก�นเป็�นที่อดๆ เร ยกว*า การลบแบบตั)อเร�ย์ง (Cascade delete)

ตั�วอย*าง การลบในกรณ ที่ ! 2 เมู�!อลบในตัาราง EMPLOYEE แล�วจะลบแบบตั*อเร ยงในตัาราง WORKS_ON ที่ !มู การอ�างถึ,ง SSN = ‘999887777’

Page 47: ฐานข้อมูลเชิงสัมพันธ์ และตัวแบบข้อมูลเชิงสัมพันธ์

Update Operations on Relations

Delete operation

3. เป็ล !ยนค*าแอที่ที่ร�บ�วตั�ที่ !ที่/าให�เก�ดการละเมู�ดกฎ เชิ*น การท'า Nullify หร%อ Set to null ค�อการเป็ล !ยนค*าข้องแอที่ที่ร�บ�วตั�ข้�อมู�ลน�6นให�เป็�น null แล�วจ,งลบค*าข้องแอที่ที่ร�บ�วตั�น�6นตัามูตั�องการ

ตั�วอย*าง การลบค*าบางแผนกออกจากตัาราง DEPARTMENT Delete from DEPARTMENT where DNUMBER=‘5’

การลบจะถึ�กป็ฏิ�เสัธ์เพัราะมู ที่�พัเพั�ลข้อง EMPLOYEE อ�างอ�งที่�พัเพั�ลที่ ! DNO=‘5’

ในกรณ น 6 จะที่/าให�ค*า DNO ในตัาราง EMPLOYEE เป็�น null ก*อน แล�วจ,งที่/าการลบ ค*าบางแผนกออกจากตัาราง DEPARTMENT ได�

Page 48: ฐานข้อมูลเชิงสัมพันธ์ และตัวแบบข้อมูลเชิงสัมพันธ์

Update Operations on Relations

Delete operation

4. เป็ล !ยนค*าแอที่ที่ร�บ�วตั�ที่ !ที่/าให�เก�ดการละเมู�ดกฎ เป0นคี)าท��ก'าหนดให�คี)าหน2�ง (Default value) มู�กใชิ�ในกรณ ที่ !ไมู*อน1ญาตัให�ค*าข้องค ย�นอกเป็�นค*าว*างหร�อกรณ ที่ !ค ย�นอกเป็�นสั*วนหน,!งข้องค ย�หล�ก

หมูายเหตั1 ที่1กคร�6งที่ !มู การบ�งค�บใชิ�กฎความูคงสัภาพัข้องการอ�างอ�ง ผ��ใชิ�ตั�องระบ1ในข้ณะที่ !น�ยามูฐานข้�อมู�ลว*าตั�องการใชิ�ที่างเล�อกใดเมู�!อมู การลบข้�อมู�ล

Page 49: ฐานข้อมูลเชิงสัมพันธ์ และตัวแบบข้อมูลเชิงสัมพันธ์

Update Operations on Relations

Modify/Update operation

Update the SALARY of the EMPLOYEE tuple with SSN = ‘999887777’ to 28000.

This update is acceptable.

กรณ ที่ ! 1:การแก�ไข้แอที่ร�บ�วตั�ที่ !ไมู*ได�เป็�นค ย�หล�กหร�อค ย�นอก จ,งไมู*ละเมู�ดกฎความูคงสัภาพัข้องข้�อมู�ล เชิ*น

Page 50: ฐานข้อมูลเชิงสัมพันธ์ และตัวแบบข้อมูลเชิงสัมพันธ์

Update Operations on Relations

Modify/Update operation

Update the DNO of the EMPLOYEE tuple with SSN = ‘999887777’ to 8.

กรณ ที่ ! 2: การแก�ไข้แอที่ร�บ�วตั�ที่ !เป็�นค ย�นอกและมู การอ�างอ�งถึ,งค ย�หล�ก เชิ*น

This update is unacceptable. No primary key with value 8 in DEPARTMENT.

ค/าสั�!งน 6ถึ�กป็ฏิ�เสัธ์เพัราะค*าใหมู*น 6ไมู*สัามูารถึอ�างอ�งถึ,งค ย�หล�กที่ !มู อย�*แล�วได�

Page 51: ฐานข้อมูลเชิงสัมพันธ์ และตัวแบบข้อมูลเชิงสัมพันธ์

Update Operations on Relations

Modify/Update operation

กรณ ที่ ! 2: ค*าที่ !ตั�องการแก�ไข้ย�งถึ�กอ�างอ�งจากค ย�นอก เชิ*น Update the SSN of the EMPLOYEE tuple with SSN = ‘999887777’

to ‘987654321’.

This update is unacceptable, because it violates primary key and referential integrity constraints.

Page 52: ฐานข้อมูลเชิงสัมพันธ์ และตัวแบบข้อมูลเชิงสัมพันธ์

Update Operations on Relations

Modify/Update operation

เมู�!อการแก�ไข้ข้�อมู�ลที่/าให�เก�ดการละเมู�ดกฎความูคงสัภาพัข้องการอ�างอ�ง ระบบจ�ดการฐานข้�อมู�ลจะเล�อกป็ฏิ�บ�ตั�จากที่างเล�อก 2 ที่าง ด�งน 61. ยอมูให�มู การแก�ไข้แบบมู เง�!อนไข้ ค�อ อน1ญาตัให�มู การแก�ไข้ค ย�หล�กเมู�!อไมู*ป็รากฏิค ย�นอกใดที่ !อ�างอ�งค ย�หล�กน 6 เร ยกว*า การแก�ไข้แบบเข้�มูงวด (Restricted update)

2. ยอมูให�แก�ไข้ค*าค ย�หล�กที่ !ถึ�กอ�างอ�ง โดยจะไป็แก�ไข้ค*าค ย�นอกที่ !อ�างถึ,งที่1กตั�วให�มู ค*าเที่*าก�บค*าใหมู*ข้องค ย�หล�กน�6น เร ยกว*า การแก�ไข้แบบตั)อเร�ย์ง (Cascade update)

และจะแก�ไข้ไป็ย�งค*าข้องค ย�นอกในที่1กๆร เลชิ�!นที่ !สั�มูพั�นธ์�ก�นให�มู ค*าตัรงก�น ตั�วอย*าง

Update the DNUMBER of the DEPARTMENT from ‘1’ to ‘2’.

All tuple of the EMPLOYEE with DNO=‘1’ will be updated to ‘2’.