This chapter covers the fundamental concepts of databases and database management systems. It starts by defining data, databases, and DBMS, then explains the characteristics and advantages of the database approach compared to the traditional file-processing approach.
يتناول هذا الفصل المفاهيم الأساسية لقواعد البيانات وأنظمة إدارتها. يبدأ بتعريف البيانات وقواعد البيانات وأنظمة إدارة قواعد البيانات (DBMS)، ثم ينتقل إلى شرح خصائص منهج قواعد البيانات ومميزاته مقارنة بالنظام التقليدي.
Data: Known facts about something that can be recorded and have an implicit meaning in the user's environment.
Examples:
البيانات: حقائق معروفة عن شيء ما يمكن تسجيلها ولها معنى ضمني في بيئة المستخدم.
أمثلة:
Database (DB): A collection of related data + meta-data.
Mini-world Example: A university database containing:
قاعدة البيانات: مجموعة من البيانات المترابطة + البيانات الوصفية (meta-data).
مثال (عالم مصغر): قاعدة بيانات جامعية تحتوي على:
Meta-Data: Data about the data. Provides the definition or description of the database structure. This definition is stored in the database catalog.
DB Definition contains:
Database Catalog: A central location where data definitions (meta-data) are stored.
البيانات الوصفية: بيانات عن البيانات. توفر تعريف أو وصف لهيكل قاعدة البيانات. يتم تخزين هذا التعريف في فهرس قاعدة البيانات.
تعريف قاعدة البيانات يحتوي على:
فهرس قاعدة البيانات: موقع مركزي يتم فيه تخزين تعريفات البيانات.
DBMS (DataBase Management System): A collection of programs/software system that enables users to create and maintain a database.
Examples: Oracle, MS Access, MS SQL Server, IBM DB2, MySQL, Ingres, Informix
نظام إدارة قواعد البيانات: مجموعة من البرامج/نظام برمجي يمكّن المستخدمين من إنشاء وصيانة قاعدة بيانات.
أمثلة: Oracle, MS Access, MS SQL Server, IBM DB2, MySQL
Database System (DBS): Includes the DBMS software together with the data itself. Sometimes, the application programs are also included.
Application Programs: Programs written in a specific language (3GL) that includes commands required to manipulate the database.
نظام قاعدة البيانات: يشمل برنامج DBMS مع البيانات نفسها. وأحياناً تُضمّن برامج التطبيقات أيضاً.
برامج التطبيقات: برامج مكتوبة بلغة محددة (3GL) تتضمن أوامر مطلوبة للتعامل مع قاعدة البيانات.
The main characteristics of the database approach versus the file-processing approach are:
الخصائص الرئيسية لمنهج قواعد البيانات مقارنة بمنهج معالجة الملفات:
People whose jobs involve the day-to-day use of large databases:
الأشخاص الذين تتضمن وظائفهم الاستخدام اليومي لقواعد البيانات الكبيرة:
DBMS has ability to represent complex relationships among the data.
Example: Relationship between students and courses, between courses and their sections, etc.
DBMS لديه القدرة على تمثيل العلاقات المعقدة بين البيانات.
مثال: العلاقة بين الطلاب والمقررات، بين المقررات وشُعبها، إلخ.
Data should satisfy certain integrity constraints:
Constraints identified at database design time. Some enforced by DBMS, others by application programs.
يجب أن تستوفي البيانات قيوداً معينة للسلامة:
القيود تُحدد وقت تصميم قاعدة البيانات. بعضها يُفرض بواسطة DBMS، وبعضها بواسطة برامج التطبيقات.
DBMS provides facility to recover from hardware and software failures through its backup and recovery sub-system.
Scenario:
DBMS يوفر إمكانية الاستعادة من أعطال الأجهزة والبرمجيات من خلال نظام النسخ الاحتياطي والاستعادة.
سيناريو:
Beyond the main advantages, there are additional implications of using the database approach:
بالإضافة إلى المزايا الرئيسية، هناك آثار إضافية لاستخدام منهج قواعد البيانات:
Despite the advantages of using a DBMS, there are situations where it may not be advisable:
على الرغم من مزايا استخدام DBMS، هناك حالات قد لا يكون فيها ذلك مستحسناً:
A DBMS may involve unnecessary overhead when:
قد يتضمن DBMS تكاليف إضافية غير ضرورية عندما:
| Use DBMS When | Use File System When |
|---|---|
| Multiple users need access | Single user application |
| Complex data relationships | Simple, well-defined data |
| Data changes frequently | Data is static |
| Security is important | Real-time requirements are strict |
This chapter covers the architectural concepts of database systems, including different data models, the difference between Schema and Instance, Three-Schema Architecture, and Data Independence.
يتناول هذا الفصل المفاهيم المعمارية لأنظمة قواعد البيانات، بما في ذلك نماذج البيانات المختلفة، الفرق بين Schema و Instance، هندسة الثلاث مستويات، واستقلالية البيانات.
Data Model: A set of concepts that can be used to describe the structure of a database (data, relationships between data, and constraints).
Data Model Operations:
A data model is the main tool for providing data abstraction by hiding storage details.
نموذج البيانات: مجموعة من المفاهيم المستخدمة لوصف هيكل قاعدة البيانات (البيانات، العلاقات، والقيود).
عمليات نموذج البيانات:
نموذج البيانات هو الأداة الرئيسية لتوفير تجريد البيانات عن طريق إخفاء تفاصيل التخزين.
Examples: Entity-Relationship Model, Object-Oriented Model
أمثلة: نموذج الكيان-العلاقة (ER)، النموذج الكائني التوجه (OO)
Examples: Relational, Hierarchical, Network Data Models
أمثلة: النموذج العلائقي، الهرمي، الشبكي
Database Schema: The description of a database. Includes descriptions of the database structure and the constraints.
مخطط قاعدة البيانات (Schema): وصف لقاعدة البيانات يتضمن وصف الهيكل والقيود.
Database Instance (State): The actual data stored in a database at any particular moment in time. Also called Extension.
نسخة قاعدة البيانات (Instance/State): البيانات الفعلية المخزنة في أي لحظة معينة. تسمى أيضاً Extension.
| Schema | Instance |
|---|---|
| Description of structure | Actual stored data |
| Does not change frequently | Changes with every update |
| Called Intension | Called Extension |
| Example: STUDENT(ID, Name, Age) | Example: (123, "Ahmed", 20) |
A convenient tool for visualizing schema levels in a DB system.
Proposed to support:
أداة مريحة لتصور مستويات المخططات في نظام قاعدة البيانات.
مقترحة لدعم:
Describes: That part of the database relevant to each user
يصف: الجزء من قاعدة البيانات المتعلق بكل مستخدم
Describes: WHAT data is stored, relationships, and constraints
يصف: ماذا يُخزن، العلاقات، والقيود
Describes: HOW data is stored
يصف: كيف يتم تخزين البيانات
Data Independence: The capacity to change the schema at one level without having to change the schema at the next higher level.
Capacity to change the conceptual schema without changing external schemas or application programs.
Capacity to change the internal schema without changing the conceptual schema.
استقلالية البيانات: القدرة على تغيير المخطط في مستوى واحد دون الحاجة لتغيير المخطط في المستوى الأعلى.
القدرة على تغيير المخطط المفاهيمي دون تغيير المخططات الخارجية أو برامج التطبيقات.
القدرة على تغيير المخطط الداخلي دون تغيير المخطط المفاهيمي.
Examples: CREATE TABLE, ALTER TABLE, DROP TABLE, CREATE VIEW
أمثلة: CREATE TABLE, ALTER TABLE, DROP TABLE, CREATE VIEW
Examples: SELECT, INSERT, UPDATE, DELETE
أمثلة: SELECT, INSERT, UPDATE, DELETE
| High-Level (Non-Procedural) | Low-Level (Procedural) |
|---|---|
| Set-oriented | Record-at-a-time |
| Specify WHAT to retrieve | Specify HOW to retrieve |
| Declarative Languages | Includes looping constructs |
| Example: SQL | Example: Embedded SQL in C |
A DBMS is a complex software system with multiple components that work together to manage the database.
نظام إدارة قواعد البيانات هو نظام برمجي معقد يتكون من مكونات متعددة تعمل معاً لإدارة قاعدة البيانات.
The catalog stores:
يخزن الكتالوج:
Most DBMSs have database utilities that help the DBA manage the database system:
معظم أنظمة DBMS لديها أدوات مساعدة تساعد مدير قاعدة البيانات في إدارة النظام:
Database systems can be organized using different architectures based on how components are distributed.
يمكن تنظيم أنظمة قواعد البيانات باستخدام هندسات مختلفة بناءً على كيفية توزيع المكونات.
| Aspect | Two-Tier | Three-Tier |
|---|---|---|
| Complexity | Simpler | More complex |
| Scalability | Limited | Better |
| Security | Direct DB access | DB hidden from client |
| Use Case | Small applications | Web/Enterprise apps |
The Entity-Relationship (ER) Model is a high-level conceptual data model used to describe data as entities, attributes, and relationships between entities. The overall logical structure of a database can be expressed graphically by an ER diagram.
The ER model describes data as:
نموذج الكيان-العلاقة (ER Model) هو نموذج بيانات مفاهيمي عالي المستوى يستخدم لوصف البيانات ككيانات وسمات وعلاقات بين الكيانات. يمكن التعبير عن البنية المنطقية الشاملة لقاعدة البيانات بيانياً من خلال مخطط ER.
يصف نموذج ER البيانات كـ:
Entity: A "thing" in the real world such as an object, subject, or event that can be identified in the Universe of Discourse (UoD) and is distinguishable from other similar objects.
Entities are important things in the miniworld described in the database.
الكيان: "شيء" في العالم الحقيقي مثل كائن أو موضوع أو حدث يمكن تحديده في نطاق الخطاب (UoD) ويمكن تمييزه عن الأشياء المماثلة الأخرى.
الكيانات هي أشياء مهمة في العالم المصغر الموصوف في قاعدة البيانات.
Physical Entity: Tangible things like Building, Car, Person
Conceptual Entity: Abstract concepts like Student, Employee, Course
كيان مادي: أشياء ملموسة مثل مبنى، سيارة، شخص
كيان مفاهيمي: مفاهيم مجردة مثل طالب، موظف، مقرر
Entity Type: Describes the schema/intension for a set of entities which have the same attributes. Represented as a rectangle box in ER diagram.
Entity Set: The collection of all entities of a particular entity type at any point in time (extension/instance).
Each Entity Type is described by its NAME and a list of its attributes.
نوع الكيان: يصف المخطط/القصد لمجموعة من الكيانات التي لها نفس السمات. يُمثل كمربع مستطيل في مخطط ER.
مجموعة الكيانات: مجموعة كل الكيانات من نوع كيان معين في أي وقت (الامتداد/النسخة).
يوصف كل نوع كيان باسمه وقائمة سماته.
Strong Entity:
Weak Entity:
الكيان القوي:
الكيان الضعيف:
Attributes: Important features of an entity that describe its properties. They are relevant for the miniworld and have to be recorded in the database.
A particular entity will have a value for each of its attributes.
COURSE entity has attributes: Course Code, Course Title, Course Description, Number of Credits
السمات: خصائص مهمة للكيان تصف صفاته. وهي ذات صلة بالعالم المصغر ويجب تسجيلها في قاعدة البيانات.
سيكون لكل كيان معين قيمة لكل سمة من سماته.
Key Attribute: One or more attributes that uniquely identifies each entity of the entity set. Also called uniqueness constraint.
STUDENT(StudID, Name, SSN, GPA) - StudID and SSN are keys because each has unique value. Name is NOT a key because multiple students can have the same name.
السمة المفتاحية: سمة واحدة أو أكثر تحدد كل كيان بشكل فريد. تسمى أيضاً قيد التفرد.
Domain/Value Set: Set of possible values that may be assigned to the attribute. Denoted by Dom(attribute).
Cardinality of a domain: The number of possible values that the domain can contain.
Age for EMPLOYEE: Domain = integers between 20 and 60. Cardinality = 41 values (60-20+1)
SSN: Domain = string of format ddd-dd-dddd
Major: Domain = {CS, SI, CIS, IT, ...}
مجموعة القيم (النطاق): مجموعة القيم الممكنة التي يمكن تعيينها للسمة. يُرمز لها بـ Dom(السمة).
عدد عناصر النطاق: عدد القيم الممكنة التي يمكن أن يحتويها النطاق.
Null Value: Used when attribute value is unknown (not available) or not applicable.
Important: NULL is NOT 0 (zero) and NOT a blank string!
Date of death is not applicable for live patients
Grade may be null for students who did not complete the course
القيمة الفارغة (Null): تُستخدم عندما تكون قيمة السمة غير معروفة أو غير قابلة للتطبيق.
مهم: NULL ليست صفر (0) وليست سلسلة فارغة!
Simple (Atomic): Cannot be further divided. Example: Age, Sex, Title
Composite: Can be divided into smaller subparts with independent meanings.
Single-valued: Have only one value. Example: Gender = F
Multi-valued: Have a set of values. Notation: Double oval
Note: In relational model, attributes are always single-valued (atomic)!
Stored: Actually stored in the database
Derived: Can be calculated from other stored attributes. Notation: Dashed oval
بسيطة (ذرية): لا يمكن تقسيمها. مثال: العمر، الجنس
مركبة: يمكن تقسيمها إلى أجزاء أصغر بمعانٍ مستقلة.
أحادية القيمة: لها قيمة واحدة فقط
متعددة القيم: لها مجموعة من القيم. الرمز: شكل بيضاوي مزدوج
مخزنة: تُخزن فعلياً في قاعدة البيانات
مشتقة: يمكن حسابها من سمات أخرى. الرمز: شكل بيضاوي متقطع
Relationship Type: An association among two or more entity types. A relationship relates entities with a specific meaning.
Relationship Set: A set of relationship instances of a particular relationship type.
Notation: Diamond shape connected to entity rectangles by lines.
Students Register for Subjects
Employee Works_For Department
Employee Manages Department
Supplier Supplies Parts To Projects
نوع العلاقة: ارتباط بين نوعين أو أكثر من الكيانات. العلاقة تربط الكيانات بمعنى محدد.
مجموعة العلاقات: مجموعة من حالات العلاقة من نوع علاقة معين.
الرمز: شكل ماسي متصل بمستطيلات الكيانات بخطوط.
Degree: The number of participating entity types in a relationship.
Binary: Employee WORKS_FOR Department (2 entities)
Ternary: Supplier SUPPLIES Parts TO Projects (3 entities)
الدرجة: عدد أنواع الكيانات المشاركة في العلاقة.
Recursive Relationship: When the same entity type participates more than once in the same relationship type in different "roles".
SUPERVISION relationship: EMPLOYEE supervises EMPLOYEE
Role 1: Supervisor (the employee who supervises)
Role 2: Supervisee (the employee being supervised)
العلاقة التكرارية: عندما يشارك نفس نوع الكيان أكثر من مرة في نفس نوع العلاقة بأدوار مختلفة.
Relationship types can also have attributes, similar to entity types.
"Since" attribute of MANAGES can go to EMPLOYEE or DEPARTMENT
"StartDate" of WORKS_FOR can go to EMPLOYEE (N-side)
"Qty" of KEEPS (Store M:N Product) must stay with relationship
يمكن أن يكون لأنواع العلاقات سمات أيضاً، مشابهة لأنواع الكيانات.
Cardinality Ratio: Specifies the number of relationship instances that an entity can participate in.
An entity in A is associated with at most ONE entity in B, and vice versa.
Employee MANAGES Department: One employee manages at most one department, and one department has at most one manager.
An entity in A is associated with ANY NUMBER of entities in B, but an entity in B is associated with at most ONE entity in A.
Department EMPLOYS Employees: One department can have many employees, but each employee works for only one department.
An entity in A is associated with ANY NUMBER of entities in B, and vice versa.
Employee WORKS_ON Project: An employee can work on several projects, and a project can have several employees.
نسبة العدد: تحدد عدد حالات العلاقة التي يمكن أن يشارك فيها الكيان.
كيان في A مرتبط بكيان واحد على الأكثر في B، والعكس صحيح.
كيان في A مرتبط بأي عدد من الكيانات في B، لكن كيان في B مرتبط بكيان واحد على الأكثر في A.
كيان في A مرتبط بأي عدد من الكيانات في B، والعكس صحيح.
Participation Constraint: Specifies whether an entity MUST participate in a relationship instance or can exist independently.
Every entity of the entity type MUST participate in the relationship.
Notation: Double line connecting entity to relationship
Every employee MUST work for a department (total participation of EMPLOYEE in WORKS_FOR)
Every department MUST have a manager (total participation of DEPARTMENT in MANAGES)
Entities CAN exist independently without participating in the relationship.
Notation: Single line connecting entity to relationship
Not every employee is a manager (partial participation of EMPLOYEE in MANAGES)
قيد المشاركة: يحدد ما إذا كان الكيان يجب أن يشارك في حالة علاقة أو يمكنه الوجود بشكل مستقل.
كل كيان من نوع الكيان يجب أن يشارك في العلاقة.
الرمز: خط مزدوج يربط الكيان بالعلاقة
يمكن للكيانات أن توجد بشكل مستقل دون المشاركة في العلاقة.
الرمز: خط مفرد يربط الكيان بالعلاقة
Identifying Relationship: A relationship between a strong entity and a weak entity type.
COURSE (strong) --- HAS --- SECTION (weak)
Section's full key = Course_No + Section_No + Year + Semester
العلاقة المحددة: علاقة بين كيان قوي وكيان ضعيف.
| Symbol | Meaning |
|---|---|
| Rectangle | Entity Type |
| Double Rectangle | Weak Entity Type |
| Diamond | Relationship Type |
| Double Diamond | Identifying Relationship |
| Oval | Attribute |
| Oval with underlined text | Key Attribute |
| Double Oval | Multi-valued Attribute |
| Dashed Oval | Derived Attribute |
| Oval hierarchy | Composite Attribute |
| Double Line | Total Participation |
| Single Line | Partial Participation |
| 1, M, N labels | Cardinality Ratio |
ملخص رموز ER:
There are several alternative notations used for ER diagrams. Different tools and textbooks may use different notations, but they all represent the same concepts.
هناك عدة رموز بديلة تُستخدم لمخططات ER. قد تستخدم الأدوات والكتب المختلفة رموزاً مختلفة، لكنها جميعاً تمثل نفس المفاهيم.
The original notation proposed by Peter Chen in 1976:
This is the notation used throughout this course.
الرمز الأصلي الذي اقترحه بيتر تشن في 1976:
هذا هو الرمز المستخدم في هذا المقرر.
Also known as Information Engineering (IE) notation. Popular in industry tools:
| Symbol | Meaning |
|---|---|
| || | One and only one (mandatory) |
| ○| | Zero or one (optional) |
| |⋈ | One or many (mandatory) |
| ○⋈ | Zero or many (optional) |
يُعرف أيضاً برمز هندسة المعلومات (IE). شائع في أدوات الصناعة:
Specifies the minimum and maximum number of relationship instances:
Example: EMPLOYEE (1,1) ----WORKS_FOR---- (1,N) DEPARTMENT
Each employee works for exactly one department (min=1, max=1)
Each department has at least one employee, possibly many (min=1, max=N)
يحدد الحد الأدنى والأقصى لعدد حالات العلاقة:
UML (Unified Modeling Language) can also represent ER concepts:
| UML Multiplicity | Meaning |
|---|---|
| 1 | Exactly one |
| 0..1 | Zero or one |
| 1..* | One or more |
| 0..* or * | Zero or more |
يمكن لـ UML (لغة النمذجة الموحدة) أيضاً تمثيل مفاهيم ER:
| Aspect | Chen | Crow's Foot | (Min,Max) | UML |
|---|---|---|---|---|
| Entity | Rectangle | Rectangle | Rectangle | Class box |
| Relationship | Diamond | Line only | Diamond | Line |
| Attributes | Ovals | Inside entity | Ovals | Inside class |
| Cardinality | 1, M, N | Symbols | (min,max) | Multiplicity |
| Common Use | Academic | Industry tools | Academic | Software design |
مخطط ER لقاعدة بيانات الشركة يُظهر:
| Key Type | Description | Can be NULL? |
|---|---|---|
| Superkey | Any set of attributes that uniquely identifies tuples | May contain NULLs |
| Candidate Key | Minimal superkey (no redundant attributes) | No |
| Primary Key | Chosen candidate key for identification | NO (Entity Integrity) |
| Foreign Key | References primary key in another relation | Yes (if partial participation) |
There are three basic operations that can change the states of relations in a database:
Each operation may cause constraint violations, and the DBMS must handle them appropriately.
هناك ثلاث عمليات أساسية يمكنها تغيير حالات العلاقات في قاعدة البيانات:
كل عملية قد تسبب انتهاكات للقيود، ويجب على DBMS التعامل معها بشكل مناسب.
Purpose: Add a new tuple to a relation R
Syntax: INSERT INTO R VALUES (v1, v2, ..., vn)
Possible Constraint Violations:
If violation occurs: REJECT the insertion
الغرض: إضافة صف جديد إلى العلاقة R
الصيغة: INSERT INTO R VALUES (v1, v2, ..., vn)
انتهاكات القيود المحتملة:
عند حدوث انتهاك: رفض الإدراج
Purpose: Remove a tuple from a relation R
Syntax: DELETE FROM R WHERE condition
Possible Constraint Violations:
Note: DELETE cannot violate domain, key, or entity integrity constraints.
الغرض: إزالة صف من العلاقة R
الصيغة: DELETE FROM R WHERE condition
انتهاكات القيود المحتملة:
ملاحظة: الحذف لا يمكن أن ينتهك قيود النطاق أو المفتاح أو سلامة الكيان.
Purpose: Change the value(s) of one or more attributes in an existing tuple
Syntax: UPDATE R SET attribute = value WHERE condition
Possible Constraint Violations:
الغرض: تغيير قيمة سمة أو أكثر في صف موجود
الصيغة: UPDATE R SET attribute = value WHERE condition
انتهاكات القيود المحتملة:
When a referential integrity constraint is violated by DELETE or UPDATE, the DBMS can handle it using one of the following options:
| Option | Action | Example |
|---|---|---|
| RESTRICT | Reject the operation that causes violation | Cannot delete department if employees exist |
| CASCADE | Propagate the operation to referencing tuples | Delete department → automatically delete all its employees |
| SET NULL | Set FK value to NULL in referencing tuples | Delete department → set employees' Dept_No to NULL |
| SET DEFAULT | Set FK value to default value in referencing tuples | Delete department → set employees' Dept_No to default dept |
عندما يتم انتهاك قيد السلامة المرجعية بواسطة DELETE أو UPDATE، يمكن لـ DBMS التعامل معه باستخدام أحد الخيارات التالية:
| Constraint | INSERT | DELETE | UPDATE |
|---|---|---|---|
| Domain | ✓ Can violate | ✗ No | ✓ Can violate |
| Key | ✓ Can violate | ✗ No | ✓ If PK changed |
| Entity Integrity | ✓ Can violate | ✗ No | ✓ If PK changed |
| Referential Integrity | ✓ Can violate | ✓ Can violate | ✓ Can violate |
1. INSERT Example:
INSERT INTO EMPLOYEE VALUES ('123456789', 'John Smith', 5)
2. DELETE Example:
DELETE FROM DEPARTMENT WHERE Dept_No = 5
3. UPDATE Example:
UPDATE EMPLOYEE SET Dept_No = 9 WHERE SSN = '123456789'
مثال INSERT: إدراج موظف جديد - يجب أن يكون SSN فريداً والقسم موجوداً
مثال DELETE: حذف قسم - قد ينتهك السلامة المرجعية إذا كان هناك موظفون
مثال UPDATE: تحديث قسم الموظف - يجب أن يكون القسم الجديد موجوداً
| ER Model | Relational Model |
|---|---|
| Entity Type | Entity Relation (Table) |
| 1:1 or 1:N Relationship | Foreign Key (or Relationship Relation) |
| M:N Relationship | Relationship Relation + Two Foreign Keys |
| N-ary Relationship | Relationship Relation + N Foreign Keys |
| Simple Attribute | Attribute (Column) |
| Composite Attribute | Set of Simple Component Attributes |
| Multivalued Attribute | New Relation + Foreign Key |
| Key Attribute | Primary Key |
Purpose: To design good relational schemas by understanding and eliminating problems caused by redundancy. We use functional dependencies and normal forms to guide the decomposition of poorly designed tables.
Key Topics:
الهدف: تصميم مخططات علائقية جيدة من خلال فهم وإزالة المشاكل الناتجة عن التكرار. نستخدم الاعتماديات الوظيفية والأشكال الطبيعية لتوجيه تفكيك الجداول سيئة التصميم.
المواضيع الرئيسية:
Redundant Data: The same information is stored multiple times, wasting storage and causing inconsistency.
Update Anomalies:
NULL Values: Excessive NULL values waste storage and make queries more complex.
البيانات المكررة: نفس المعلومات مخزنة عدة مرات، مما يهدر المساحة ويسبب عدم التناسق.
شذوذات التحديث:
قيم NULL: القيم الفارغة الزائدة تهدر المساحة وتجعل الاستعلامات أكثر تعقيداً.
Definition: A functional dependency X → Y means that the value of attribute set X uniquely determines the value of attribute set Y.
Formal Definition: X → Y holds if and only if whenever two tuples t1 and t2 have t1[X] = t2[X], then they must also have t1[Y] = t2[Y].
Notation:
التعريف: الاعتماد الوظيفي X → Y يعني أن قيمة مجموعة السمات X تحدد بشكل فريد قيمة مجموعة السمات Y.
التعريف الرسمي: X → Y يتحقق إذا وفقط إذا كلما كان لصفين t1 و t2 نفس القيمة t1[X] = t2[X]، فيجب أن يكون لديهما أيضاً t1[Y] = t2[Y].
الترميز:
Example: SSN → {Name, BirthDate, Address}
The Social Security Number uniquely determines a person's name, birthdate, and address. If two people have the same SSN, they must have the same name, birthdate, and address.
Trivial FD: X → Y where Y ⊆ X (the dependent is a subset of the determinant)
Example: {Ssn, Ename} → Ssn is trivial.
Non-trivial FD: X → Y where Y ⊄ X (the dependent is not entirely contained in the determinant)
Example: Ssn → Ename is non-trivial.
Completely Non-trivial FD: X → Y where X ∩ Y = ∅ (no overlap between determinant and dependent)
الاعتماد البديهي: X → Y حيث Y ⊆ X (التابع جزء من المحدد)
مثال: {Ssn, Ename} → Ssn اعتماد بديهي.
الاعتماد غير البديهي: X → Y حيث Y ⊄ X (التابع ليس محتوى بالكامل في المحدد)
مثال: Ssn → Ename غير بديهي.
الاعتماد غير البديهي تماماً: X → Y حيث X ∩ Y = ∅ (لا تداخل بين المحدد والتابع)
Full Functional Dependency: Y is fully functionally dependent on X if:
Partial Functional Dependency: Y is partially dependent on X if:
الاعتماد الوظيفي الكامل: Y يعتمد اعتماداً وظيفياً كاملاً على X إذا:
الاعتماد الوظيفي الجزئي: Y يعتمد جزئياً على X إذا:
Example: Given PK = {Ssn, Pnumber}
• {Ssn, Pnumber} → Hours is a full dependency (need both to determine hours worked)
• {Ssn, Pnumber} → Ename is a partial dependency (Ssn alone determines Ename)
Definition: Z is transitively dependent on X if:
The dependency goes through an intermediate attribute Y that is not a superkey.
التعريف: Z يعتمد اعتماداً متعدياً على X إذا:
الاعتماد يمر عبر سمة وسيطة Y التي ليست مفتاحاً فائقاً.
Example: In EMPLOYEE(Ssn, Ename, Dnumber, Dname, Dmgr_ssn)
• Ssn → Dnumber (employee works in a department)
• Dnumber → Dname (department number determines department name)
• Dnumber ↛ Ssn (department doesn't determine employee)
Therefore: Dname is transitively dependent on Ssn through Dnumber.
Armstrong's rules are sound (only derive correct FDs) and complete (can derive all FDs that hold).
Primary Rules (RAT):
قواعد أرمسترونج سليمة (تشتق فقط الاعتماديات الصحيحة) وكاملة (يمكنها اشتقاق جميع الاعتماديات التي تتحقق).
القواعد الأساسية (RAT):
Secondary Rules (derived from RAT):
القواعد الثانوية (مشتقة من RAT):
Example of Decomposition (IR4):
If Ssn → {Ename, Bdate}, then Ssn → Ename AND Ssn → Bdate
Example of Union (IR5):
If Ssn → Ename and Ssn → Bdate, then Ssn → {Ename, Bdate}
Definition: A relation is in 1NF if and only if:
Violations:
التعريف: العلاقة تكون في الشكل الطبيعي الأول إذا وفقط إذا:
الانتهاكات:
Not in 1NF: STUDENT(ID, Name, Phones: "555-1234, 555-5678")
In 1NF: Create separate STUDENT_PHONE(Student_ID, Phone) table
Definition: A relation is in 2NF if and only if:
Key Terms:
Note: 2NF issues only occur when the primary key is composite (multiple attributes).
التعريف: العلاقة تكون في الشكل الطبيعي الثاني إذا وفقط إذا:
مصطلحات مهمة:
ملاحظة: مشاكل 2NF تحدث فقط عندما يكون المفتاح الأساسي مركباً (سمات متعددة).
Not in 2NF: WORKS_ON(Ssn, Pnumber, Hours, Ename, Pname)
• Ssn → Ename (partial dependency - Ename depends only on Ssn)
• Pnumber → Pname (partial dependency - Pname depends only on Pnumber)
Decomposition to 2NF:
• EMPLOYEE(Ssn, Ename)
• PROJECT(Pnumber, Pname)
• WORKS_ON(Ssn, Pnumber, Hours)
Definition: A relation is in 3NF if and only if for every non-trivial FD X → Y:
Alternative Definition: A relation is in 3NF if:
التعريف: العلاقة تكون في الشكل الطبيعي الثالث إذا وفقط إذا لكل اعتماد غير بديهي X → Y:
تعريف بديل: العلاقة تكون في 3NF إذا:
Not in 3NF: EMP_DEPT(Ssn, Ename, Dnumber, Dname, Dmgr_ssn)
• Ssn → Dnumber (OK - Ssn is the key)
• Dnumber → Dname (VIOLATION - Dnumber is not a superkey and Dname is not prime)
This is a transitive dependency: Ssn → Dnumber → Dname
Decomposition to 3NF:
• EMPLOYEE(Ssn, Ename, Dnumber)
• DEPARTMENT(Dnumber, Dname, Dmgr_ssn)
| Normal Form | Requirement | Eliminates |
|---|---|---|
| 1NF | Atomic values only | Repeating groups |
| 2NF | 1NF + No partial dependencies | Partial dependencies |
| 3NF | 2NF + No transitive dependencies (or RHS is prime) | Transitive dependencies |
| الشكل الطبيعي | المتطلب | يزيل |
|---|---|---|
| 1NF | قيم ذرية فقط | المجموعات المتكررة |
| 2NF | 1NF + لا اعتماديات جزئية | الاعتماديات الجزئية |
| 3NF | 2NF + لا اعتماديات متعدية (أو RHS أساسية) | الاعتماديات المتعدية |
SQL (Structured Query Language) is the standard language for interacting with relational databases. It is used to create, modify, and query data stored in a relational database management system (RDBMS).
SQL statements can be divided into:
SQL (لغة الاستعلام الهيكلية) هي اللغة القياسية للتفاعل مع قواعد البيانات العلائقية. تُستخدم لإنشاء وتعديل والاستعلام عن البيانات المخزنة في نظام إدارة قواعد البيانات العلائقية.
يمكن تقسيم عبارات SQL إلى:
SQL can be used to perform the following operations:
يمكن استخدام SQL لتنفيذ العمليات التالية:
The CREATE TABLE statement is used to create a new table in the database.
Syntax:
تُستخدم عبارة CREATE TABLE لإنشاء جدول جديد في قاعدة البيانات.
الصيغة:
CREATE TABLE employee (
emp_id NUMBER(4),
emp_name VARCHAR2(30),
hire_date DATE,
salary NUMBER(8,2)
);
| Data Type | Description |
|---|---|
| VARCHAR2(n) | Variable-length character data (up to n characters) |
| CHAR(n) | Fixed-length character data (exactly n characters) |
| NUMBER(p,s) | Numeric data with precision p and scale s |
| DATE | Date and time values |
| نوع البيانات | الوصف |
|---|---|
| VARCHAR2(n) | بيانات نصية متغيرة الطول (حتى n حرف) |
| CHAR(n) | بيانات نصية ثابتة الطول (بالضبط n حرف) |
| NUMBER(p,s) | بيانات رقمية بدقة p ومقياس s |
| DATE | قيم التاريخ والوقت |
The PRIMARY KEY constraint uniquely identifies each row in a table. It must contain unique values and cannot contain NULL values.
Column-level syntax:
Table-level syntax:
قيد PRIMARY KEY يُحدد بشكل فريد كل صف في الجدول. يجب أن يحتوي على قيم فريدة ولا يمكن أن يحتوي على قيم NULL.
صيغة مستوى العمود:
صيغة مستوى الجدول:
-- Column-level PRIMARY KEY
CREATE TABLE department (
dept_id NUMBER(4) PRIMARY KEY,
dept_name VARCHAR2(30)
);
-- Table-level PRIMARY KEY
CREATE TABLE department (
dept_id NUMBER(4),
dept_name VARCHAR2(30),
PRIMARY KEY (dept_id)
);
The FOREIGN KEY constraint establishes a relationship between two tables. It references the PRIMARY KEY of another table.
Syntax:
A FOREIGN KEY constraint enforces referential integrity: you cannot insert a value that does not exist in the parent table.
قيد FOREIGN KEY يُنشئ علاقة بين جدولين. يُشير إلى المفتاح الرئيسي لجدول آخر.
الصيغة:
قيد FOREIGN KEY يُطبق التكامل المرجعي: لا يمكنك إدراج قيمة غير موجودة في الجدول الأصلي.
CREATE TABLE employee (
emp_id NUMBER(4) PRIMARY KEY,
emp_name VARCHAR2(30),
dept_id NUMBER(4),
FOREIGN KEY (dept_id) REFERENCES department(dept_id)
);
The INSERT statement adds new rows to a table.
Syntax:
Rules:
عبارة INSERT تُضيف صفوفاً جديدة إلى الجدول.
الصيغة:
القواعد:
INSERT INTO department (dept_id, dept_name) VALUES (10, 'Accounting'); INSERT INTO employee VALUES (101, 'John Smith', '15-JAN-2020', 5000);
There are two methods to insert NULL values:
هناك طريقتان لإدراج قيم NULL:
-- Implicit: omit commission column INSERT INTO employee (emp_id, emp_name, salary) VALUES (102, 'Jane Doe', 4500); -- Explicit: specify NULL INSERT INTO employee (emp_id, emp_name, salary, commission) VALUES (103, 'Bob Wilson', 4000, NULL);
The UPDATE statement modifies existing rows in a table.
Syntax:
Important: Without a WHERE clause, ALL rows in the table will be updated!
عبارة UPDATE تُعدّل الصفوف الموجودة في الجدول.
الصيغة:
مهم: بدون جملة WHERE، سيتم تحديث جميع الصفوف في الجدول!
-- Update specific employee's salary UPDATE employee SET salary = 5500 WHERE emp_id = 101; -- Update multiple columns UPDATE employee SET salary = 6000, dept_id = 20 WHERE emp_id = 102;
The DELETE statement removes rows from a table.
Syntax:
Important: Without a WHERE clause, ALL rows will be deleted!
Integrity Constraint Error: You cannot delete a row that is referenced by a FOREIGN KEY in another table.
عبارة DELETE تُزيل الصفوف من الجدول.
الصيغة:
مهم: بدون جملة WHERE، سيتم حذف جميع الصفوف!
خطأ قيد التكامل: لا يمكنك حذف صف تتم الإشارة إليه بواسطة FOREIGN KEY في جدول آخر.
-- Delete specific employee DELETE FROM employee WHERE emp_id = 103; -- Delete all employees in department 10 DELETE FROM employee WHERE dept_id = 10;
When performing DML operations, you may encounter integrity constraint violations:
عند تنفيذ عمليات DML، قد تواجه انتهاكات قيود التكامل:
Poor database design leads to several problems:
Ambiguous meaning of NULL:
التصميم السيء لقاعدة البيانات يؤدي إلى عدة مشاكل:
المعنى الغامض لـ NULL:
Insert Anomalies: You cannot create/insert a new course unless you have a student enrolled in that course.
Update Anomalies: In case of updating StudentName, you have to update many rows.
Delete Anomalies: If a course has only one student, deleting the student will delete the course.
شذوذ الإدراج: لا يمكنك إنشاء/إدراج مقرر جديد ما لم يكن هناك طالب مسجل في ذلك المقرر.
شذوذ التحديث: في حالة تحديث اسم الطالب، يجب تحديث العديد من الصفوف.
شذوذ الحذف: إذا كان المقرر يحتوي على طالب واحد فقط، فإن حذف الطالب سيحذف المقرر.
| Student# | StudentName | Course# | CourseName |
|---|---|---|---|
| 100 | Ali | CS100 | C++ |
| 100 | Ali | CS101 | Java |
| 200 | Ahmad | CS200 | OS |
Note: "Ali" is repeated - this causes update anomalies!
A Functional Dependency (FD) is a constraint between two sets of attributes in a relation schema.
If X and Y are two sets of attributes in the same relation schema R, then X → Y means that X functionally determines Y.
الاعتماد الوظيفي (FD) هو قيد بين مجموعتين من السمات في مخطط العلاقة.
إذا كان X و Y مجموعتين من السمات في نفس مخطط العلاقة R، فإن X → Y تعني أن X يحدد وظيفياً Y.
The FD constraint is that for any two tuples t1 and t2 in the relation instance r(R) that have:
This means:
X is termed the left-hand-side (LHS) or determinant
Y is termed the right-hand-side (RHS)
قيد FD هو أنه لأي صفين t1 و t2 في مثيل العلاقة r(R) اللذان لديهما:
هذا يعني:
X يسمى الجانب الأيسر (LHS) أو المحدد
Y يسمى الجانب الأيمن (RHS)
Example: A relation DEPARTMENT (DNO, DNAME, DLOC) can have:
FD1: DNO → DLOC FD2: DNO → DNAME
Inference Rules combine known facts to produce ("infer") new facts.
There are 6 inference rules (IR1-IR6). IR1-IR3 are called Armstrong's Inference Rules.
These rules are sound (logically correct) and complete (can imply any possible logical FD).
قواعد الاستدلال تجمع الحقائق المعروفة لإنتاج ("استنتاج") حقائق جديدة.
هناك 6 قواعد استدلال (IR1-IR6). تسمى IR1-IR3 قواعد أرمسترونج للاستدلال.
هذه القواعد سليمة (صحيحة منطقياً) وكاملة (يمكنها استنتاج أي FD منطقي ممكن).
A set of attributes always determines itself or any of its subsets.
مجموعة السمات تحدد دائماً نفسها أو أي من مجموعاتها الفرعية.
If ESSN ⊆ {ESSN, Dependent_Name} then
{ESSN, Dependent_Name} → ESSN holds
Adding the same set of attributes to both the LHS & RHS of a FD results in another valid FD.
إضافة نفس مجموعة السمات إلى كل من LHS و RHS لـ FD ينتج FD صالح آخر.
If SSN → Ename then
{SSN, Address} → {Ename, Address}
FDs are transitive.
الاعتمادات الوظيفية متعدية.
If SSN → Dno and Dno → Dlocation then
SSN → Dlocation
IR4: Decomposition Rule
We can remove attributes from the RHS and decompose the FD.
IR5: Additive (Union) Rule
We can union attributes from the RHS and combine FDs into a single FD (reverse of IR4).
IR6: Pseudo Transitive Rule
A variant of IR3.
IR4: قاعدة التحليل
يمكننا إزالة السمات من RHS وتحليل FD.
IR5: قاعدة الاتحاد
يمكننا دمج السمات من RHS وتجميع FDs في FD واحد (عكس IR4).
IR6: قاعدة التعدي الزائف
متغير من IR3.
Definition: Given a set F of functional dependencies on R, the closure of F denoted by F+ is the set of all functional dependencies inferred from F via the inference rules.
To compute F+:
التعريف: بمعرفة مجموعة F من الاعتمادات الوظيفية على R، فإن إغلاق F المرمز له بـ F+ هو مجموعة جميع الاعتمادات الوظيفية المستنتجة من F عبر قواعد الاستدلال.
لحساب F+:
Example: Let R(A,B,C,D) with F={A→B, A→C, BC→D}. Find F+
A→B and A→C then A→BC (Rule 5) A→BC and BC→D then A→D (Rule 3) A→B and A→D then A→BD (Rule 5) A→C and A→D then A→CD (Rule 5) A→B and A→C and A→D then A→BCD (Rule 5)
Given a relation schema R and a set of FDs that hold on R. Let α be a set of attributes in R. Then:
Use: If α+ contains all attributes of R, then α is a candidate key.
بمعرفة مخطط علاقة R ومجموعة من FDs تنطبق على R. لتكن α مجموعة سمات في R. إذن:
الاستخدام: إذا احتوى α+ على جميع سمات R، فإن α هو مفتاح مرشح.
Example: Given R(A, B, C) with F={A→B, B→C}. Calculate A+
Initially, A+ = {A}
From A→B we get A+ = {A, B}
From B→C we get A+ = {A, B, C}
Therefore A+ = {A, B, C} = all attributes of R
So A is a candidate key!
Normalization is a method for organizing data elements in a database into tables to minimize duplication.
Why Normalization?
Goal: In each table, all non-key attributes should be dependent on the primary key.
التطبيع هو طريقة لتنظيم عناصر البيانات في قاعدة بيانات إلى جداول لتقليل التكرار.
لماذا التطبيع؟
الهدف: في كل جدول، يجب أن تعتمد جميع السمات غير المفتاحية على المفتاح الرئيسي.
A relation schema is in 1NF if:
No composite attributes and no multivalued attributes.
مخطط العلاقة يكون في 1NF إذا:
لا سمات مركبة ولا سمات متعددة القيم.
Not in 1NF: R(SSN, Name(F-name, L-name), {telephone})
Name is composite, telephone is multivalued!
A relation schema is in 2NF if for all FDs (α→β), one of the following is satisfied:
Key point: Remove partial dependencies to achieve 2NF.
مخطط العلاقة يكون في 2NF إذا كان لجميع FDs (α→β)، أحد الشروط التالية متحقق:
النقطة الرئيسية: أزل الاعتمادات الجزئية لتحقيق 2NF.
A relation schema is in 3NF if for all FDs (α→β), one of the following is satisfied:
Prime attribute: An attribute that is a member of any candidate key.
Nonprime attribute: An attribute that is not a member of any candidate key.
Key point: Remove transitive dependencies to achieve 3NF.
مخطط العلاقة يكون في 3NF إذا كان لجميع FDs (α→β)، أحد الشروط التالية متحقق:
سمة أولية: سمة هي عضو في أي مفتاح مرشح.
سمة غير أولية: سمة ليست عضو في أي مفتاح مرشح.
النقطة الرئيسية: أزل الاعتمادات المتعدية لتحقيق 3NF.
A relation schema is in BCNF if for all FDs (α→β), one of the following is satisfied:
BCNF is stricter than 3NF - it removes the third condition about prime attributes.
Decomposition for BCNF:
مخطط العلاقة يكون في BCNF إذا كان لجميع FDs (α→β)، أحد الشروط التالية متحقق:
BCNF أكثر صرامة من 3NF - تزيل الشرط الثالث حول السمات الأولية.
التحليل لـ BCNF:
When a relation satisfies 3NF:
عندما تحقق العلاقة 3NF: