بسم الله الرحمن الرحيم Lecture 9

Download Report

Transcript بسم الله الرحمن الرحيم Lecture 9

‫بسم هللا الرحمن الرحيم‬
Lecture (7)
Chapter 5
The Relational Data Model and
Relational Database Constraints
1
‫‪‬‬
‫‪‬‬
‫‪‬‬
‫‪‬‬
‫‪2‬‬
‫قدمه الباحث ‪ Ted Codd‬في ‪.) IBM Research( 1970‬‬
‫يعتبر من أشهر أنواع الـ‪ Implementation models‬ويستخدم في كثير‬
‫من الـ ‪ DBMS‬الحالية‪.‬‬
‫يمتاز بسهولة وبساطة مفاهيمه وأساسه الرياضي(‪)Mathematical Foundation‬‬
‫يعتمد علي أساس مفهوم العالقة الرياضية )‪ ) Mathematical Relation‬ويخضع‬
‫لقوانين نظرية المجموعات )‪.)Set theory‬‬
‫‪ ‬تعرف قاعدة البيانات في النموذج العالئقي علي أنها مجموعة من العالقات‬
‫(‪. )set of relations‬‬
‫‪ ‬العالقة تمثل بجدول من القيم )‪ (Table of values‬وهي تشبه لحد ما ملف السجالت‬
‫المسطح‪.‬‬
‫‪ ‬وتعرف العالقة علي انها مجموعة من الصفوف ( ‪.(set of rows‬‬
‫‪ ‬اي صف (‪ )row‬يمثل مجموعة من القيم (‪ )set of values‬والتي تمثل حقيقة‪.‬‬
‫‪ ‬أعمدة الجدول أوالعالقة تسمي بـ )‪ (columns‬وتمثل صفات العالقة )‪.(attributes‬‬
‫‪‬‬
‫‪‬‬
‫‪3‬‬
‫الصفوف في النموذج العالئقي يطلق عليها )‪. (tuples‬‬
‫اسم العالقة وأسماء االعمدة )‪ (name of attributes‬تساعد علي تفسير القيم في صفوف‬
‫العالقة‬
‫العالقة تتحدث عن نوع الكائن ‪ Student‬وأن القيم فيي أي صيف مين صيفوف العالقية تتحيدث عين )‪(particular entity‬‬
‫(طالب خاص) ‪.‬‬
‫‪4‬‬
‫‪‬‬
‫‪‬‬
‫‪‬‬
‫اي صفة في العالقة لها مجال‪.‬‬
‫المجال هو مجموعة عناصر القيم (‪ )Atomic values‬التي يمكن أن تأخذها الصفة‬
‫التي تنتمي لهذا المجال‪.‬‬
‫يمكن أن تعتبر الـ ‪ Data types‬المعروفة (‪ )…,int,char‬مجاالت للصفات ‪.‬‬
‫‪ ‬يمكن أيضا تعريف المجال ويكون له اسم ‪،‬نوع ‪،‬نسق)‪ (format‬ويسمي هذا‬
‫بالتعريف المنطقي للمجال )‪.(logical definition of domain‬‬
‫‪ ‬مثال‪:‬‬
‫المجال للصفة ‪ StdNo‬في العالقة ‪Student‬‬
‫اسم المجال‪ ، Student Number :‬النوع‪ ، char(5) :‬النسق )‪:(format‬‬
‫‪. dd-dd‬‬
‫‪5‬‬
‫‪‬‬
‫خارطة العالقة ‪ R‬توضح كاألتي ))‪R(A1: dom(A1), A2: dom(A2),., An: dom(An‬‬
‫او )‪R(A1, A2,…....., An‬‬
‫‪‬‬
‫‪‬‬
‫‪‬‬
‫‪‬‬
‫‪6‬‬
‫اسم العالقة ‪( R:‬يختلف عن أسماء كل العالقات في داخل قاعدة البيانات الواحدة)‬
‫صفات العالقة‪. An،.....، A2، A1 :‬‬
‫ولكل صفة ‪ Ai‬مجال )‪. dom(Ai‬‬
‫درجة العالقة‪ :‬تعني عدد صفات العالقة‪ R ) .‬من الدرجة ‪. )n‬‬
(current set of tuples)) ‫ تعني مجموعة الصفوف الحالية المكونة للعالقة‬:‫ حالة العالقة‬


