Download as pdf or txt
Download as pdf or txt
You are on page 1of 29

Review of Database Modeling

1- The three-views database architecture (The ANSI/SPARC) architecture


a. External (user view)
b. Conceptual view (Logical view) ‫ مراحل اللي بنمر فيهم أول اشي بنفهم المشكلة اللي حنعمل عشانها الداتابيس‬3 ‫هدول الـ‬
c. Internal (physical view) SQL ‫ للرسمة واخر مرحلة بنستخدم الـ‬Mapping ‫ وبعدين بنعمل‬ERD ‫ونرسم الـ‬

2- Table=Relation
a. Table degree (# of columns = fields = attributes)
b. Table cardinality (# of rows = records = tuples)
c. Field’s domain = possible values for a field = datatype of a field
d. Database instance

‫ اللي فيه‬columns ‫ ألي جدول هي كم عدد الـ‬degree ‫ الـ‬/ ... Employee ‫ أو‬Student ‫ مثال‬Table ‫ هي نفسها الـ‬Relation ‫الـ‬
‫ صف‬500 ‫ بالجدول لو كان في‬records ‫ هي كم عدد الصفوف أو الـ‬cardinality ‫ الـ‬/ degree = 2 ‫يعني لو كان فيه عامودين الـ‬
/ int , varchar ‫ ممكن يكون‬Attributes ‫ للـ‬datatype ‫ هي عبارة عن الـ‬domain ‫ الـ‬/ 500 ‫ تساوي‬cardinality ‫بتكون الـ‬
‫ منها في وقت معين‬sample ‫ من الداتابيس يعني باخد‬view ‫ هي لما أعمل‬database instance ‫الـ‬

3- ER diagram
a. Entity (Strong & Weak)
b. Attributes (single, composite, derived, multi-valued)
c. Keys (candidate, super, primary, secondary, surrogate )
d. Relationships
- Degree (unary, binary, ternary, n-ary)
- Cardinality (1-1, 1-M, M-M)
- Participation (total (full), partial (optional))
- Attributes on relationships
- Identifying relationship (connecting strong entity to weak entity)

‫ الـ‬/ 2 ‫ موجود جدول بميز أنواعهم بصفحة‬attributes ‫ الـ‬/ Strong , week ‫ هي أي اشي يمكن وصفه وإلها نوعين‬entity ‫الـ‬
‫ اللي هي نسبة‬cardinality ‫ هي كم عدد الجداول اللي مرتبطة بهاي العالقة والـ‬degree ‫ الـ‬: ‫ الها خصائص‬relationships
‫المشاركة بين الجداول يعني لو أخدنا العالقة بين األقسام والموظفين بنحكي أكتر من موظف بشتغل بقسم واحد بس الموظف ما بكون‬
‫ هي طبيعة المشاركة بالمثال اللي قبل كل‬participation ‫ الـ‬/ ‫ من جهة الموظفين‬m ‫ والـ‬1:m ‫تابع ألكتر من قسم وهون بتكون‬
att ‫ه‬ ‫ ي‬Attributes on relationships ‫ الـ‬/ ‫ من الجهتين‬total ‫األقسام بشتغل فيها موظفين وكل الموظفين تابعين ألقسام فبتكون‬
‫ ه الىل ربتبط ن‬Identifying relationship ‫ الـ‬/ ‫ـجدولي الىل ربتبطهم‬
strong ‫بي‬ ‫ن‬ ‫ وإلها عالقة بال‬relationship ‫بتكون موجودة عىل الـ‬
‫ي ي‬ ‫ي‬
. week entity ‫ و‬entity
4- Mapping ER diagram into relations (tabular representation)
a. Mapping strong and weak entities
b. Mapping multivalued attributes
c. Mapping relationships (1-1, 1-M, M-M)

week entity ‫ لجدول الـ‬composite PK ‫ عبارة عن‬Discriminator ‫ والـ‬FK ‫ وبكون الـ‬week ‫ وبنحطه بجدول الـ‬strong ‫ اللي بالـ‬PK ‫بننقل الـ‬
composite PK ‫ تبع الجدول اللي كانت موجوده فيه وبصيروا التنين مع بعض‬primary key ‫ بحطها بـجدول جديد وبنقل معها الـ‬multivalue ‫ الـ‬/
‫جدول وبنحطهم مع‬ ‫ تبع كل‬pk ‫ وبناخد الـ‬relationship ‫ بنعمل جدول جديد باسم الـ‬m:m ‫ والـ‬m ‫ لعند الـ‬1‫ من جهة الـ‬pk ‫ بناخد الـ‬m:1 ‫ بحالة الـ‬/
‫الجدول الجديد‬

5- EER diagram
a. Specialization/Generalization
b. Specialization constraints:
- Distinct & Overlapping
- Total and partial
c. Aggregation
d. Categorization (union)
e. Mapping specialization/generalization into relations
f. Mapping aggregation into relations

6- SQL Create Statement


a. Datatypes
b. Attribute constraints (primary key, unique, null, not null, default)
c. Foreign key constraint
d. Check condition
e. On delete/update constraint

Type Definition Example


Simple Attributes ‫هي بتكون جاهزة وما بقدر أقسمها أكتر مثل الرقم الجامعي‬
...salary ‫أو الـ‬ ID

Composite Attribute LN ‫ و‬FN ‫هي بقدر أقسمها ألجزاء أكتر مثل األسم بتقسم للـ‬
Name

FN LN

Derived Attribute ‫ ثانية مثل أنه اشتق العمر‬attribute ‫هي اللي بتكون مشتقة من‬
Age
‫من تاريخ الميالد‬

Single Attribute ‫الشخص ما بكون أله‬- ‫ مثل االسم‬.‫يأخذ قيمة واحدة دائ ًما‬ ‫ي‬
ID ‫ والـ‬-‫أكتر من اسم‬ ‫أكثر‬ ID

composite ‫ وممكن‬Simple ‫ممكن تكون‬

Multivalue Attribute
‫يأخذ أكثر من قيمة مثل التلفون ممكن يكون اله أكثر من رقم واحد‬ Phone
‫الىل ممكن نستنتجهم من الـ ‪: ERD‬‬
‫بعض األشياء ي‬
‫ر‬ ‫ن‬ ‫ن‬
‫ه ‪ unary relationship‬أو ‪ recursive‬معناها انه الـ ‪ emp‬يف عالقة بينه وبي نفسه وبنقرأها بأكت من طريقة‪:‬‬ ‫‪ -‬الـ ‪ Supervision‬ي‬
‫‪1) Employee can supervise another employee.‬‬ ‫‪2) Employee is a supervisor for other employees.‬‬
‫‪3) An Employee can be supervisee by other employee.‬‬
‫ن‬
‫الموظفي عليهم مدراء‪.‬‬ ‫ن‬
‫الموظفي يكونو مدراء ومش كل‬ ‫الـ ‪ participation‬لهاي الـ ‪ relationship‬حيكون ‪ partial‬ألنه مش نضوري كل‬
‫ن‬
‫موظفي بس الموظف بكون عليه مدير واحد‪.‬‬ ‫الـ ‪ cardinality‬حتكون ‪ m :1‬الموظف ممكن يكون مدير عىل عدة‬

