44
Chapter 5 The Relational Data Model and Relational Database Constraints م ي ح ر ل ا ن م ح ر ل ه ا ل ل م ا س بLecture (7) 1

Chapter 5 The Relational Data Model and Relational Database Constraints

Embed Size (px)

DESCRIPTION

بسم الله الرحمن الرحيم Lecture ( 7 ). Chapter 5 The Relational Data Model and Relational Database Constraints. An introduction of relational model. قدمه الباحث Ted Codd في 1970 ( IBM Research ). يعتبر من أشهر أنواع الـ Implementation models ويستخدم في كثير من الـ DBMS الحالية . - PowerPoint PPT Presentation

Citation preview

Page 1: Chapter 5 The Relational Data Model and Relational Database Constraints

Chapter 5The Relational Data Model and

Relational Database Constraints

الرحيم الرحمن الله بسمLecture (7)

1

Page 2: Chapter 5 The Relational Data Model and Relational Database Constraints

الباحث (.IBM Research )1970في Ted Codd قدمه الـ أنواع أشهر من Implementation modelsيعتبر

الـ من كثير في .الحالية DBMSويستخدم( الرياضي وأساسه مفاهيمه وبساطة بسهولة Mathematicalيمتاز

Foundation ) الرياضية العالقة مفهوم أساس علي Mathematical يعتمد

Relation)) المجموعات ويخضع نظرية (.(Set theoryلقوانين

2

Page 3: Chapter 5 The Relational Data Model and Relational Database Constraints

البيانات تعرف علي قاعدة العالئقي النموذج من في مجموعة أنهاالعالقات

(set of relations . )القيم العالقة من بجدول ما (Table of values)تمثل لحد تشبه وهي

. المسطح السجالت ملف العالقة الصفوف ) وتعرف من مجموعة انها . set of rows)علي( صف القيم( )rowاي من مجموعة تمثل( values of setيمثل والتي

.حقيقة بـ تسمي أوالعالقة الجدول الـعالقة (columns)أعمدة صفات وتمثل

(attributes). يطلق العالئقي النموذج في .(tuples)عليها الصفوف االـعمدة وأسماء العالقة تفسير (name of attributes)اسم علي تساعد

العالقة صفوف في القيم

3

Page 4: Chapter 5 The Relational Data Model and Relational Database Constraints

الكائن نوع عن تتحدث عن Studentالعالقة تتحدث العالقة صفوف من صف أي في القيم وأن(particular entity ). ) خاص ) طالب

4

Page 5: Chapter 5 The Relational Data Model and Relational Database Constraints

مجال لها العالقة في صفة .اي( القيم عناصر مجموعة هو أن( Atomic valuesالمجال يمكن التي

. المجال لهذا تنتمي التي الصفة تأخذها الـ تعتبر أن للصفات,…( .int,charالمعروفة )Data typesيمكن مجاالت

نسق، ،نوع اسم له ويكون المجال تعريف أيضا (format)يمكنللمجال المنطقي بالتعريف هذا logical definition of)ويسمي

domain). :مثال

للصفة العالقة StdNoالمجال Studentفي : المجال : Student Numberاسم النوع ،char(5) النسق ،(format):

dd-dd.

5

Page 6: Chapter 5 The Relational Data Model and Relational Database Constraints

العالقة كاألتي Rخارطة :R(A1: dom(A1), A2: dom(A2),., An توضحdom(An))

R(A1, A2,…....., An) او العالقة داخل ) R :اسم في العالقات كل أسماء عن يختلف

) الواحدة البيانات قاعدة العالقة .A1 ، A2،.....، An: صفات صفة .dom(Ai)مجال Aiولكل . : العالقة صفات عدد تعني العالقة الدرجة (R درجة ( .nمن

6

Page 7: Chapter 5 The Relational Data Model and Relational Database Constraints

: للعالقة المكونة الحالية الصفوف مجموعة تعني العالقة current set of)حالةtuples)) بـ لها عن r(R)ويرمز عبارة وهي العالقة خارطة اسم نفس ولها

الـ من .m-tuples (cardinality of R)مجموعةr(R)={t1,t2,….,tm}