m-‫ ولها نفس اسم خارطة العالقة وهي عبارة عن مجموعة من الـ‬r(R)‫ويرمز لها بـ‬
. (cardinality of R) tuples
r(R)={t1,t2,….,tm}
.(n values) ‫ يحتوي علي‬t ‫ حيث كل صف‬
t=<v1,………,vn> 
‫ أو قيمة خالية‬1≥ i ≥ n ‫ حيث‬dom(Ai( ‫ هي عبارة عن عنصر في المجال‬vi 
.(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
‫‪‬‬
‫‪‬‬
‫‪‬‬
‫‪8‬‬
‫) ‪ r (R‬هي عبارة عن عالقة رياضية من الدرجة ‪ n‬للمجاالت‬
‫)‪dom(An)،....، dom(A2)،dom(A1‬‬
‫)‪r(R)  dom (A1) X dom (A2) X ....X dom(An‬‬
‫وهي تمثل مجموعة جزئية من الحاصل الكارتيزي لمجاالت الصفات وهي‬
‫المجموعات المختلفة التي يمكن تكوينها من عناصر المجاالت‪.‬‬
:Student ‫خارطة العالقة‬
Student (Std-Name,Std-No,Tel-No,Address,Average,Age)
6= ‫درجة العالقة‬
،Student.Std-Name ‫مثال‬، (Dot Notation) R.A ‫ تكتب‬R ‫ للعالقة‬A ‫الصفة‬
. 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
Informal Terms
Formal Terms
Table
Relation
Column Header
Attribute
All possible Column
Values
Row
Domain
Table Definition
Schema of a Relation
Populated Table
State of the Relation
Tuple
10
‫‪ ‬ترتيب الصفوف في العالقة‪-:‬‬
‫العالقة معرفة علي أساس أنها مجموعة من الصفوف‪ ،‬رياضيا عناصر المجموعة‬
‫ال يشترط فيها الترتيب ‪ .‬وبالتالي فتعريف العالقة ال يحتوي علي أي شرط‬
‫للترتيب‪ .‬أحيانا يكون الترتيب منطقيا‪.‬‬
‫‪ ‬ترتيب القيم في الصف‪-:‬‬
‫علي حسب ترتيب الصفات في خارطة العالقة‪ ،‬ترتيب الصفات أيضا غير محدد‬
‫في العالقة وقد يكون أيضا منطقيا‪.‬‬
‫‪11‬‬
-:‫القيم في الصفوف‬
. )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
‫‪ ‬الصفوف في العالقة إما توضح حقيقة لكائنات خاصة‬
‫)‪ (Particular Entities‬كما في العالقة ‪ Student‬أو حقيقة لعالقة‬
‫بين الكائنات‪.‬‬
‫‪ ‬مثال‪:‬‬
‫‪ ‬خارطة العالقة )‪ : Majors(Std-No , Dep-No‬يوضح كل‬
‫صف في هذه العالقة عالقة كل طالب والقسم الذي يتبع له (عالقة بين‬
‫كائنات مختلفة)‪.‬‬
‫‪13‬‬
Domain Constraint ‫ قيد المجال‬
Key Constraint ‫ قيد المفتاح‬
Entity Integrity Constraint ‫ قيد التكاملية للكائن‬
Referential Integrity ‫ قيد التكاملية المرجعية‬
Constraint
‫ يعرف القيدان األخيران بقيود التكاملية‬
) IC) ‫) ويرمز لها بـ‬Integrity Constraints(
14

‫ يجب أن تكون قيمة صماء في‬A ‫ تقتضي أن أي قيمة للصفة‬
dom(A).
Domain constraint specify that the value of
each attribute A within each tuple MUST BE
ATOMIC VALUE FROM DOM(A).
15

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

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

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:

The primary key value is used to uniquely identify
each tuple in a relation
◦ CAR(State, Reg#, SerialNo, Make, Model, Year)
◦ We chose SerialNo as the primary key
◦ 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
19
‫‪ ‬تعرف العالقة علي أنها مجموعة من الصفوف‪ ،‬وأي عنصر في‬
‫المجموعة يجب أن ال يتكرر وبالتالي فال يسمح بتكرار أي صف في‬
‫العالقة ولضمان ذلك يجب أن يكون هنالك مفتاح أساسي‬
‫)‪(Primary Key‬لكل عالقة وهو يتكون من صفة أو أكثر من‬
‫صفات العالقة ويستخدم لتمييز أي صف عن األخر وقيمته التتكرر‬
‫فى صفوف العالقة (خاصيةالتفرد ‪)Uniqueness‬‬
‫‪ ‬اذا المفتاح االساسى ‪ PK‬للعالقة ‪ R‬يحقق االتى‪:‬‬
‫)‪t1(PK)<>t2(PK)<>…………..tm(PK‬‬
‫‪20‬‬
‫‪‬‬
‫‪‬‬
‫‪‬‬
‫‪‬‬
‫‪21‬‬
‫مثال ‪:‬في العالقة ‪ Student‬يمكن أن تكون الصفة ‪ Std-No‬هي‬
‫المفتاح األساسي للعالقة ‪.‬‬
‫قد يكون للعالقة أكثر من صفة يمكن أن تمثل المفتاح األساسي( كل‬
‫صفة علي حدة تمتاز بخاصية التمييز) وتسمي هذه الصفات بـ‬
‫‪( Candidate Keys‬المفاتيح المرشحة)‪ .‬ولكن يتم اختيار صفة‬
‫واحدة من بين هذه الصفات لتمثل الـ ‪PK‬‬
‫‪.‬يوضح الـ‪ PK‬في خارطة العالقة بوضع خط تحت الصفة التي تمثل صفة‬
‫المفتاح األساسي ‪.‬‬
‫‪R (A , B, C) Simple PK, R (A , B, C) Composite PK‬‬
‫‪‬‬
‫‪22‬‬
‫أحيانا نحدد ايضا ما إذا كانت الصفة يمكن أن تأخذ القيمة ‪Null‬‬
‫فمثال نحدد أن الصفة ‪ Std-Name‬دائما ‪ Not Null‬وهذا يعتبر‬
‫قيد بالنسبة للصفة ‪.‬‬
‫‪ ‬خارطة قاعدة البيانات العالئقية ‪-: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‬‬
COMPANY Database Schema
24
‫‪ ‬الصفة ‪ DNumber‬في العالقة ‪ Department‬وفي العالقة ‪Dep-‬‬
‫‪ Location‬يمثالن نفس المفهوم‪.‬‬
‫‪ ‬ال يشترط في الصفات التي تمثل نفس المفهوم أن يكون لها نفس االسم في‬
‫العالقات المختلفة‪.‬‬
‫‪ ‬الصفة ‪ DNo‬في العالقة ‪ Employee‬والصفة ‪ Dnum‬في العالقة‬
‫‪ Project‬يشيران الي نفس المفهوم وهو رقم القسم‪.‬‬
‫‪ ‬نستطيع أن نتعرف علي أن الصفتان لهما نفس المفهوم من فهم معني العالقات‬
‫‪Semantic meaning of the relations‬‬
‫‪ ‬نستخدم الـ ‪ DDL‬النشاء خارطة قاعدة البيانات (وهي جزء من أوامر الـ‪)SQL‬‬
‫باالضافة الي تحديد كل القيود الخاصة بقاعدة البيانات‪.‬‬
‫‪25‬‬
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
‫‪ ‬قيود المفتاح وقيود التكاملية للكائن تحدد فى عالقة واحدة فقط ( ‪individual‬‬
‫‪ )relation‬ولكن قيود التكاملية المرجعية تكون مشتركة بين عالقتين‬
‫)‪ )specified between two relations‬وهى مفيدة للحفاظ على‬
‫صحة البيانات )‪ (Data Consistency‬بين صفوف العالقتين‪.‬‬
‫‪27‬‬
‫‪‬‬
‫‪.1‬‬
‫‪.2‬‬
‫‪‬‬
‫‪28‬‬
‫التكاملية المرجعية بين العالقتين ‪R1‬و‪ R2‬تقتضى األتي ‪:‬‬
‫وجود عمود او صفة( اوصفات) فى عالقة ‪ R1‬يمثل مفتاح خارجى‬
‫( ‪(foreign key‬ويرمز له ب(‪ )FK‬وتسمى ‪ R1‬العالقة الراجعة الى‬
‫المرجع( ‪ )referencing relation‬بحيث يمثل هذا المفتاح الخارجى‬
‫نفس مفهوم ويكون له نفس مجال المفتاح االساسى(‪ )PK‬فى العالقة‬
‫‪،R2‬وتسمى ‪ R2‬بالعالقه المرجع(‪.(referenced relation‬‬
‫قيمة المفتاح الخارجى فى ‪ R1‬إما ان تكون ‪ NULL‬او تكون قيمة من قيم‬
‫المفتاح االساسى فى العالقة المرجع ‪.R2‬‬
‫اذا لم يتحقق الشرطان اعاله فاذا اليوجد ‪FK‬فى ‪ R1‬وبالتالي التتحقق شروط‬
‫التكاملية المرجعية بين ‪R1‬و‪.R2‬‬


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


Student (Std-No, Std-name, Project-No)
Project(Pr-No, Pr-Name)
Student
Std-No
Std- Name
Project -No
1
Ali
2
3
project
Pr-No
Pr-Name
5
5
Math
Osman
Null
7
Expert
Systems
Omer
6
6
Database
‫لمادا ؟‬FK ‫تمثل الـ‬Student ‫ فى‬Project – No ‫هنا يمكن القول بأن شروط التكاملية المرجعيه مستوفاة آلن‬
30
‫‪‬‬
‫‪.1‬‬
‫‪.2‬‬
‫‪.3‬‬
‫‪31‬‬
‫مادا يحدث للمفتاح الخارجى ‪ FK‬لو عدلنا او حذفنا المفتاح االساسى فى‬
‫العالقة المرجع ‪ :‬هنالك ثالثة خيارات ‪:‬‬
‫منع الحذف او التعديل لقيم المفتاح االساسى عند وجود مفتاح خارجى ‪FK‬‬
‫يحمل نفس القيمة ) ‪. (Restrict‬‬
‫جعل قيمة المفتاح الخارجى ‪ NULL‬عند حذف المفتاح االساسى )‪.(Nullify‬‬
‫متابعة حذف او تعديل قيم المفتاح الخارجى عند حذف او تعديل المفتاح‬
‫االساسى ) ‪. (Cascade‬‬
‫‪‬‬
‫‪‬‬
‫‪‬‬
‫‪‬‬
‫‪‬‬
‫‪32‬‬
‫من المثال السابق لو حذفنا الصف االول فى العالقه ‪ Project‬الخيارات ‪-:‬‬
‫منع الحذف للصف االول الن قيمة المفتاح االساسى فى هذا الصف تمثل قيمة‬
‫لمفتاح خارجى فى العالقة ‪ student‬فى الصف االول‬
‫جعل قيمة المفتاح الخارجى فى الصف االول فى العالقه ‪ student‬تساوى‬
‫‪. Null‬‬
‫او حذف الصف االول للعالقه ‪Student‬‬
‫كل قيود التكاملية توضح فى خارطة قاعدة البيانات القاعدية باستخدام اوامر الـ‬
‫‪ ( DDL‬تعرف كجزء من تعريف قاعدة البيانات )‪.‬‬
‫‪‬‬
‫‪‬‬
‫‪33‬‬
‫يمكن توضيح قيد تكاملية المرجع على خارطة قاعدة البيانات العالئقية برسم‬
‫خطوط تربط بين العالقة المرجع والعالقة الراجعة للمرجع بحيث رأس السهم في‬
‫العالقة المرجع‪.‬‬
‫انظر الشكل ادناه‪.‬‬
Referential Integrity Constraints for COMPANY database
34

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




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
Populated database state for COMPANY
37






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

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

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

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


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

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
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