‫الموظفي نضوري يكونو مدراء أما من جهة الـ‬


‫ن‬ ‫‪ -‬الـ ‪ : MANAGES‬هون الـ ‪ participation‬من جهة الـ ‪ employee‬حتكون ‪ partial‬ألنه مش كل‬
‫فه ‪ total‬ألنه ازم كل قسم يكون اله مدير‪.‬‬
‫‪ department‬ي‬
‫ه ‪ 1:1‬الموظف بكون مدير عىل قسم واحد والقسم بكون اله مدير واحد ‪.‬‬ ‫الـ ‪ cardinality‬ي‬

‫مالحظة‪ :‬لما بدي أعمل ‪ mapping‬للـ ‪ manages‬المفروض اخد الـ ‪ pk‬من عند الـ ‪ partial‬لعند الـ ‪ total‬بس بهاي الحالة بقدر اعمل جدول جديد أحط‬
‫فيه الـ ‪ pk‬تبع الـ ‪ employee‬والـ ‪ department‬مع الـ ‪. start-date‬‬

‫ه ‪ M:M‬بس ليش حطينا ‪ M:N‬ألنه عدد الـ ‪ employee‬مش نضوري يساوي عدد الـ ‪. project‬‬
‫‪ -‬الـ ‪ WORKS_ON‬ي‬
‫‪-‬الـ ‪ DEPENDES_OF‬ه ‪ Identifying relationship‬ألنها ربتبط ‪ strong‬مع ‪ week‬الـ ‪ name‬حطينه تحته خط مقطع ألنه ممكن يكون عندي ر‬
‫اكت من‬ ‫ي‬
‫ابن الهم نفس االسم كيف بنحل هاي المشكلة؟ لما نعمل ‪ mapping‬بننقل الـ ‪ pk‬تبع الـ ‪ employee‬وبنحطه عند الـ ‪ dependent‬وهيك بصت ‪strong‬‬
Data Modeling Using the EER

EER model includes all modeling concepts of the ER model.


‫ا‬
‫ والـ‬Inheritance ‫ مثًل زي لما يكون عنا‬OPP ‫ وهاي المفاهيم ظهرت بسبب الـ‬ERD ‫ ؟ ألنه ن يف مفاهيم جديدة صعب نمثلها بالـ‬EER ‫ليش لجأنا للـ‬
‫ بس ضفنا عليهم أشياء‬ER ‫الىل عند الـ‬
‫ فيه نفس الخصائص ي‬EER ‫ وطلع الـ‬model ‫ فحدثنا عىل هاد الـ‬ER ‫ هاي األشياء ما بنقدر نمثلها بالـ‬encapsulation
‫شو هم؟‬

In addition, EER includes:

- Subclasses and superclasses


- Specialization and generalization
- Category or union type
- Attribute and relationship inheritance

Subclasses, Superclasses, and Inheritance:

Terms for relationship between a superclass and any one of its subclasses:

- Superclass/subclass
- Supertype/subtype
- Class/subclass relationship

Type inheritance: Subclass entity inherits all attributes and relationships of superclass.

Specialization
Generalization
‫ليش بنستخدم ‪ super class‬و ‪ sub class‬؟‬

‫ممكن يكون عنا ‪ entity‬رئيسية زي الـ ‪ employee‬وهدول الـموظفين الهم خصائص مشتركة وبتفرعو ألكتر من اشي بس ممكن كمان هاي التفرعات تتفرد‬
‫بخاصية لحالها فبتكون خصائصها هي المشتركة باإلضافة للخاصية اللي بتتفرد فيها‪.‬‬

‫لو أخدنا ً‬
‫مثال الـ ‪ Secretary‬حيكونو الـ ‪ Attribute‬إلها عبارة عن‪:‬‬