صف كل علي tحيث .(n values)يحتويt=<v1,………,vn>vi( المجال في عنصر عن عبارة قيمة n ≥ i ≥1 حيث dom(Aiهي أو

خالية) (null.

vi is the value corresponding to attribute Ai t[Ai] is the ith value in the tuple t , which corresponds to the

attribute Ai .

7

Page 8: Chapter 5 The Relational Data Model and Relational Database Constraints

r (R ) الدرجة من رياضية عالقة عن عبارة للمجاالت nهيdom(A1)، dom(A2)،....، dom(An)

r(R) dom (A1) X dom (A2) X ....X dom(An) لمجاالت الكارتيزي الحاصل من جزئية مجموعة تمثل وهي

من تكوينها يمكن التي المختلفة المجموعات وهي الصفات . المجاالت عناصر

8

Page 9: Chapter 5 The Relational Data Model and Relational Database Constraints

العالقة : StudentخارطةStudent (Std-Name,Std-No,Tel-No,Address,Average,Age)= العالقة 6درجة الصفةA للعالقةR تكتبR.A (Dot Notation) مثال،Student.Std-

Name ، Student.Age. العالقة الصف Studentفي لدينا كان إذاt=('Ali', '02-11', '554612', 'Omdurman','87', '22')t[Std-Name] = <'Ali>t[Std-No,Address,Age] = <'02-11', 'Omdurman',

'22>' ) ( الخارطة ولكن البيانات تحديث عند باستمرار تتغير العالقة حالة

) ( سابقا ذكر كما تتغير ما نادرا

9

Page 10: Chapter 5 The Relational Data Model and Relational Database Constraints

Informal Terms Formal Terms

TableRelation

Column HeaderAttribute

All possible Column Values

Domain

RowTuple

Table DefinitionSchema of a Relation

Populated TableState of the Relation

10

Page 11: Chapter 5 The Relational Data Model and Relational Database Constraints

-: العالقة في الصفوف ترتيبرياضيا الصفوف، من مجموعة أنها أساس علي معرفة العالقةفتعريف . وبالتالي الترتيب فيها يشترط ال المجموعة عناصر

. الترتيب يكون أحيانا للترتيب شرط أي علي يحتوي ال العالقةمنطقياً.

-: الصف في القيم ترتيبالصفات ترتيب العالقة، خارطة في الصفات ترتيب حسب علي

. منطقيا أيضا يكون وقد العالقة في محدد غير أيضا

11

Page 12: Chapter 5 The Relational Data Model and Relational Database Constraints

-: الصفوف في القيم صماء )القيم تكون أن يجب الصف ( .Atomicفي

◦ Each value in a tuple must be from the domain of the attribute for that column If tuple t = <v1, v2, …, vn> is a tuple (row) in the

relation state r of R(A1, A2, …, An) Then each vi must be a value from dom(Ai)

القيمة تأخذ قد الصفوف في القيم (.Null (بعض Several meaning for null:Value unknown, value exists but is not available, or attribute does

not apply to this tuple .

12

Page 13: Chapter 5 The Relational Data Model and Relational Database Constraints

خاصة لكائنات حقيقة توضح إما العالقة في الصفوف (Particular Entities) العالقة في أو Studentكما

. الكائنات بين لعالقة حقيقة:مثال العالقة يوضح : Majors(Std-No , Dep-No)خارطة

والقسم طالب كل عالقة العالقة هذه في صف كل.) مختلفة ) كائنات بين عالقة له يتبع الذي

13

Page 14: Chapter 5 The Relational Data Model and Relational Database Constraints

المجال Domain Constraintقيد المفتاح Key Constraintقيد للكائن التكاملية Entity Integrity Constraintقيد المرجعية التكاملية Referential Integrityقيد

Constraint التكاملية بقيود األخيران القيدان يعرف( Integrity Constraints )بـ لها ( (ICويرمز

14

Page 15: Chapter 5 The Relational Data Model and Relational Database Constraints

للصفة قيمة أي أن صماء Aتقتضي قيمة تكون أن يجب dom(A)في .

Domain constraint specify that the value of each attribute A within each tuple MUST BE ATOMIC VALUE FROM DOM(A).

15

Page 16: Chapter 5 The Relational Data Model and Relational Database Constraints

Superkey of R: ◦ Is a set of attributes SK of R with the following

condition: No two tuples in any valid relation state r(R) will have

the same value for SK That is, for any distinct tuples t1 and t2 in r(R), t1[SK]

t2[SK] This condition must hold in any valid state r(R)

Key of R:◦ A "minimal" superkey◦ That is, a key is a superkey K such that removal of

any attribute from K results in a set of attributes that is not a superkey (does not possess the superkey uniqueness property)

16

Page 17: Chapter 5 The Relational Data Model and Relational Database Constraints

Example: Consider the CAR relation schema:◦ CAR(State, Reg#, SerialNo, Make, Model, Year)◦ CAR has two keys:

Key1 = {State, Reg#} Key2 = {SerialNo}

◦ Both are also superkeys of CAR◦ {SerialNo, Make} is a superkey but not a key.

In general:◦ Any key is a superkey (but not vice versa)◦ Any set of attributes that includes a key is a

superkey◦ A minimal superkey is also a key

17

Page 18: Chapter 5 The Relational Data Model and Relational Database Constraints

If a relation has several candidate keys, one is chosen arbitrarily to be the primary key. ◦ The primary key attributes are underlined.

Example: Consider the CAR relation schema:◦ CAR(State, Reg#, SerialNo, Make, Model, Year)◦ We chose SerialNo as the primary key

The primary key value is used to uniquely identify each tuple in a relation◦ Provides the tuple identity

Also used to reference the tuple from another tuple◦ General rule: Choose as primary key the smallest of

the candidate keys (in terms of size)◦ Not always applicable – choice is sometimes

subjective

18

Page 19: Chapter 5 The Relational Data Model and Relational Database Constraints

19

Page 20: Chapter 5 The Relational Data Model and Relational Database Constraints

وأي الصفوف، من مجموعة أنها علي العالقة تعرففال وبالتالي يتكرر ال أن يجب المجموعة في عنصر

يجب ذلك ولضمان العالقة في صف أي بتكرار يسمحأساسي مفتاح هنالك يكون أن

(Primary Key ) أو صفة من يتكون وهو عالقة لكلعن صف أي لتمييز ويستخدم العالقة صفات من أكثر

خاصيةالتفرد ) العالقة صفوف فى التتكرر وقيمته األخرUniqueness)

االساسى المفتاح ً :Rللعالقة PKاذا االتى يحققt1(PK)<>t2(PK)<>…………..tm(PK)

20

Page 21: Chapter 5 The Relational Data Model and Relational Database Constraints

: العالقة في الصفة Studentمثال تكون أن يمكنStd-No. للعالقة األساسي المفتاح هي

المفتاح تمثل أن يمكن صفة من أكثر للعالقة يكون قد ) التمييز) بخاصية تمتاز حدة علي صفة كل األساسي

بـ الصفات هذه المفاتيح ) Candidate Keysوتسميهذه(. بين من واحدة صفة اختيار يتم ولكن المرشحة

الـ لتمثل PKالصفات . الـ التي PKيوضح الصفة تحت خط بوضع العالقة خارطة في

األساسي . المفتاح صفة تمثلR (A , B, C) Simple PK, R (A , B, C) Composite PK

21

Page 22: Chapter 5 The Relational Data Model and Relational Database Constraints

تأخذ أن يمكن الصفة كانت إذا ما ايضا نحدد أحياناالصفة Nullالقيمة أن نحدد دائما Std-Nameفمثال

Not Null. للصفة بالنسبة قيد يعتبر وهذا

22

Page 23: Chapter 5 The Relational Data Model and Relational Database Constraints

العالئقية البيانات قاعدة Relational Database خارطةSchema-:

وبالتالي العالقات من مجموعة عن عبارة العالئقية البيانات قاعدةا قاعدة فخارطة

S البيانات لقاعدة المكونة العالقات خرط مجموعة تمثلS ={ R1 , R2, ….., Rm}

(Integrity Constraints IC) التكاملية قيود من ومجموعة◦ A set S of relation schemas that belong to the same database.◦ S is the name of the whole database schema◦ S = {R1, R2, ..., Rn}◦ R1, R2, …, Rn are the names of the individual relation

schemas within the database S

العالئقية البيانات قاعدة :-Relational Database Stateحالةالبيانات لقاعدة المكونة العالقات حاالت مجموعة {r1, r2, ………,rm}هي

صحيحة حاالت ) (Valid States)وهي قاعدة قيود القيود لكل مستوفية أي(.ICالبيانات

23

Page 24: Chapter 5 The Relational Data Model and Relational Database Constraints

COMPANY Database Schema

24

Page 25: Chapter 5 The Relational Data Model and Relational Database Constraints

الصفةDNumber العالقة العالقة Departmentفي وفيDep-Location. المفهوم نفس يمثالن

لها يكون أن المفهوم نفس تمثل التي الصفات في يشترط ال. المختلفة العالقات في االسم نفس

الصفةDNo العالقة في Dnumوالصفة Employeeفي. Projectالعالقة القسم رقم وهو المفهوم نفس الي يشيران

من المفهوم نفس لهما الصفتان أن علي نتعرف أن نستطيعالعالقات معني فهم

Semantic meaning of the relations الـ من ) DDLنستخدم جزء وهي البيانات قاعدة خارطة النشاء

الـ بقاعدة( SQLأوامر الخاصة القيود كل تحديد الي باالضافةالبيانات.

25

Page 26: Chapter 5 The Relational Data Model and Relational Database Constraints

ان ◦ يمكن ال العالقة فى صف إلى االساسى المفتاح قيمة ان تقتضى.NULLتكون الصفوف بين للتمييز تستخدم الصفة هذه ألن وذلك

◦ 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) If PK has several attributes, null is not allowed in any of

these attributes◦ Note: Other attributes of R may be constrained to disallow

null values, even though they are not members of the primary key.

26

Page 27: Chapter 5 The Relational Data Model and Relational Database Constraints

فقط واحدة عالقة فى تحدد للكائن التكاملية وقيود المفتاح قيود(individual relation )تكون المرجعية التكاملية قيود ولكن

عالقتين بين ( (specified between two relationsمشتركةالبيانات صحة على للحفاظ مفيدة Data) وهى

Consistency). العالقتين صفوف بين

27

Page 28: Chapter 5 The Relational Data Model and Relational Database Constraints

العالقتين بين المرجعية R2وR1التكامليةاألتي :تقتضى

.1 ) ( عالقة فى اوصفات صفة او عمود خارجى R1 وجود مفتاح يمثل((foreign key ( ب له الراجعة R1 وتسمى( FKويرمز العالقة

( المرجع المفتاح( referencing relationالى هذا يمثل بحيثالمفتاح مجال نفس له ويكون مفهوم نفس الخارجى

العالقة( PKاالساسى) بالعالقه R2،وتسمى R2فى.referenced relation)المرجع)

فى 2. الخارجى المفتاح تكون R1قيمة ان قيمة NULLإما تكون اوالمرجع العالقة فى االساسى المفتاح قيم .R2من

اليوجد 3. ً فاذا اعاله الشرطان يتحقق لم وبالتالي R1فى FKاذابين المرجعية التكاملية شروط .R2وR1التتحقق

28

Page 29: Chapter 5 The Relational Data Model and Relational Database Constraints

Another definition:- (When a foreign key value is not null ( in

referencing relation ) then it must be exist as a value of PK in another relation ( referenced relation) otherwise is not a foreign key).

29

Page 30: Chapter 5 The Relational Data Model and Relational Database Constraints

Student (Std-No, Std-name, Project-No) Project(Pr-No, Pr-Name)

Student Std-NoStd- Name Project -Noproject Pr-NoPr-Name

1Ali55Math

2OsmanNull7Expert Systems

3Omer66Database

آلن مستوفاة المرجعيه التكاملية شروط بأن القول يمكن الـ Studentفى Project – Noهنا ؟ FKتمثل لمادا30

Page 31: Chapter 5 The Relational Data Model and Relational Database Constraints

الخارجى للمفتاح يحدث المفتاح FKمادا حذفنا او عدلنا لوخيارات : : ثالثة هنالك المرجع العالقة فى االساسى

وجود 1. عند االساسى المفتاح لقيم التعديل او الحذف منعخارجى القيمة FKمفتاح نفس . (Restrict)يحمل

الخارجى 2. المفتاح قيمة المفتاح NULLجعل حذف عند(Nullify).االساسى

او 3. حذف عند الخارجى المفتاح قيم تعديل او حذف متابعةاالساسى المفتاح . (Cascade)تعديل

31

Page 32: Chapter 5 The Relational Data Model and Relational Database Constraints

العالقه فى االول الصف حذفنا لو السابق المثال منProject -: الخيارات

هذا فى االساسى المفتاح قيمة الن االول للصف الحذف منعالعالقة فى خارجى لمفتاح قيمة تمثل فى studentالصف

االول الصف العالقه فى االول الصف فى الخارجى المفتاح قيمة جعل

student تساوىNull. للعالقه االول الصف حذف Studentاو

القاعدية البيانات قاعدة خارطة فى توضح التكاملية قيود كلالـ اوامر قاعدة ) DDLباستخدام تعريف من كجزء تعرف

البيانات (.

32

Page 33: Chapter 5 The Relational Data Model and Relational Database Constraints

البيانات قاعدة خارطة على المرجع تكاملية قيد توضيح يمكنوالعالقة المرجع العالقة بين تربط خطوط برسم العالئقية

. المرجع العالقة في السهم رأس بحيث للمرجع الراجعة. ادناه الشكل انظر

33

Page 34: Chapter 5 The Relational Data Model and Relational Database Constraints

Referential Integrity Constraints for COMPANY database

34

Page 35: Chapter 5 The Relational Data Model and Relational Database Constraints

Semantic Integrity Constraints:◦ based on application semantics and cannot be

expressed by the model.◦ Example: “the max. no. of hours per employee for

all projects he or she works on is 56 hrs per week” A constraint specification language may

have to be used to express these . SQL-99 allows triggers and ASSERTIONS to

express for some of these.

35

Page 36: Chapter 5 The Relational Data Model and Relational Database Constraints

Each relation will have many tuples in its current relation state

The relational database state is a union of all the individual relation states

Whenever the database is changed, a new state arises

Basic operations for changing the database:◦ INSERT a new tuple in a relation◦ DELETE an existing tuple from a relation◦ MODIFY an attribute of an existing tuple

Next slide shows an example state for the COMPANY database

36

Page 37: Chapter 5 The Relational Data Model and Relational Database Constraints

Populated database state for COMPANY

37

Page 38: Chapter 5 The Relational Data Model and Relational Database Constraints

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.

38

Page 39: Chapter 5 The Relational Data Model and Relational Database Constraints

In case of integrity violation, several actions can be taken:◦ Cancel the operation that causes the violation

(RESTRICT or 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

39

Page 40: Chapter 5 The Relational Data Model and Relational Database Constraints

INSERT may violate any of the constraints:◦ Domain constraint:

if one of the attribute values provided for the new tuple is not of the specified attribute domain

◦ Key constraint: if the value of a key attribute in the new tuple already

exists in another tuple in the relation◦ Referential integrity:

if a foreign key value in the new tuple references a primary key value that does not exist in the referenced relation

◦ Entity integrity: if the primary key value is null in the new tuple

40

Page 41: Chapter 5 The Relational Data Model and Relational Database Constraints

DELETE may violate only referential integrity:◦ If the primary key value of the tuple being deleted is

referenced from other tuples in the database Can be remedied by several actions: RESTRICT,

CASCADE, SET NULL (see Chapter 8 for more details) RESTRICT option: reject the deletion CASCADE option: propagate the new primary key value into

the foreign keys of the referencing tuples SET NULL option: set the foreign keys of the referencing

tuples to NULL◦ One of the above options must be specified during

database design for each foreign key constraint

41

Page 42: Chapter 5 The Relational Data Model and Relational Database Constraints

UPDATE may violate domain constraint and NOT NULL constraint on an attribute being modified

Any of the other constraints may also be violated, depending on the attribute being updated:◦ Updating the primary key (PK):

Similar to a DELETE followed by an INSERT Need to specify similar options to DELETE

◦ Updating a foreign key (FK): May violate referential integrity

◦ Updating an ordinary attribute (neither PK nor FK): Can only violate domain constraints

42

Page 43: Chapter 5 The Relational Data Model and Relational Database Constraints

Presented Relational Model Concepts◦ Definitions◦ Characteristics of relations

Discussed Relational Model Constraints and Relational Database Schemas◦ Domain constraints’◦ Key constraints◦ Entity integrity◦ Referential integrity

Described the Relational Update Operations and Dealing with Constraint Violations

43

Page 44: Chapter 5 The Relational Data Model and Relational Database Constraints

Consider the following relations for a database that keeps track of student enrollment in courses and the books adopted for each course:

STUDENT(SSN, Name, Major, Bdate)

COURSE(Course#, Cname, Dept)

ENROLL(SSN, Course#, Quarter, Grade)

BOOK_ADOPTION(Course#, Quarter, Book_ISBN)

TEXT(Book_ISBN, Book_Title, Publisher, Author)

Draw a relational schema diagram specifying the foreign keys for this schema.

44