‫)‪(Ssn , Fname , Lname , Minit , Birth-date , Address , Typing-speed‬‬

‫‪Specialization and Generalization‬‬

‫الـ ‪ model‬بس أشوفه بفكر فيه بطريقتين‪:‬‬

‫‪ -‬من األعلى لألسفل أو من العموم إلى الخصوص )‪ (Top-Down‬هيك أحنا بنعمل )‪(Specialization‬‬

‫‪ -‬من األسفل لألعلى أو من الخصوص للعموم )‪ (Bottom-Up‬هيك بنعمل )‪(Generalization‬‬

‫مالحظة‪ :‬سواء عملنا ‪ Generalization‬أو ‪ Specialization‬المحصلة وحدة بس بتفرق بأنه كيف بنينا الـ ‪model‬‬

‫‪Constraints:‬‬
‫)‪(Partial / Total) (Overlap / Disjoint‬‬

‫‪Partial‬‬ ‫‪Total‬‬

‫يعني مش كل الـ ‪ employee‬حيتوزعو ممكن يكون‬


‫يعني كل الـ ‪ employees‬الزم يتوزعو على‬
‫في موظف ما بنتمي ألي نوع من الـ ‪. subclasses‬‬
‫الـ ‪ . subclasses‬ومبتتمثل بخطين‬
‫ومبتتمثل بخط واحد‬

‫‪Overlab‬‬ ‫‪O‬‬ ‫‪Disjoint‬‬ ‫‪d‬‬

‫يعني عادي الموظف يكون تابع ألكتر من ‪subclass‬‬ ‫يعني الموظف بكون تابع لـ ‪ subclass‬واحد بمعنى‬
‫بمعنى أخر أنه في تقاطع بين الـ ‪ entities‬الموظف‬ ‫أخر أنه ما في تقاطع بين الـ ‪ entities‬الموظف يا‬
‫ممكن يكون ‪ Secretary‬و ‪ Technician‬مع بعض‬ ‫بكون ‪ Secretary‬أو ‪ Technician‬مستحيل يكون‬
‫التنين مع بعض‬

‫)‪Partial: An entity may not belong to any of the subelasses (single-line‬‬

‫)‪Total: Every entity in the superclass must be a member of some subelass (double-edge‬‬
Specialization: Process of defining a set of subclasses of an entity type
▪ Defined on the basis of some distinguishing characteristic of the entities in the superclass

▪ Subclass can define:

▪ Specific attributes

▪ Specific relationship types

Generalization
▪ Reverse process of abstraction

▪ Generalize into a single superclass

▪ Original entity types are special subclasses

▪ Generalization: Process of defining a generalized entity type from the given entity types

Disjointness constraint: Specifies that the subclasses of the specialization must be disjoint.

▪ Completeness (or totalness) constraint

▪ May be total or partial

▪ Disjointness and completeness constraints are independent

Total/Overlap

Total/Disjoint Total/Disjoint
Specialization hierarchy: Every subclass participates as a subclass in only one class/subclass relationship.
▪ Results in a tree structure or strict hierarchy

Specialization lattice: Subclass can be a subclass in more than one class/subclass relationship.

employee, student, staff ‫ مثل الـ‬/ ‫ واحد‬parent ‫ إله‬son ‫ واحد فقط أو كل‬superclass ‫ يكون إله‬subclass ‫ يعني الـ‬:hierarchy ‫الـ‬
student_assistant ‫ مثل الـ‬/ parent ‫ إله أكتر من‬son ‫ أو كل‬superclass ‫ إله أكنر من‬subclass ‫ يعني الـ‬: lattice ‫الـ‬

Multiple inheritance: Subclass with more than one superclass


▪ If attribute (or relationship) originating in the same superclass inherited more than once via different paths
in lattice

• Included only once in shared subclass


superclass ‫ متفرعين من الـ‬subclass ‫لما يمون في عنا أكتر من‬

Modeling of UNION Types Using Categories:


▪ Union type or a category

▪ Represents a single superclass/subclass relationship with more than one superclass

▪ Subclass represents a collection of objects that is a subset of the UNION of distinct entity types

▪ Attribute inheritance works more selectively

▪ Category can be total or partial

Some modeling methodologies do not have union types

‫ فيعني ما في‬total ‫ والـ‬partial ‫ ما في بينهم أشياء مشتركة وعادي بكون فيه الـ‬subclasses ‫؟ بنستخدمه لما يكون في عنا‬union ‫ليش بنستخدم الـ‬
‫ وهو أصال مش كتير مستخدم‬subclasses ‫ غير بطبيعة الـ‬generalization ‫ والـ‬specialization ‫أختالف بينه وبين الـ‬
‫ فهو طالب دراسات عليا في إله صفات بختلف‬grad_student ‫ هو دكتور بالجامعة وإله صفات خاصة فيه أما بالنسبة للـ‬faculty ‫هسا عنا الـ‬
‫ بس ممكن هم التنين يعملو أبحاث فهاي شغلة اشتركو فيها فجمعناهم‬specialization ‫ أو‬generalization ‫فيها عن الدكتور فما بصير اعمللهم‬
.union ‫ وهاد هو استخدام الـ‬instructor_resarcher ‫ واحد وسميناه‬superclass ‫التنتين مع بعض بـ‬

▪ Many specializations and subclasses can be defined to make the conceptual model accurate

▪ If subclass has few specific attributes and no specific relationships Can be merged into the superclass

‫ مش مرتبطين بأي‬subclasses ‫ قليلة جدا أو الـ‬subclasses ‫ اللي على الـ‬attributes ‫ لو كان الـ‬EER ‫ للـ‬mapping ‫لما نيجي نعمل الـ‬
subclasses ‫ بس وما بعمل للـ‬superclass ‫ واحد للـ‬table ‫ هون بنعمل‬relationship
Aggregation and Association

Aggregation: Abstraction concept for building composite objects from their component objects

Association: Associate objects from several independent classes

Main structural distinction


When an association instance is deleted / Participating objects may continue to exist

‫ اللي‬3attrebut ‫ فصار عنا‬.‫ حيتعينو الموظفين‬interview ‫ للناس اللي مقدمين عليها ونتيجة هاي الـ‬interview ‫ وبتعمل‬company ‫عنا‬
‫ جديدة وسميناها‬relationship ‫ بعدين عملنا‬3 ‫ متصل فيها‬relationship ‫ يعني الـ‬ternary ‫ إلهم‬degree ‫ والـ‬b ‫موجودين بالرسمة‬
‫ تانية مباشرة فالحل لهاي المشكلة أنه نتعامل مع الـ‬relationship ‫ بـ‬relationship ‫ بس ما بصير ترتبط الـ‬c ‫ زي بالرسمة‬results_in
aggregation ‫ وهاي هي فكرة الـ‬d ‫ وحدة زي ما عملنا بالرسمة‬entity ‫ على أساس أنهم‬interview ‫ والـ‬job_applicant ‫ والـ‬company

‫ أللهم‬mapping ‫ فيعني لما نعمل‬m:m ‫ هي‬cardinality ‫ وكانت ناتجة عن العالقة اللي بالمربع ؟ ألنه الـ‬d ‫ جدوال بالرسمة‬3 ‫ليش طلع معنا‬
relationship ‫ باسم الـ‬table ‫حنعمل‬

job_applicant ‫ ؟ ألنه وجودها معتمد عللى الشركة والـ‬week entity ‫ طلعت‬interview ‫ليش الـ‬
‫ ؟‬Generalization ‫ و‬Specialization ‫ليش بنعمل‬

Predicate defined subclasses: The subclass is defined through a predicate on the attributes of the superclass.

‫ والتنين إلهم‬instructor ‫ أو‬student ‫ إما بكون فيها‬university ‫عنا الـ‬


‫ تبعه أكبر‬income ‫ إنه الـ‬instructor ‫ بس في شرط بميز الـ‬income
predict defined ‫ بكون‬condition ‫من صفر يعني لما يكون في عنا‬

Attribute defined subclasses: The subclasses in the specialization are all defined by the same attribute of the
superclass.

‫ زي‬subclasses ‫ مخصصة للـ‬attribute ‫الحالة التانية لما يكون في‬


typing speed ‫ أسمه‬attribute ‫ إلهم‬secretary ‫مثال ممكن يكون للـ‬

User defined subclasses Membership in the subclasses is determined at the insertion operation level

‫ لهدف معين مثال زي هون‬specialization ‫ بحدد أنه بده يعمل‬user ‫الـ‬


.home_made ‫ أو‬purchased ‫ ممكن يكون‬cookies ‫عنا الـ‬
Relationship types only partially applicable to the superclass

‫ ممكن ترتبط بـ‬subclass ‫ ألنه الـ‬Specialization ‫كمان بنعمل‬


‫ مرتبطة مع‬purchased ‫ مستقلة عن غيرها زي هون الـ‬relationship
store ‫الـ‬
Relational Algebra

Relation Schema:

A relation schema is used to describe a relation

Denoted by R(A1, A2, A3, …, An), where

R: Relation schema name

A1, …, An: attributes of R ‫ أو األعمدة‬attributes ‫ هي عدد الـ‬degree ‫الـ‬


‫اللي بالجدول‬
The degree of a relation is the number of attributes in a relation schema.

STUDENT (Name, SSN, HomePhone, Address, OfficePhone, Age, GPA)

Degree (STUDENT) = 7

A relation (or relation instance) r of the relation schema R(A1, A2, …, An), denoted by r(R), is a set of n-tuples

r = {t1, t2, …, tm) ‫ وبيحكيلنا أنه‬record ‫ هي القيم المخزنة بالـ‬v ‫الـ‬


. NULL ‫ممكن تكون في قيمة‬
each n-tuple ti is an ordered list of n values,

ti = <v1, v2, …, vn) where each value vi, 1 ≤ i ≤ n, is an element of dom(Ai) or a special null value

Basic Relational Algebra Operations: Is a collection of operators that are used to manipulate entire
relations. The result of each operation is a new relation which can be further manipulated.

. SQL ‫ وهي تعتبر مصدر للـ‬SQL ‫ للـ‬background ‫ ؟ عشان نفهم الـ‬Relational Algebra ‫ليش بنستخدم الـ‬

SELECT Operation

Format: σ selection-condition (R)

σ --- Select (sigma)

Selection- condition ‫هاد الشرط اللي بحدد من خالله حيرجع الريكورد أو ال‬

R ‫الجدول اللي بطبق عليه الشرط‬


Semantics: returns all tuples of relation R that satisfy the selection-condition

Select operation is unary. It applies to a single relation

is used to select a subset of the tuples in a relation that satisfy a selection condition.

‫ وبيرجع منهم بس اللي حقق الشرط وهي‬record ‫ بكون فيها شرط على الـ‬Select ‫الـ‬
)‫ ألنه عشان أطبقه بحتاج لجدول واحد (هدول ركز عليهم الدكتور‬unary ‫عبارة عن‬

Formats of Selection Conditions:

(a) A op v: A is an attribute, op is an operator (=, ≠, <, ≤ , >, ≥), and v is a constant.

Age < = 20, Name = `Bill'

Age < = 20 Attribute (Age) / Operator ( < =) / Value (20)

‫ وبنحط شرط أنها تساوي قيمة معينة أو تكون أكبر أو أصغر منها‬Attribute ‫بكون عنا‬

(b) A op B: A and B are two attributes in R.

Persons(SSN, Name, Birthplace, Residence)

Birthplace = Residence Attribute1(Birthplace ) / Operator ( =) / Attribute 2(Residence)

Attribute ‫هون بالشرط طرفين المقارنة الزم يكونو‬

(c) Combinations of (a) and (b) connected by and, or or not.

Age < = 20 and Birthplace = Residence

‫هون بكون دامج الحالتين اللي فوق‬

Find all students who are 20 years old or younger, and whose birthplace is the same as his/her
residence.
‫ أو أقل باإلضافة إىل إنه المكان الىل انولدو فيه هو نفس مكان اإلقامة فالزم نعمل رش ن‬20 ‫بده نطلع الطالب الىل عمرهم‬
‫طي زي أخر حالة‬ ‫ي‬ ‫ي‬
σ Age ≤ 20 and Birthplace = Residence(Students)

: select ‫ وبما أنه ما في شرط على األعمدة بنحط * بعد‬SQL Statement ‫ لـ‬Relational Algebra ‫تحويل الـ‬

Select * from Students where Age ≤ 20 and Birthplace = Residence;

‫الصفين اللي طلعو تحقق فيهم الشرطين مع بعض‬

) records ‫ (عدد الـ‬2 ‫ ؟‬cardinality ‫شو الـ‬ / ) columns ‫ (عدد الـ‬6 ‫ للجدول؟‬degree ‫شو الـ‬

Commutativity of select:

σ condition-1(σ condition-2(R)) = σ condition-2(σ condition-1(R) = σ condition-1 and condition-2(R)

σ city = “Irbid” AND GPA > 65 (STUDENT) or σ city = “Irbid” (σ GPA > 65 (STUDENT)

‫ هي تبادلية يعني ما بفرق بأي شرط أبلش أول يعني مش مهم الترتيب زي عملية الجمع والضرب من وين ما أبلش نفس الناتج‬select ‫الـ‬

PROJECT Operation

π --- project (pi)

Format: π attribute-list(R),

where attribute-list is a subset of all attributes in R

Semantics: Returns all tuples of relation R but for each tuple, only values under attribute-list are returned

Project removes duplicate tuples automatically.

unary ‫ وبيرجع بس القائمة اللي بحددله اياها وبرضه هاي‬column ‫ بكون فيها شرط على الـ‬Project ‫الـ‬

‫ بشيل التكرار وبس بحط واحد منهم‬records ‫ لما يكون في تكرار بالـ‬:‫مالحظة‬
- Selects certain columns from the table and discards other columns.

π <attribute-list> (<relation-name>)

- degree of resulting relation is equal to the number of attributes in the <attribute-list>

- The number of tuples of the result of project is less than or equal to the number of tuples in the original
relation. (It removes the duplicates)

lest ‫ اللي موجودين بالـ‬attributes ‫ للجدول اللي طلع بالناتج هو عبارة عن عدد الـ‬degree ‫الـ‬
‫ بالجدول األًصلي أو أقل (بكون أقل بحالة وجود‬records ‫ فهي بتساوي عدد الـ‬cardinality ‫أما بالنسبة للـ‬
) records ‫التكرار بالـ‬

Output

π Name,GPA (Students) Relational Algebra

Select Name , GPA from Students; SQL Statement

columns ‫ ألنه بس طلعنا الـ‬2 ‫ طلعت‬cardinality ‫ بالنسبة للـ‬2attribiuts ‫ اللي مكتوبة فيها‬list ‫ ألنه الـ‬2 ‫ طلعت‬degree ‫الـ‬
‫ ) هيك يعتبر ما‬Johan , 3.1( ‫ ) والتاني‬Johan , 3.2( ‫ لو مثال واحد كان‬.‫ فشلناه‬record ‫اللي بده اياهم طلع في تكرار بأحد الـ‬
‫في تكرار‬

If attribute-list-1 ⊆ attribute-list-2, then π attribute-list-1(π attribute-list-2(R)) = π attribute-list-1(R)

The Project operation is not commutative.

‫ يعني اللي برا جزء من اللي جوا‬list2 ‫ هي جزء من‬list1 ‫ جوا بعض عشان تزبط الجملة الزم تكون‬2 project ‫لما يكون عنا‬
.‫ مش تبادلي يعني الترتيب مهم‬project ‫ ومن هون بنعرف أنه الـ‬list1 ‫والناتج حيكون‬

EX: Retrieve all student numbers and names who live in Amman.

π STNO, ST-Name (σ City = “Amman”(STUDENT))

Select STNO , ST-name from Student where City= “Amman”;

STNO , ST-Name ‫ اللي هم‬columns ‫ برا ما حيزبط ألنه باألول حيحدد الـ‬city = Amman ‫لو أجينا هون بدنا نعكس وخلينا الشرط تبع الـ‬
. city ‫ للـ‬column ‫وبعدين بس يجي يشوف إذا المدينة كانت عمان ما حيكون في‬
In complex queries, it becomes necessary to store intermediate results, therefore we should know how to
give names to relations and attributes.

Amman-students = σ city = “Amman” (STUDENT) ‫ وبنخزنها‬sub result ‫لما تصير الجملة كتير معقدة بنجزءها يعني بنعمل‬
relation ‫ واألشي المطلوب بنطبقه على هاي الـ‬relation ‫بـ‬
Result = π STNO, ST-Name (Amman-Students) or

Result(Number,Name) = π STNO, ST-Name (Amman-Students)

Ex: Find the name and GPA of all students who are 20 years old or younger and whose birthplace and
residence are the same.

π Name, GPA(σ Age ≤ 20 and Birthplace=Residence(Students))

Select Name , GPA from Student where Age ≤ 20 and Birthplace=Residence;

RENAME Operation

ρ --- rename (rho)

Format: ρ S(R)

Semantics: Make a copy of relation R and name the copy as S

1- ρ S(R): Rename R only


‫ وأخزن هاي النسخة‬relation ‫ يعني أنه أعمل نسخة من الـ‬Rename ‫الـ‬
: Rename ‫ طرق للـ‬3 ‫ عنا‬. ‫باسم جديد‬
2- ρ S(B1, B2, …, Bn)(R): Rename R and its attributes
Relation ‫ يعني بس بغير اسم الـ‬S ‫ لـ‬R ‫ من‬relation ‫ أغير اسم الـ‬-1
3- ρ (B1, B2, …, Bn)(R): Rename attributes only ‫ باإلضافة إلى أنه أغير اسم الـ‬S ‫ لـ‬R ‫ من‬Relation ‫ أغير اسم الـ‬-2
‫ اللي جواتها بس الزم أحطهم بنفس الترتيب بالجدول األًصلي‬Attributes
attributes ‫ بس بغير اسم الـ‬-3

AS ‫ ؟ باستخدام الـ‬SQL ‫ بالـ‬Rename ‫كيف بنعمل‬

Select * from R AS S ;

Select A1 AS B1 , A2 AS B2 from R AS S;

Select A1 AS B1 , A2 AS B2 from R ;
Set Theoretic Operations
UNION, INTERSECTION, DIFFERENCE.

binary (applied to two relations at a time)

To apply any of these operators to relations, relations should be union-compatible.

Two relations R(A1, A2, …, An) and S(B1, B2, …, Bm) are said to be union-compatible if:

they have the same degree (n = m) and dom(Ai) = dom(Bi) for 1 ≤ i ≤ n.

Both R and S have the same number of attributes and the corresponding attributes have the same domain

union compatible ‫ يعني عشان أطبقهم بحتاج جدولين وتاني شرط أنهم يكونو هدول الجدولين‬Binary ‫ عبارة عن‬3 Operations ‫هدول الـ‬
.‫ المتقابلة الزم يكون متشابه‬attributes ‫ للـ‬datatype ‫ يعني الـ‬domain ‫ (عدد األعمدة) ونفس الـ‬degree ‫يعني إلهم نفس الـ‬

∪ --- union

Format: R1 ∪ R2

Semantics: Returns all tuples that belong to either R1 or R2.


‫ اللي بالجدول األول باإلضافة للي موجودين‬records ‫بياخد الـ‬
Formally: R1 ∪ R2 = { t | t ϵ R1 or t ϵ R2 } is commutative.
‫ موجود بالناتج‬record ‫بالجدول التاني وبيشيل التكرار فأي‬
Condition of union: R1 and R2 must be union compatible. ‫النهائي حيكون تابع للجدول األول أو التاني‬

The union operator removes duplicate tuples automatically.

∪ Output

‫ ألنه بنشيل التكرار‬5 ‫ تساوي‬cardinality ‫ ألحد الجداول أما الـ‬degree ‫ هو نفسه الـ‬3 ‫ للجدول الناتج تساوي‬degree ‫الـ‬

Select * from R1 Union Select * from R2; SQL Statement


Set Difference

- set difference
Format: R1 - R2

Semantics: Returns all tuples that belong to R1 but not R2.

Formally: R1 - R2 = { t | t ϵ R1 and t ϵ R2 } is not commutative.

Set difference also requires union compatibility between R1 and R2

. R2 – R1 ‫ ناتجها غير عن‬R1 – R2 ‫نفس الشروط اللي قبل بس هون بطلعلي الفرق بين الجدولين وهون بفرق الترتيب يعني‬

Output
-

. R2 ‫ ومش موجودين بـ‬R1 ‫ الموجودين بـ‬records ‫بيطلع الـ‬

Select * from R1 Intersect Select * from R2; SQL Statement

1 ‫ بيطلع‬cardinality ‫ والـ‬3 ‫ بيطلع‬degree ‫الـ‬

INTERSECTION

∩ --- set intersection


Format: R1 ∩ R2

Semantics: Returns all tuples that belong to both R1 and R2.

Formally: R1 ∩ R2 = { t | t ϵ R1 and t ϵ R2 } is commutative.

Derivation from existing operators: R1 ∩ R2 = R1 - (R1 - R2) = R2 - (R2 - R1)

. difference ‫ مشتقة من الـ‬Intersection ‫ وعملية الـ‬.‫ المشتركين بين الجدولين‬records ‫برجع الـ‬
Union and Intersection are commutative operations:

R ∪ S = S ∪ R and R∩S=S∩R

Union and Intersection can be applied to any number of relations and both are associative:

R ∪ (S ∪ Q) = (S ∪ R) ∩ Q and R ∩ (S ∩ Q) = (S ∩ R) ∩ Q ‫ تجميعيات وتبديليات‬intersection ‫ والـ‬union ‫عملية الـ‬


difference ‫عكس عملية الـ‬
Difference operator is not commutative:

R - S ≠ S - R in general.

Cartesian Product

X --- Cartesian product

Format: R1 X R2

Semantics: Returns every tuple that can be formed by concatenating a tuple in R1 with a tuple in R2

Binary operation, but the relations on which it is applied do not have to be union compatible.

‫ وهو عبارة عن ضرب ديكارتي بياخد الصف‬. union compatible ‫ بس مش ضروري يكونو‬binary ‫عشان أطبقه بحتاج جدولين يعني‬
‫األول من الجدول األول وبضربه بكل الصفوف اللي بالجدول التاني وبعدين بياخد الصف التاني من الجدول األول وبيضربه بكل الصفوف اللي‬
. concatenation ‫ بيضربه يعني بيعمل بينهم‬..... ‫بالجدول التاني وهيك‬

‫زي ما حكينا فوق أ خد الصف األول بالجدول وضربه بكل الصفوف بالجدول التاني وبعدين الصف التاني وضربه بكل الصفوف والصف التالت‬
. ‫نفس األشي‬

Degree = number of column in R1 + number of column in R2


Cardinality = number of records in R1 X number of records in R1
If R1 and R2 have common attributes, then the full names of these attributes must be used.
Example: Use R.A instead of A

‫ عشان نحدد كل عامود من المتشابهين ألي جدول تابع أو ممكن أحط قبلها اسم الجدول اللي‬rename ‫لو في عامود تكرر أسمه بالجدولين بنعمل‬
. R1.B ‫تابعة أله‬

To prevent identical attribute names from occurring in the same relation schema, R x R is not allowed.
However, R X ρS(R) is allowed.

Commutativity: R1 X R2 = R2 X R1

‫ األصلية‬relation ‫ وبضرب الـ‬rename ‫ ممنوع لكن بقدر أعمل منها نسخة عن طريق الـ‬R X R ‫ ببعضها‬relation ‫ما بقدر أضرب نفس الـ‬
. ‫بهاي النسخة‬

‫عملية الضرب تبديلية بس الناتج حيختلف بالترتيب‬

Given R(A1, A2, …, An) and S(B1, B2, …, Bm)

R X S = Q(A1, A2, …, An, B1, B2, …, Bm)


‫ للجدول الناتج هي عبارة عن عدد األعمدة بالجدول‬degree ‫الـ‬
degree of Q = n + m
.‫األول باإلضافة لعدد األعمدة في الجدول التاني‬
If R1 has N tuples and R2 has M tuples, then ‫ فهي عدد الصفوف بالجدول األول ضرب‬cardinality ‫وبالنسبة للـ‬
.‫عدد الصفوف بالجدول التاني‬
R1 X R2 has N*M tuples

Cartesian product is extremely expensive:


If R1 and R2 are both large, then each relation may need to be scanned many times to perform the Cartesian
product. Writing out the result can be very expensive due to the large size of the result.

EX: Retrieve for each female employee a list of names of her dependents

‫ ؟‬SQL ‫ كيف بنكتبها بالـ‬، female ‫ يكونو‬employee ‫ بس بشرط هدول الـ‬employee ‫بده نرجع اسماء أوالد الـ‬

Select Fname , Lname , Dependent_name ferom Employee , Dependent where


Employee.ssn = Dependent.essn and Sex = “f” ;

FEMALE-EMPS = σ Sex = “F” (EMPLOYEE)

EMP-NAMES = π Fname,Lname,SSN(FEMALE-EMPS)

EMP-DEPENDENTS = EMP-NAMES X DEPENDENT

ACTUAL-DEP = σ SSN = ESSN (EMP-DEPENDENTS)

RESULT = π Fname,Lname,Dependent-name(ACTUAL-DEP)
sex= f ‫ اللي الـ‬employees ‫ وطلع كل الـ‬employee ‫ على جدول الـ‬select ‫ أول أشي عمل‬-1 :‫بدنا نمشي خطوة خطوة كيف حلها‬

EMP- ‫ ) وخزنا الناتج في‬Fname,Lname,SSN ( ‫ أعمدة‬3 ‫ وحدد هدول الـ‬project ‫ الناتج اللي طلع معنا من أول خطوة عمل عليه‬-2
NAMES

dependent ‫ فبيطلع معنا جدول كبير بيحتوي على كل بيانات الـ‬dependent ‫ والـ‬EMP-NAMES ‫ بين الـ‬cartesian prodact ‫ عملنا‬-3
‫ بس مضروبين ببعض‬female employees ‫ للـ‬Fname,Lname,SSN ‫باالضافة للـ‬

ssn = essn ‫ عن طريق الشرط أنه‬employee ‫ تابع ألي‬dependent ‫ بنحدد كل‬-4

. ‫ أعمدة اللي بدنا اياهم‬3 ‫ الناتج اللي بيطلع معنا بنحدد منه الـ‬-5

Example: Find the names of each employee and his/her manager

.‫ تبعه‬manager ‫ ومين الـ‬employee ‫بده نطلع اسم الـ‬

Select Name , Manager from Employee , Department where Dept-Name = Name;

A relational algebra expression is:

π Employees.Name, Departments.Manage (σ Employees.Dept_Name = Departments.Name (Employees X Departments))


A simplified version (don't use full name when you don't have to):

π Employees.Name, Manager (σ Dept_Name =Departments.Name (Employees X Departments))


‫ أنه اسم الـ‬records ‫ بين الجدولين وبينتج عنا جدول كبير بعدين بعمل شرط على الـ‬Cartesian product ‫ أول اشي بنعمل‬:‫الجملة األولى‬
‫ تبعه بنعمل‬manager ‫ ومين الـ‬employee ‫ بعد ما حددنا كل‬department ‫ بساوي االسم بجدول الـ‬employee ‫ بجدول الـ‬department
. ‫ على الناتج وبنحدد األعمدة المطلوبة‬project

attribute ‫ قبل اسم الـ‬table ‫والجملة التانية نفس األولى بس بسطها ألنه االسم مش نفسه فعادي مش ضروري نحط اسم الـ‬

Use assignment operator ( = ) to save the intermediate result into a temporary relation

Example: The following expression

π Employees.Name, Manager (σ Dept_Name =Departments.Name (Employees X Departments))

is equivalent to the following series of expressions:


‫السؤال اللي قبل بنقدر بدل ما نكتبه بجملة وحدة نعمله‬
TEMP1 = Employees X Departments ‫أجزاء والترتيب بكون نفس الترتيب اللي مكتوب‬
‫بالشرح أول الصفحة‬
TEMP2 = σ Dept_Name=Departments.Name(TEMP1)

RESULT =π Employees.Name, Manager (TEMP2)

EX: Find the names of all students who have the highest GPA

. GPA ‫بده نطلع اسماء الطالب اللي إلهم أعلى‬

Step 1: Find the GPAs that are not the highest

TEMP1 = π Students.GPA(σ Students.GPA < S2.GPA (Students X ρ S2(Students)))

‫ عشان أعرف‬table ‫أول اشي بعمل نسخة من الـ‬


‫ وبعمل بين الـ‬S2 ‫ اللي هي‬GPA ‫أقارن بين الـ‬
cartesian ‫ والنسخة اللي عملناها‬Student
‫ وبضربهم ببعض عشان أقارن وبطلع‬product
TEMP1 ‫ وخزنا الناتج بـ‬GPA ‫الطالب إلي إلهم أقل‬
Step 2: Find the highest GPA

TEMP2 = π GPA(Students) - TEMP1

Step 3: Find the names of students who have the highest GPA

RESULT = π Name(σ Students.GPA = TEMP2.GPA (Students X TEMP2))

‫ وهيك بيطلع معنا‬Student ‫ من جدول الـ‬TEMP1 ‫ الناتج اللي طلع معنا بأول خطوة هو األقل وأحنا بدنا األعلى بنطرح الـ‬:‫الخطوة الثانية‬
GPA ‫ بيحتوي على عامود فيه أعلى‬TEMP2 ‫ سميناه‬table

. GPA ‫ وبطلع أسماء الطالب إلي إلهم أعلى‬TEMP2 ‫ والـ‬student ‫ بين الـ‬cartesian product ‫ بنعمل‬:‫الخطوة الثالثة‬

Join

∞ --- join

Format: R1 ∞ join-condition R2

Semantics: Returns all tuples in R1 X R2 which satisfy the join condition

Derivation from existing operators: R1 ∞ join-condition R2 = σ join-condition(R1 X R2)

Format of join condition: ‫ بضرب الجدولين ببعض وبعدين‬cartesian product ‫ هو عبارة عن‬join ‫الـ‬
:‫ اللي حققو الشرط بالنسبة للشرط في نوعين‬records ‫برجعلي الـ‬
R1.A op R2.B
‫ أما لو ما كان في شرط بكون اسمه‬theta join ‫إذا كان الشرط موجود هاد بنسميه‬
R1.A1 op R2.B1 and R1.A2 op R2.B2 . . . natural ‫ أو‬equijoin

Tuples whose join attributes are NULL do not appear in the result

.‫ ما بيطلعو بالنتيجة النهائية‬null ‫ اللي فيهم‬records ‫الـ‬

Ex: Find the names of all employees and their department locations
join ‫ تبع الـقسم بما أنه االشي المطلوب من جدولين فهون عرفنا انه حنعمل‬location ‫بده نطلع اسم الموظف باإلضافة للـ‬

π Employees.Name, Location (Employees ∞ Dept-Name = Departments.Name Departments)

: ‫ ؟ بطريقتين‬SQL ‫كيف بنحولها لـ‬

Select Employees.Name, Location from Employees Inner join Department where


Dept-Name = Departments.Name;

Select Employees.Name, Location from Employees , Department where Dept-Name = Departments.Name;

‫ أنه االسم بجدول‬Dept-Name = Departments.Name ‫ اللي حققو الشرط تبع‬records ‫اللي عملناه أنه ضربنا الجدولين ببعض وأخدنا الـ‬
‫ الناتج اللي طلع معنا بنحدد منه أنه بدنا هدول العامودين‬null ‫ وما بناخد الـ‬employee ‫ نفس االسم بجدول الـ‬department ‫الـ‬
Employees.Name, Location

.‫ تبعهم موجود بالجدولين‬department ‫ ألنه اسم الـ‬Mary ‫ و‬Johan ‫فأعطانا‬

Ex: Find the names of all employees who earn more than his/her manager

‫ اللي راتبهم‬employees ‫بده نطلع اسماء الـ‬


‫أعلى من راتب مديرهم‬

π Employees.Name (Employees ∞ Employees.Manager-SSN = EMP.SSN and Employees.Salary > EMP.Salary ρ EMP(Employees))

employee ‫ اللي بجدول الـ‬Managerssn ‫ والشرط أنه الـ‬EMP ‫ بينه وبين نسخة تانية منه سميناها‬join ‫ وعملنا‬Employee ‫أخدنا جدول الـ‬
EMP ‫ بجدول الـ‬salary ‫ أكبر من الـ‬employee ‫ بجدول الـ‬Salary ‫ وكمان أنه الـ‬EMP ‫ اللي بجدول الـ‬SSN ‫تساوي‬

:‫توضيح للحل‬

Employee EMP

‫أول خطوة عملنا النسخة من الجدول‬


‫‪Employee.‬‬ ‫‪Employee.‬‬ ‫‪Employee.‬‬ ‫‪Employee.‬‬ ‫‪EMP.SSN‬‬ ‫‪EMP.Name EMP.Salary EMP.‬‬
‫‪SSN‬‬ ‫‪Name‬‬ ‫‪Salary‬‬ ‫‪Manager-SSN‬‬ ‫‪Manager-SSN‬‬
‫‪123456789‬‬ ‫‪Johan‬‬ ‫‪34k‬‬ ‫‪234567891‬‬ ‫‪234567891 Bill‬‬ ‫‪40k‬‬ ‫‪Null‬‬

‫‪456789123‬‬ ‫‪Mike‬‬ ‫‪41k‬‬ ‫‪345678912‬‬ ‫‪345678912 Mary‬‬ ‫‪38k‬‬ ‫‪Null‬‬

‫بجدول الـ ‪ employee‬شو هم الـ ‪ manager-ssn‬اللي موجودين بجدول الـ ‪ EMP‬بالـ ‪ column‬تبع الـ ‪ SSN‬كان عبارة عن الـ ‪record‬‬
‫األول والرابع ودمجناهم مع الصف الثالث والرابع من جدول الـ ‪ EMP‬وهيك صار مقابل كل ‪ employee‬الـ ‪ manager‬تبعه‬

‫هسا بنحكي مين الموظف اللي راتبه أعلى من راتب مديره يعني أي ‪ record‬فيها ‪ employee.salary > EMP.salary‬يعني الصف األخير‬
‫فباخد اسم ‪ Mike‬وبحطه بجدول لحاله‪.‬‬

‫‪Equijoin‬‬
‫‪Definition: A join is called an equijoin if only equality operator is used in all join conditions.‬‬

‫بنحكي عن الـ ‪ join‬إنه ‪ equijoin‬إذا كان شرط الـ ‪ join‬عبارة عن مساوة‬

‫الزم يكون في عامود مشترك بين الجدولين على أساسه بعمل الـ ‪ join‬بروح على عامود الـ ‪ B‬بالجدول األول وباخد أول صف فيه طلع ‪ b‬بقارن‬
‫هاي الـ ‪ b‬بالقيم اللي موجودة بالجدول التاني برضه بالعامود ‪ B‬فبالقي أول قيمة ‪ b‬وباخد الصفكامل ومن هون طلع معنا أول صف بالجدول‬
‫الناتج وبعدين بقارن القيمة اللي بالصف التاني اللي هي برضه ‪ b‬فباخد الصف التاني من الجدول األول وبدمجه مع الصف األول من الجدول‬
‫التاني وهيك نتج الصف التاني بالجدول الناتج وأخر اشي الـ ‪ c‬بالقي في ‪ c‬بالصف التاني فباخد الصف التالت مع الصف التاني وهيك بنتج أخر‬
‫صف بالجدول ‪.‬‬

‫لو حكالنا كم عدد األعمدة إذا استخدمنا ‪ natural equijoin‬بنشيل العامود المكرر اللي هو ‪ B‬وبيطلعو ‪3‬‬

‫أما لو طلب عدد األعمدة لما نستخدم الـ ‪ equijoin‬بيضل تكرار األعمدة بكونو ‪.4‬‬

‫وطبعا بقيم التكرار اللي بالـ ‪. records‬‬

‫‪Most joins in practice are equijoins.‬‬


Natural Join
Definition: A join between R1 and R2 is a natural join if There is an equality comparison between every pair of
identically named attributes from the two relations.

Among each pair of identically named attributes from the two relations, only one remains in the result.

Natural join is denoted by ∞ with no join conditions explicitly specified.

‫ العامود المشترك ما بكرره‬join ‫ بكون لما يكون في عنا جدولين فيهم عامود مشترك وبكون إلهم نفس االسم وبس أعمل الـ‬natural join ‫الـ‬
.‫ومش ضروري نحط الشرط هون عشان الهم نفس االسم‬

Ex: R1(A, B, C) ∞ R2(A, C, D) has attributes (A, B, C, D) in the result

Questions: How to express natural join in terms of equijoin and other relational operator?

R1 ∞ R2 = π R1.A, B, R2.C, D (R1∞ R1.A=R2.A and R1.C= R2.C R2)

‫ كتبناهم بس مرة وحدة‬A , C ‫ مش ضروري نحط شرط وبنالحظ بالناتج ما كررنا األعمدة المشتركة اللي هم‬natural join ‫بما أنه حعمل‬

.‫ على الناتج‬project ‫ العادي حط شرط المساوة وبعدين عمل‬equijoin ‫وبعد المساوة شرح كيف عملناها باستخدام الـ‬

The relational algebra is a set of expressions as defined below: A relation is an expression.

If E1 and E2 are expressions, so are

σ P(E1), π A(E1), ρ S(E1), E1 ∪ E2, E1-E2, E1 X E2


That is, any expression that can be formed from base relations and the six relational operators is a relational
algebra expression

Outerjoin

∞O --- outer join


records ‫ اللي تساوت عندهم األعمدة باإلضافة للـ‬records ‫ العادي باخد الـ‬join ‫زي الـ‬
Format: R1 ∞O R2 ‫اللي بالجدول األول و التاني‬

Semantics: like join except it retains dangling tuples from both R1 and R2 and it uses null to fill out missing
entries
‫‪∞LO --- left outer join‬‬
‫بياخد الـ ‪ records‬اللي تساوت عندهم األعمدة باإلضافة للـ ‪ records‬اللي بالجدول األول‬
‫‪Format: R1 ∞LO R2‬‬ ‫بمعنى تاني اللي موجودين على يسار الـ ‪join‬‬

‫‪Semantics: like outerjoin but retains only dangling tuples of the relation on the left‬‬

‫‪∞RO --- right outer join‬‬


‫بياخد الـ ‪ records‬اللي تساوت عندهم األعمدة باإلضافة للـ ‪ records‬اللي بالجدول التاني‬
‫‪Format: R1 ∞RO R2‬‬ ‫بمعنى تاني اللي موجودين على يمين الـ ‪join‬‬

‫‪Semantics: like outerjoin but retains only dangling tuples of the relation on the right‬‬

‫‪Outer join‬‬

‫‪Right Outer join‬‬ ‫‪Left Outer join‬‬

‫بنالحظ لما عملنا الـ ‪ left‬وأخدنا الـ ‪ records‬اللي موجودين على يسار ومش موجودين باليمين بنحط القيم لألعمدة اللي جاية من جدول الـ‬
‫‪ right‬بنكتبهم ‪ null‬والعكس بالحالة التانية‬
Aggregate Functions & Grouping
Well known Aggregate Functions: SUM, AVERAGE, MAXIMUM, MINIMUM, COUNT

Format:  (Script F)

<grouping attributes>  <function list> (R)

<grouping attributes> is a list of attributes in R

. grouping ‫ اللي بحددهم من الجدول وحعمل عليهم‬attributs ‫ هم الـ‬grouping attributs ‫الـ‬

<function list> is a list of (<function> <attribute>) pairs

‫ على أساسهم‬grouping ‫ اللي حعمل الـ‬functions ‫الـ‬

<function> is one of aggregate functions

<attribute> is an attribute in R

EX: Retrieve each department number, the number of employees in the department, and their average
salary:

salary ‫ تبع الـ‬average ‫ اللي فيه باإلضافة للـ‬employees ‫ كم عدد الـ‬department ‫بده نطلع لكل‬

DNO  COUNT( SSN), AVERAGE(SALARY) (EMPLOYEE)


‫ وطلعنا عدد الموظفين ومعدل رواتبهم‬dno ‫ على أساس الـ‬grouping ‫عملنا الـ‬
‫‪Division Operator‬‬

‫‪A‬‬ ‫‪B1‬‬ ‫‪A/B1‬‬


‫‪sno‬‬ ‫‪Pno‬‬ ‫‪Pno‬‬ ‫‪Sno‬‬ ‫بدنا نقسم ‪ A / B1‬العامود المشترك بينهم هو‬
‫‪S1‬‬ ‫‪P1‬‬ ‫‪P2‬‬ ‫‪S1‬‬ ‫‪ pno‬فباخد القيمة اللي موجودة فيه وبشوف‬
‫‪S1‬‬ ‫‪P2‬‬ ‫شو اللي بقابلها بالـ ‪ sno‬ولو كان في تكرار‬
‫‪S2‬‬
‫‪S1‬‬ ‫‪P3‬‬ ‫في قيم الـ ‪ sno‬ما باخدها‬
‫‪S1‬‬ ‫‪P4‬‬ ‫‪S3‬‬
‫‪S2‬‬ ‫‪P1‬‬ ‫‪S4‬‬
‫‪S2‬‬ ‫‪P2‬‬
‫‪S3‬‬ ‫‪P2‬‬
‫‪S4‬‬ ‫‪P2‬‬
‫‪S4‬‬ ‫‪P4‬‬

‫‪B2‬‬ ‫‪A/B2‬‬
‫بدنا نقسم ‪ A / B2‬باخد القيمة اللي موجودة‬
‫‪sno‬‬ ‫‪Pno‬‬ ‫‪Pno‬‬ ‫‪Sno‬‬ ‫في ‪ pno‬مع بعض التنتين وبشوف شو اللي‬
‫‪S1‬‬ ‫‪P1‬‬ ‫‪P2‬‬ ‫‪S1‬‬ ‫بقابلها بالـ ‪ sno‬ولو كان في تكرار في قيم‬
‫‪S1‬‬ ‫‪P2‬‬
‫‪P4‬‬ ‫‪S4‬‬ ‫الـ ‪ sno‬ما باخدها‬
‫‪S1‬‬ ‫‪P3‬‬
‫‪S1‬‬ ‫‪P4‬‬
‫‪S2‬‬ ‫‪P1‬‬
‫‪S2‬‬ ‫‪P2‬‬
‫‪S3‬‬ ‫‪P2‬‬
‫‪S4‬‬ ‫‪P2‬‬
‫‪S4‬‬ ‫‪P4‬‬

‫‪A/B3‬‬ ‫‪B3‬‬
‫‪sno‬‬ ‫‪Pno‬‬ ‫بدنا نقسم ‪ A / B3‬باخد القيمة اللي موجودة‬
‫‪Pno‬‬ ‫‪Sno‬‬
‫‪S1‬‬ ‫‪P1‬‬ ‫في ‪ pno‬مع بعض التالت وبشوف شو اللي‬
‫‪S1‬‬ ‫‪P2‬‬ ‫‪P1‬‬ ‫‪S1‬‬ ‫بقابلها بالـ ‪ sno‬ولو كان في تكرار في قيم‬
‫‪S1‬‬ ‫‪P3‬‬ ‫‪P2‬‬ ‫الـ ‪ sno‬ما باخدها‬
‫‪S1‬‬ ‫‪P4‬‬
‫‪P4‬‬
‫‪S2‬‬ ‫‪P1‬‬
‫‪S2‬‬ ‫‪P2‬‬
‫‪S3‬‬ ‫‪P2‬‬
‫‪S4‬‬ ‫‪P2‬‬
‫‪S4‬‬ ‫‪P4‬‬

You might also like