Database Fundamentals

Chapter 1: Databases and Database Users

Chapter Contents

  1. Basic Definitions
  2. Example of a Database
  3. A Simplified Database System Environment
  4. Characteristics of the Database Approach
  5. Database System Users
  6. Advantages of Using the DBMS Approach
  7. Additional Implications of Using the Database Approach
  8. When Not Using a DBMS

Chapter Summary

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)، ثم ينتقل إلى شرح خصائص منهج قواعد البيانات ومميزاته مقارنة بالنظام التقليدي.

1. What is Data?

Data: Known facts about something that can be recorded and have an implicit meaning in the user's environment.

Examples:

  • Person's name, telephone, address, email, picture
  • Student's ID, name, address, sex, major
  • Product's name, description, manufacturer, price
  • Book's publisher, author, title

البيانات: حقائق معروفة عن شيء ما يمكن تسجيلها ولها معنى ضمني في بيئة المستخدم.

أمثلة:

  • اسم الشخص، هاتفه، عنوانه، بريده الإلكتروني، صورته
  • رقم الطالب، اسمه، عنوانه، جنسه، تخصصه
  • اسم المنتج، وصفه، المصنّع، السعر
  • الناشر، المؤلف، العنوان

2. What is Database?

Database (DB): A collection of related data + meta-data.

Mini-world Example: A university database containing:

  • Entities: STUDENTs, COURSEs, SECTIONs, INSTRUCTORs
  • Relationships:
    • SECTIONs are of specific COURSEs
    • STUDENTs take SECTIONs
    • COURSEs have prerequisite COURSEs

قاعدة البيانات: مجموعة من البيانات المترابطة + البيانات الوصفية (meta-data).

مثال (عالم مصغر): قاعدة بيانات جامعية تحتوي على:

  • الكيانات: الطلاب، المقررات، الشُعب، المدرسون
  • العلاقات:
    • الشُعب تابعة لمقررات محددة
    • الطلاب يسجلون في شُعب
    • المقررات لها متطلبات سابقة

3. What is 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:

  • Structure of each file
  • Type and storage format of each field
  • Various constraints on the data

Database Catalog: A central location where data definitions (meta-data) are stored.

البيانات الوصفية: بيانات عن البيانات. توفر تعريف أو وصف لهيكل قاعدة البيانات. يتم تخزين هذا التعريف في فهرس قاعدة البيانات.

تعريف قاعدة البيانات يحتوي على:

  • هيكل كل ملف
  • نوع وتنسيق تخزين كل حقل
  • القيود المختلفة على البيانات

فهرس قاعدة البيانات: موقع مركزي يتم فيه تخزين تعريفات البيانات.

4. What is DBMS?

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

DBMS Functionality:

  1. Defining: Specifying data types, structures, and constraints
  2. Constructing/Loading: Process of storing data on secondary storage medium
  3. Manipulating: Querying, inserting, deleting, updating, generating reports
  4. Sharing: Allowing multiple users and programs to access concurrently

نظام إدارة قواعد البيانات: مجموعة من البرامج/نظام برمجي يمكّن المستخدمين من إنشاء وصيانة قاعدة بيانات.

أمثلة: Oracle, MS Access, MS SQL Server, IBM DB2, MySQL

وظائف DBMS:

  1. التعريف (Defining): تحديد أنواع البيانات والهياكل والقيود
  2. البناء/التحميل (Constructing): عملية تخزين البيانات على وسيط تخزين ثانوي
  3. المعالجة (Manipulating): الاستعلام، الإدراج، الحذف، التحديث، إنشاء التقارير
  4. المشاركة (Sharing): السماح لعدة مستخدمين وبرامج بالوصول المتزامن

5. What is a Database System?

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) تتضمن أوامر مطلوبة للتعامل مع قاعدة البيانات.

DBS = DB + DBMS + Application Programs

Characteristics of the Database Approach

The main characteristics of the database approach versus the file-processing approach are:

  1. Self-describing nature: The database system contains the data and a complete description of its structure and constraints (meta-data in the catalog).
  2. Insulation between programs and data: Data structure can be changed without needing to change programs that access it.
  3. Data abstraction: DBMS provides a conceptual representation of data without including details of how it is stored.
  4. Support of multiple views: Different users can see different parts of the database based on their needs.
  5. Sharing of data and multi-user transaction processing: Allows multiple users to access the database simultaneously while ensuring data integrity.

الخصائص الرئيسية لمنهج قواعد البيانات مقارنة بمنهج معالجة الملفات:

  1. الطبيعة الذاتية الوصف: نظام قاعدة البيانات يحتوي على البيانات ووصف كامل لهيكلها وقيودها (meta-data في الفهرس).
  2. العزل بين البرامج والبيانات: يمكن تغيير هيكل البيانات دون الحاجة لتغيير البرامج التي تصل إليها.
  3. تجريد البيانات: يوفر DBMS تمثيلاً مفاهيمياً للبيانات دون تضمين تفاصيل كيفية تخزينها.
  4. دعم عدة عروض: يمكن لمستخدمين مختلفين رؤية أجزاء مختلفة من قاعدة البيانات حسب احتياجاتهم.
  5. مشاركة البيانات ومعالجة المعاملات: يسمح لعدة مستخدمين بالوصول في نفس الوقت مع ضمان سلامة البيانات.

Database System Users - Actors on the Scene

People whose jobs involve the day-to-day use of large databases:

1. Database Administrators (DBA)

  • Managing database resources
  • Authorizing access to the database
  • Coordinating and monitoring its use

2. Database Designers

  • Identify data to be stored
  • Choose appropriate structures
  • Communicate with all potential users

3. End Users

  • Casual Users: Access DB occasionally with different needs
  • Naive/Parametric Users: Use pre-prepared queries frequently
  • Sophisticated Users: Write their own queries
  • Standalone Users: Maintain personal databases

4. System Analysts and Application Programmers

  • System Analysts: Determine end-user requirements
  • Application Programmers: Implement specifications as programs

الأشخاص الذين تتضمن وظائفهم الاستخدام اليومي لقواعد البيانات الكبيرة:

1. مدراء قواعد البيانات (DBA)

  • إدارة موارد قاعدة البيانات
  • تفويض الوصول إلى قاعدة البيانات
  • تنسيق ومراقبة استخدامها

2. مصممو قواعد البيانات

  • تحديد البيانات التي سيتم تخزينها
  • اختيار الهياكل المناسبة
  • التواصل مع جميع المستخدمين المحتملين

3. المستخدمون النهائيون

  • العرضيون (Casual): يحتاجون معلومات مختلفة في كل مرة
  • المعياريون (Naive/Parametric): يستخدمون استعلامات معدة مسبقاً
  • المتقدمون (Sophisticated): يكتبون استعلاماتهم الخاصة
  • المستقلون (Standalone): يحتفظون بقواعد بيانات شخصية

4. محللو الأنظمة ومبرمجو التطبيقات

  • محللو الأنظمة: يحددون متطلبات المستخدمين النهائيين
  • مبرمجو التطبيقات: ينفذون المواصفات كبرامج

Actors behind the Scene

  • DBMS designers and developers: Design and develop the DBMS software
  • Tools developers: Develop tools for database management
  • Operators: Backup and maintain the database
  • مصممو ومطورو DBMS: يصممون ويطورون برنامج نظام إدارة قواعد البيانات
  • مطورو الأدوات: يطورون أدوات لإدارة قواعد البيانات
  • المشغلون: النسخ الاحتياطي وصيانة قاعدة البيانات

1. Controlling Redundancy

  • In DBMS approach, data is stored in only one place
  • Saves space
  • Sometimes redundancy enhances performance
  • DBMS provides environment where redundancy can be controlled (minimized, not eliminated)
  • في منهج DBMS، يتم تخزين البيانات في مكان واحد فقط
  • يوفر المساحة
  • أحياناً التكرار يحسن الأداء
  • DBMS يوفر بيئة يمكن فيها التحكم بالتكرار (تقليله وليس إلغاؤه تماماً)

2. Restricting Unauthorized Access

  • Users must be authorized to access the DB
  • Some users: only Retrieve data
  • Some users: Retrieve and Update data
  • DBMS provides security and authorization subsystem
  • DBA creates accounts and specifies restrictions
  • يجب تفويض المستخدمين للوصول إلى قاعدة البيانات
  • بعض المستخدمين: فقط استرجاع البيانات
  • بعض المستخدمين: استرجاع وتحديث البيانات
  • DBMS يوفر نظام أمان وتفويض فرعي
  • DBA ينشئ الحسابات ويحدد القيود

3. Providing Multiple User Interfaces

  • Menu-driven Interfaces / Query Languages: For End Users
  • Programming Language Interfaces: For Application Programmers
  • Forms and Command Codes: For Parametric Users
  • Both Forms-style and Menu-driven interfaces are known as GUI
  • واجهات القوائم / لغات الاستعلام: للمستخدمين النهائيين
  • واجهات لغات البرمجة: لمبرمجي التطبيقات
  • النماذج وأكواد الأوامر: للمستخدمين المعياريين
  • واجهات النماذج والقوائم تُعرف بـ GUI

4. Representing Complex Relationships

DBMS has ability to represent complex relationships among the data.

Example: Relationship between students and courses, between courses and their sections, etc.

DBMS لديه القدرة على تمثيل العلاقات المعقدة بين البيانات.

مثال: العلاقة بين الطلاب والمقررات، بين المقررات وشُعبها، إلخ.

5. Enforcing Integrity Constraints

Data should satisfy certain integrity constraints:

  • EMPLOYEES Age should be 16-70
  • STUDENT Grades values should be {A, B, C, D, F}
  • EmployeeID should have unique value for each employee
  • Student No must be a 9 digit integer

Constraints identified at database design time. Some enforced by DBMS, others by application programs.

يجب أن تستوفي البيانات قيوداً معينة للسلامة:

  • عمر الموظف يجب أن يكون بين 16-70
  • درجات الطالب يجب أن تكون {A, B, C, D, F}
  • رقم الموظف يجب أن يكون فريداً لكل موظف
  • رقم الطالب يجب أن يكون عدداً صحيحاً من 9 أرقام

القيود تُحدد وقت تصميم قاعدة البيانات. بعضها يُفرض بواسطة DBMS، وبعضها بواسطة برامج التطبيقات.

6. Providing Backup and Recovery

DBMS provides facility to recover from hardware and software failures through its backup and recovery sub-system.

Scenario:

  • An update program is executing
  • Computer System fails in the middle of the update
  • DBMS restores the database to a state prior to the update and restarts the update program

DBMS يوفر إمكانية الاستعادة من أعطال الأجهزة والبرمجيات من خلال نظام النسخ الاحتياطي والاستعادة.

سيناريو:

  • برنامج تحديث قيد التنفيذ
  • نظام الكمبيوتر يفشل في منتصف التحديث
  • DBMS يستعيد قاعدة البيانات لحالة ما قبل التحديث ويعيد تشغيل برنامج التحديث

Additional Implications of Using the Database Approach

Beyond the main advantages, there are additional implications of using the database approach:

بالإضافة إلى المزايا الرئيسية، هناك آثار إضافية لاستخدام منهج قواعد البيانات:

1. Potential for Enforcing Standards

  • DBA can define and enforce standards among database users
  • Standards include: naming conventions, display formats, report structures, documentation standards, update procedures, and access rules
  • Facilitates communication and cooperation between departments
  • يمكن لمدير قاعدة البيانات تحديد وفرض المعايير بين مستخدمي قاعدة البيانات
  • تشمل المعايير: اصطلاحات التسمية، تنسيقات العرض، هياكل التقارير، معايير التوثيق، إجراءات التحديث، وقواعد الوصول
  • يسهل التواصل والتعاون بين الأقسام

2. Reduced Application Development Time

  • Developing a new application takes much less time than writing a new file-processing application
  • Estimate: 1/4 to 1/6 of the time needed for file systems
  • DBMS provides many functions required by applications
  • تطوير تطبيق جديد يستغرق وقتاً أقل بكثير من كتابة تطبيق ملفات جديد
  • التقدير: من 1/4 إلى 1/6 من الوقت المطلوب لأنظمة الملفات
  • DBMS يوفر العديد من الوظائف المطلوبة للتطبيقات

3. Flexibility

  • Database structure can be changed to meet new requirements
  • Changes can be made without affecting existing programs
  • This is due to program-data independence
  • يمكن تغيير هيكل قاعدة البيانات لتلبية المتطلبات الجديدة
  • يمكن إجراء التغييرات دون التأثير على البرامج الموجودة
  • هذا بسبب استقلالية البرنامج عن البيانات

4. Availability of Up-to-Date Information

  • DBMS makes the database available to all users immediately
  • As soon as one user updates the database, all other users can see it
  • Essential for many transaction-processing applications (airline reservations, banking, etc.)
  • DBMS يجعل قاعدة البيانات متاحة لجميع المستخدمين فوراً
  • بمجرد أن يحدث مستخدم قاعدة البيانات، يمكن لجميع المستخدمين الآخرين رؤيتها
  • ضروري للعديد من تطبيقات معالجة المعاملات (حجوزات الطيران، البنوك، إلخ)

5. Economies of Scale

  • Consolidating data and applications reduces wasteful overlap
  • Investment in personnel, hardware, and software can be reduced
  • Overall operational and management costs can be reduced
  • دمج البيانات والتطبيقات يقلل من التداخل المهدر
  • يمكن تقليل الاستثمار في الموظفين والأجهزة والبرمجيات
  • يمكن تقليل تكاليف التشغيل والإدارة الإجمالية

When Not to Use a DBMS

Despite the advantages of using a DBMS, there are situations where it may not be advisable:

على الرغم من مزايا استخدام DBMS، هناك حالات قد لا يكون فيها ذلك مستحسناً:

High Overhead Costs of DBMS

  • Initial investment: Hardware, software, training
  • Overhead for providing: Security, concurrency control, recovery, integrity functions
  • Generality: DBMS is written to be general, not specific to one application
  • الاستثمار الأولي: الأجهزة، البرمجيات، التدريب
  • التكاليف الإضافية لتوفير: الأمان، التحكم في التزامن، الاستعادة، وظائف السلامة
  • العمومية: DBMS مكتوب ليكون عاماً، وليس محدداً لتطبيق واحد

When NOT to Use a DBMS

A DBMS may involve unnecessary overhead when:

  • Database and applications are simple, well-defined, and not expected to change
  • Stringent real-time requirements that may not be met because of DBMS overhead
  • No multiple-user access to data is required

قد يتضمن DBMS تكاليف إضافية غير ضرورية عندما:

  • قاعدة البيانات والتطبيقات بسيطة، محددة جيداً، وغير متوقع أن تتغير
  • متطلبات صارمة للوقت الحقيقي قد لا تتحقق بسبب التكاليف الإضافية لـ DBMS
  • لا حاجة للوصول متعدد المستخدمين للبيانات

Summary: DBMS vs File System

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

Database System Environment

Simplified Database System Environment Users Application Programs DBMS Software Database (DB) Meta-Data (Catalog) Database System
Database System = DB + DBMS + Application Programs

Database Users Hierarchy

Database System Users Actors on the Scene Database Administrators (DBA) Database Designers End Users System Analysts & App Programmers Actors behind the Scene DBMS Designers & Developers Tools Developers Operators (Backup & Maintenance) DATABASE SYSTEM

Chapter 1 - Questions

1. What is the definition of Data?

A) A collection of programs that manage a database
B) Known facts that can be recorded and have implicit meaning
C) A file storage system
D) A graphical user interface
Answer: B

2. What is Meta-Data?

A) The main data in the database
B) Data about data - description of database structure
C) Deleted data
D) User data
Answer: B

3. What is the correct formula for Database System (DBS)?

A) DBS = Data + Users
B) DBS = DBMS + Hardware
C) DBS = DB + DBMS + Application Programs
D) DBS = Software + Hardware
Answer: C

4. Which is NOT a function of DBMS?

A) Defining
B) Constructing
C) Compiling
D) Sharing
Answer: C - DBMS functions: Defining, Constructing, Manipulating, Sharing

5. Who are the "Actors behind the Scene"?

A) DBMS designers, Tools developers, Operators
B) DBA, Database Designers, End Users
C) Hardware engineers only
D) Network administrators only
Answer: A

6. What is the benefit of "Controlling Redundancy"?

A) Increase data duplication
B) Store data in one place and save space
C) Create multiple copies of data
D) Delete all duplicate data
Answer: B

Chapter 2: DB System Concepts and Architecture

Chapter Contents

  1. Data Models, Schemas, and Instances
  2. Three-Schema Architecture and Data Independence
  3. DBMS Languages and Interfaces
  4. The DB System Environment
  5. Database System Utilities
  6. Centralized and Client/Server Architectures for DBMSs
  7. Classification of DBMSs

Chapter Summary

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، هندسة الثلاث مستويات، واستقلالية البيانات.

What is a Data Model?

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:

  • Basic Operations: Add, Delete, Retrieve, Update
  • User-defined Operations: e.g., Compute GPA

A data model is the main tool for providing data abstraction by hiding storage details.

نموذج البيانات: مجموعة من المفاهيم المستخدمة لوصف هيكل قاعدة البيانات (البيانات، العلاقات، والقيود).

عمليات نموذج البيانات:

  • العمليات الأساسية: إضافة، حذف، استرجاع، تحديث
  • العمليات المخصصة: مثل حساب المعدل التراكمي (GPA)

نموذج البيانات هو الأداة الرئيسية لتوفير تجريد البيانات عن طريق إخفاء تفاصيل التخزين.

Categories of Data Models

1. High-Level / Conceptual Data Models

  • Provide concepts close to how users perceive data
  • Concerned with WHAT data is represented (not HOW)
  • Often expressed with graphical notation: ER diagram, UML
  • No detail about computer implementation or storage

Examples: Entity-Relationship Model, Object-Oriented Model

  • توفر مفاهيم قريبة من طريقة إدراك المستخدمين للبيانات
  • تركز على ماذا يُمثل من البيانات (وليس كيف)
  • تُعبر عادة برسومات مثل ER Diagram و UML
  • لا تفاصيل عن تنفيذ الكمبيوتر أو التخزين

أمثلة: نموذج الكيان-العلاقة (ER)، النموذج الكائني التوجه (OO)

2. Representational / Implementation Data Models

  • Provide concepts between high-level and low-level
  • Balance user views with some computer storage details
  • Concerned with HOW data is represented in the database
  • Hide some details of data storage

Examples: Relational, Hierarchical, Network Data Models

  • توفر مفاهيم تقع بين المستويين العالي والمنخفض
  • توازن بين عروض المستخدم وتفاصيل تخزين الكمبيوتر
  • تركز على كيف يُمثل البيانات في قاعدة البيانات
  • تخفي بعض تفاصيل التخزين

أمثلة: النموذج العلائقي، الهرمي، الشبكي

3. Low-Level / Physical Data Models

  • Provide concepts describing how data is stored as files
  • Represent: record formats, record orderings, access paths
  • Access Path: A structure that makes the search for particular DB records efficient
  • توفر مفاهيم تصف تفاصيل كيفية تخزين البيانات كملفات
  • تمثل: تنسيقات السجلات، ترتيب السجلات، مسارات الوصول
  • مسار الوصول (Access Path): هيكل يجعل البحث عن سجلات معينة أكثر كفاءة

Database Schema

Database Schema: The description of a database. Includes descriptions of the database structure and the constraints.

  • Specified during database design
  • Not expected to change frequently
  • Also called Intension

مخطط قاعدة البيانات (Schema): وصف لقاعدة البيانات يتضمن وصف الهيكل والقيود.

  • يُحدد أثناء تصميم قاعدة البيانات
  • لا يُتوقع تغييره بشكل متكرر
  • يُعرف أيضاً بـ Intension

Database Instance

Database Instance (State): The actual data stored in a database at any particular moment in time. Also called Extension.

  • Initial Database State: When database is first loaded
  • Valid State: A state that satisfies structure and constraints
  • Database state changes every time the database is updated

نسخة قاعدة البيانات (Instance/State): البيانات الفعلية المخزنة في أي لحظة معينة. تسمى أيضاً Extension.

  • الحالة الأولية: عند تحميل قاعدة البيانات لأول مرة
  • الحالة الصالحة: حالة تستوفي الهيكل والقيود
  • حالة قاعدة البيانات تتغير مع كل تحديث

Schema vs Instance Comparison

SchemaInstance
Description of structureActual stored data
Does not change frequentlyChanges with every update
Called IntensionCalled Extension
Example: STUDENT(ID, Name, Age)Example: (123, "Ahmed", 20)

Three-Schema Architecture

A convenient tool for visualizing schema levels in a DB system.

Proposed to support:

  • Program-data independence
  • Support of multiple views of the data

أداة مريحة لتصور مستويات المخططات في نظام قاعدة البيانات.

مقترحة لدعم:

  • استقلالية البرنامج عن البيانات
  • دعم عدة عروض للبيانات

The Three Levels

1. External Level (View Level)

  • Contains multiple external schemas or views
  • Each schema describes the part of DB a user group is interested in
  • Hides the rest of the database from that user group

Describes: That part of the database relevant to each user

  • يحتوي على عدة مخططات خارجية أو عروض
  • كل مخطط يصف الجزء الذي تهتم به مجموعة معينة
  • يخفي باقي قاعدة البيانات عن تلك المجموعة

يصف: الجزء من قاعدة البيانات المتعلق بكل مستخدم

2. Conceptual Level

  • Contains one conceptual schema
  • Describes structure and constraints of whole database

Describes: WHAT data is stored, relationships, and constraints

  • يحتوي على مخطط مفاهيمي واحد
  • يصف هيكل وقيود قاعدة البيانات بالكامل

يصف: ماذا يُخزن، العلاقات، والقيود

3. Internal Level

  • Contains one internal schema
  • Describes physical storage structures and access paths

Describes: HOW data is stored

  • يحتوي على مخطط داخلي واحد
  • يصف هياكل التخزين الفيزيائية ومسارات الوصول

يصف: كيف يتم تخزين البيانات

Data Independence

Data Independence: The capacity to change the schema at one level without having to change the schema at the next higher level.

1. Logical Data Independence

Capacity to change the conceptual schema without changing external schemas or application programs.

2. Physical Data Independence

Capacity to change the internal schema without changing the conceptual schema.

استقلالية البيانات: القدرة على تغيير المخطط في مستوى واحد دون الحاجة لتغيير المخطط في المستوى الأعلى.

1. الاستقلالية المنطقية (Logical)

القدرة على تغيير المخطط المفاهيمي دون تغيير المخططات الخارجية أو برامج التطبيقات.

2. الاستقلالية الفيزيائية (Physical)

القدرة على تغيير المخطط الداخلي دون تغيير المخطط المفاهيمي.

1. Data Definition Language (DDL)

  • Used by DBA and database designers to specify the conceptual schema
  • DDL compiler translates statements and provides DB schema stored in catalog
  • Also used to define internal and external schemas (views)

Examples: CREATE TABLE, ALTER TABLE, DROP TABLE, CREATE VIEW

  • تستخدم من قبل DBA ومصممي قواعد البيانات لتحديد المخطط المفاهيمي
  • مترجم DDL يترجم العبارات ويوفر مخطط قاعدة البيانات في الفهرس
  • تستخدم أيضاً لتعريف المخططات الداخلية والخارجية

أمثلة: CREATE TABLE, ALTER TABLE, DROP TABLE, CREATE VIEW

2. Data Manipulation Language (DML)

  • Used to specify database retrievals and updates
  • DML commands can be embedded in a host language (COBOL, C)
  • Or applied directly as stand-alone commands (query language)

Examples: SELECT, INSERT, UPDATE, DELETE

  • تستخدم لتحديد استرجاعات وتحديثات قاعدة البيانات
  • يمكن تضمين أوامر DML في لغة مضيفة (COBOL, C)
  • أو تطبيقها مباشرة كأوامر مستقلة (لغة استعلام)

أمثلة: SELECT, INSERT, UPDATE, DELETE

Types of DML

High-Level (Non-Procedural)Low-Level (Procedural)
Set-orientedRecord-at-a-time
Specify WHAT to retrieveSpecify HOW to retrieve
Declarative LanguagesIncludes looping constructs
Example: SQLExample: Embedded SQL in C

The Database System Environment

A DBMS is a complex software system with multiple components that work together to manage the database.

نظام إدارة قواعد البيانات هو نظام برمجي معقد يتكون من مكونات متعددة تعمل معاً لإدارة قاعدة البيانات.

DBMS Component Modules

  • Stored Data Manager: Controls access to DBMS information stored on disk (database and catalog)
  • DDL Compiler: Processes schema definitions and stores them in the catalog
  • Runtime Database Processor: Handles database access at runtime (executes privileged commands, query plans, and canned transactions)
  • Query Compiler: Handles interactive queries by parsing, analyzing, compiling, and optimizing them
  • Precompiler: Extracts DML commands from application programs written in host language
  • مدير البيانات المخزنة: يتحكم في الوصول إلى معلومات DBMS المخزنة على القرص
  • مترجم DDL: يعالج تعريفات المخطط ويخزنها في الكتالوج
  • معالج قاعدة البيانات وقت التشغيل: يتعامل مع الوصول لقاعدة البيانات وقت التشغيل
  • مترجم الاستعلامات: يتعامل مع الاستعلامات التفاعلية بتحليلها وتجميعها وتحسينها
  • المترجم المسبق: يستخرج أوامر DML من برامج التطبيقات

System Catalog (Data Dictionary)

The catalog stores:

  • Names of files, data items, and storage details
  • Mapping information between schemas at different levels
  • Constraints on the data
  • Descriptive information (owner, creation date, usage statistics)

يخزن الكتالوج:

  • أسماء الملفات وعناصر البيانات وتفاصيل التخزين
  • معلومات التعيين بين المخططات على المستويات المختلفة
  • القيود على البيانات
  • معلومات وصفية (المالك، تاريخ الإنشاء، إحصائيات الاستخدام)

Database System Utilities

Most DBMSs have database utilities that help the DBA manage the database system:

معظم أنظمة DBMS لديها أدوات مساعدة تساعد مدير قاعدة البيانات في إدارة النظام:

1. Loading Utility

  • Used to load existing data files into the database
  • Converts data from various formats (text, spreadsheet) to database format
  • Reformats data for storage in the database
  • يُستخدم لتحميل ملفات البيانات الموجودة إلى قاعدة البيانات
  • يحول البيانات من تنسيقات مختلفة (نص، جدول بيانات) إلى تنسيق قاعدة البيانات
  • يعيد تنسيق البيانات للتخزين في قاعدة البيانات

2. Backup Utility

  • Creates a backup copy of the database
  • Usually dumps entire database onto tape or other storage medium
  • Incremental backups are also supported (only changes since last backup)
  • ينشئ نسخة احتياطية من قاعدة البيانات
  • عادة يفرغ قاعدة البيانات بالكامل على شريط أو وسيط تخزين آخر
  • يدعم أيضاً النسخ الاحتياطي التزايدي (التغييرات فقط منذ آخر نسخة)

3. Database Storage Reorganization Utility

  • Reorganizes database file structures
  • Improves performance by optimizing storage
  • Creates new indexes or removes old ones
  • يعيد تنظيم هياكل ملفات قاعدة البيانات
  • يحسن الأداء عن طريق تحسين التخزين
  • ينشئ فهارس جديدة أو يزيل القديمة

4. Performance Monitoring Utility

  • Monitors database usage and provides statistics to DBA
  • Helps DBA make decisions about reorganization or optimization
  • Tracks query execution times, disk access patterns, etc.
  • يراقب استخدام قاعدة البيانات ويوفر إحصائيات لمدير قاعدة البيانات
  • يساعد المدير في اتخاذ قرارات حول إعادة التنظيم أو التحسين
  • يتتبع أوقات تنفيذ الاستعلامات وأنماط الوصول للقرص، إلخ

Other Utilities

  • Data Dictionary/Repository: Stores catalog information and design decisions
  • Report Writers/Generators: Create reports from database data
  • Application Development Environments: Tools to develop GUI applications
  • Communication Software: Allows users to access database remotely
  • قاموس/مستودع البيانات: يخزن معلومات الكتالوج وقرارات التصميم
  • مولدات التقارير: تنشئ تقارير من بيانات قاعدة البيانات
  • بيئات تطوير التطبيقات: أدوات لتطوير تطبيقات واجهة المستخدم
  • برامج الاتصال: تسمح للمستخدمين بالوصول لقاعدة البيانات عن بعد

Centralized and Client/Server Architectures for DBMSs

Database systems can be organized using different architectures based on how components are distributed.

يمكن تنظيم أنظمة قواعد البيانات باستخدام هندسات مختلفة بناءً على كيفية توزيع المكونات.

Centralized DBMS Architecture

  • All DBMS functionality, application programs, and user interface run on a single machine
  • Users access through terminals (dumb terminals with no processing power)
  • All processing is done on the central computer
  • Example: Mainframe systems
  • جميع وظائف DBMS وبرامج التطبيقات وواجهة المستخدم تعمل على جهاز واحد
  • المستخدمون يصلون من خلال طرفيات (طرفيات بدون قدرة معالجة)
  • جميع المعالجة تتم على الكمبيوتر المركزي
  • مثال: أنظمة الحاسب المركزي (Mainframe)

Basic Client/Server Architecture

  • Specialized servers with specific functionality (print server, file server, web server, database server)
  • Client machines provide user interface and local processing
  • Clients access servers when needed
  • Connected via network (LAN or Internet)
  • خوادم متخصصة بوظائف محددة (خادم الطباعة، خادم الملفات، خادم الويب، خادم قاعدة البيانات)
  • أجهزة العميل توفر واجهة المستخدم والمعالجة المحلية
  • العملاء يصلون للخوادم عند الحاجة
  • متصلون عبر شبكة (LAN أو الإنترنت)

Two-Tier Client/Server Architecture

  • Tier 1 (Client): User interface programs and application programs run on client
  • Tier 2 (Server): Query and transaction functionality provided by server
  • Client sends SQL queries to database server
  • Server processes queries and returns results
  • Uses standards like ODBC (Open Database Connectivity) and JDBC (Java Database Connectivity)
  • المستوى 1 (العميل): برامج واجهة المستخدم وبرامج التطبيقات تعمل على العميل
  • المستوى 2 (الخادم): وظائف الاستعلام والمعاملات يوفرها الخادم
  • العميل يرسل استعلامات SQL لخادم قاعدة البيانات
  • الخادم يعالج الاستعلامات ويعيد النتائج
  • يستخدم معايير مثل ODBC و JDBC

Three-Tier Client/Server Architecture

  • Tier 1 (Presentation/Client): User interface (Web browser, mobile app)
  • Tier 2 (Business Logic/Application Server): Application programs, business rules, data validation
  • Tier 3 (Database Server): DBMS and database storage
  • Common for web applications
  • Better security: clients cannot directly access database
  • المستوى 1 (العرض/العميل): واجهة المستخدم (متصفح الويب، تطبيق الجوال)
  • المستوى 2 (منطق الأعمال/خادم التطبيقات): برامج التطبيقات، قواعد الأعمال، التحقق من البيانات
  • المستوى 3 (خادم قاعدة البيانات): DBMS وتخزين قاعدة البيانات
  • شائع في تطبيقات الويب
  • أمان أفضل: العملاء لا يمكنهم الوصول مباشرة لقاعدة البيانات

Comparison: Two-Tier vs Three-Tier

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

Classification of DBMSs

1. According to Data Model

  • Hierarchical: DB as tree of nodes, access top-to-bottom using pointers
  • Network: DB as graph, access using links
  • Relational: DB as collection of relations (tables), access using values
  • Object: Uses OO language functionality

2. According to Number of Users

  • Single-user: One user at a time (PC)
  • Multi-user: Multiple users concurrently

3. According to Purpose

  • General-purpose
  • Special-purpose

4. According to Number of Sites

  • Centralized: Data stored at a single site
  • Distributed: Data distributed on many sites (Network)

1. حسب نموذج البيانات

  • الهرمي: قاعدة البيانات كشجرة، الوصول باستخدام المؤشرات
  • الشبكي: قاعدة البيانات كرسم بياني، الوصول باستخدام الروابط
  • العلائقي: قاعدة البيانات كجداول، الوصول باستخدام القيم
  • الكائني: يستخدم وظائف لغات OO

2. حسب عدد المستخدمين

  • مستخدم واحد: مستخدم واحد في كل مرة
  • متعدد المستخدمين: عدة مستخدمين في نفس الوقت

3. حسب الغرض

  • للأغراض العامة
  • لأغراض خاصة

4. حسب عدد المواقع

  • مركزي: البيانات في موقع واحد
  • موزع: البيانات موزعة على عدة مواقع (شبكة)

Three-Schema Architecture

Three-Schema Architecture External Level (View Level) View 1 View 2 View 3 View N Mapping Conceptual Level Conceptual Schema Mapping Internal Level Internal Schema Stored Database

Data Models Categories

Categories of Data Models High-Level (Conceptual) ER, OO Models WHAT is represented Representational (Implementation) Relational, Network HOW in DB Low-Level (Physical) Record Formats HOW stored User View Computer Storage

Chapter 2 - Questions

1. What is a Data Model?

A) A program to manage data
B) A set of concepts to describe database structure
C) A type of file
D) A user interface
Answer: B

2. Which is a High-Level Data Model?

A) Relational Model
B) Entity-Relationship Model
C) Network Model
D) Physical Storage Model
Answer: B

3. What is the difference between Schema and Instance?

A) No difference
B) Schema changes frequently, Instance is fixed
C) Schema describes structure (fixed), Instance is actual data (changes)
D) Schema is for data, Instance is for programs
Answer: C

4. What are the three levels in Three-Schema Architecture?

A) User, Admin, System
B) External, Conceptual, Internal
C) High, Medium, Low
D) Input, Process, Output
Answer: B

5. What is Logical Data Independence?

A) Change internal schema without changing conceptual
B) Change conceptual schema without changing external schemas
C) Change data without changing programs
D) Change hardware without changing software
Answer: B

6. What is DDL?

A) Language for manipulating data
B) Language for defining database structure
C) Language for querying
D) Language for control
Answer: B

7. High-Level DML vs Low-Level DML?

A) High-Level is faster
B) High-Level specifies "WHAT", Low-Level specifies "HOW"
C) No difference
D) High-Level is for programmers only
Answer: B

8. What is Access Path?

A) File path in the system
B) A structure that makes search for DB records efficient
C) Network link
D) User interface
Answer: B

9. Which level describes "HOW data is stored"?

A) External Level
B) Conceptual Level
C) Internal Level
D) View Level
Answer: C

10. Where does actual data exist in Three-Schema Architecture?

A) External Level
B) Conceptual Level
C) All three levels
D) Physical level only (below Internal)
Answer: D - The three schemas are only descriptions. Actual data exists only at the physical level.

Chapter 3: The Entity-Relationship Model

Chapter Contents

  1. Entity-Relationship Model Concepts
  2. Entities, Entity Types and Sets
  3. Attributes (Key, Composite, Multi-valued, Derived)
  4. Relationships (Types, Degree, Constraints)
  5. Cardinality Ratios (1:1, 1:N, M:N)
  6. Participation Constraints (Total vs Partial)
  7. Weak Entities and Identifying Relationships
  8. ER Diagram Notation
  9. Alternative Notations for ER Diagram

What is the ER Model?

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:

  • Entities - Things in the real world
  • Attributes - Properties that describe entities
  • Relationships - Associations between entities

نموذج الكيان-العلاقة (ER Model) هو نموذج بيانات مفاهيمي عالي المستوى يستخدم لوصف البيانات ككيانات وسمات وعلاقات بين الكيانات. يمكن التعبير عن البنية المنطقية الشاملة لقاعدة البيانات بيانياً من خلال مخطط ER.

يصف نموذج ER البيانات كـ:

  • الكيانات - أشياء في العالم الحقيقي
  • السمات - خصائص تصف الكيانات
  • العلاقات - روابط بين الكيانات

1. What is an Entity?

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) ويمكن تمييزه عن الأشياء المماثلة الأخرى.

الكيانات هي أشياء مهمة في العالم المصغر الموصوف في قاعدة البيانات.

2. Entity Types

Physical Entity: Tangible things like Building, Car, Person

Conceptual Entity: Abstract concepts like Student, Employee, Course

كيان مادي: أشياء ملموسة مثل مبنى، سيارة، شخص

كيان مفاهيمي: مفاهيم مجردة مثل طالب، موظف، مقرر

3. Entity Type vs Entity Set

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.

مجموعة الكيانات: مجموعة كل الكيانات من نوع كيان معين في أي وقت (الامتداد/النسخة).

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

4. Strong Entity vs Weak Entity

Strong Entity:

  • Existence does NOT depend on other entities
  • Has a key on its own
  • Notation: Single rectangle
  • Examples: Student, Course, Employee

Weak Entity:

  • Existence DEPENDS on another strong entity (owner)
  • Has a partial key (discriminator)
  • Notation: Double rectangle
  • Examples: Section (depends on Course), Dependent (depends on Employee)

الكيان القوي:

  • وجوده لا يعتمد على كيانات أخرى
  • له مفتاح خاص به
  • الرمز: مستطيل مفرد
  • أمثلة: طالب، مقرر، موظف

الكيان الضعيف:

  • وجوده يعتمد على كيان قوي آخر (المالك)
  • له مفتاح جزئي (مميز)
  • الرمز: مستطيل مزدوج
  • أمثلة: شعبة (تعتمد على المقرر)، المُعال (يعتمد على الموظف)

1. What are Attributes?

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

السمات: خصائص مهمة للكيان تصف صفاته. وهي ذات صلة بالعالم المصغر ويجب تسجيلها في قاعدة البيانات.

سيكون لكل كيان معين قيمة لكل سمة من سماته.

2. Key Attributes

Key Attribute: One or more attributes that uniquely identifies each entity of the entity set. Also called uniqueness constraint.

  • Value identifies each entity uniquely
  • Must be distinct for each entity in the entity set
  • Unique for every extension of the entity type
  • Cannot be NULL valued
  • Notation: Underlined name inside oval

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.

السمة المفتاحية: سمة واحدة أو أكثر تحدد كل كيان بشكل فريد. تسمى أيضاً قيد التفرد.

  • القيمة تحدد كل كيان بشكل فريد
  • يجب أن تكون مميزة لكل كيان
  • فريدة لكل امتداد لنوع الكيان
  • لا يمكن أن تكون قيمة فارغة (NULL)
  • الرمز: اسم مسطر داخل شكل بيضاوي

3. Value Set (Domain) of Attributes

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(السمة).

عدد عناصر النطاق: عدد القيم الممكنة التي يمكن أن يحتويها النطاق.

4. Null Values of Attributes

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) وليست سلسلة فارغة!

5. Attribute Types

Simple vs Composite Attributes

Simple (Atomic): Cannot be further divided. Example: Age, Sex, Title

Composite: Can be divided into smaller subparts with independent meanings.

  • Name(FirstName, LastName, MiddleName)
  • Address(Street, City, State, Country)
  • Phone(AreaCode, PhoneNumber, Extension)

Single-valued vs Multi-valued Attributes

Single-valued: Have only one value. Example: Gender = F

Multi-valued: Have a set of values. Notation: Double oval

  • Degree = {BSc, Master, Ph.D.}
  • Phone = {Home, Office, Mobile}
  • Color = {White, Blue, Red}

Note: In relational model, attributes are always single-valued (atomic)!

Stored vs Derived Attributes

Stored: Actually stored in the database

Derived: Can be calculated from other stored attributes. Notation: Dashed oval

  • Age can be derived from Birth_date
  • Yearly_Salary = 12 * Monthly_Salary
  • Number_of_Employees derived from counting employees in department

السمات البسيطة مقابل المركبة

بسيطة (ذرية): لا يمكن تقسيمها. مثال: العمر، الجنس

مركبة: يمكن تقسيمها إلى أجزاء أصغر بمعانٍ مستقلة.

السمات أحادية القيمة مقابل متعددة القيم

أحادية القيمة: لها قيمة واحدة فقط

متعددة القيم: لها مجموعة من القيم. الرمز: شكل بيضاوي مزدوج

السمات المخزنة مقابل المشتقة

مخزنة: تُخزن فعلياً في قاعدة البيانات

مشتقة: يمكن حسابها من سمات أخرى. الرمز: شكل بيضاوي متقطع

1. What is a Relationship?

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

نوع العلاقة: ارتباط بين نوعين أو أكثر من الكيانات. العلاقة تربط الكيانات بمعنى محدد.

مجموعة العلاقات: مجموعة من حالات العلاقة من نوع علاقة معين.

الرمز: شكل ماسي متصل بمستطيلات الكيانات بخطوط.

2. Relationship Degree

Degree: The number of participating entity types in a relationship.

  • Binary (Degree 2): Two entity types - Most common
  • Ternary (Degree 3): Three entity types
  • N-ary (Degree n): N entity types

Binary: Employee WORKS_FOR Department (2 entities)

Ternary: Supplier SUPPLIES Parts TO Projects (3 entities)

الدرجة: عدد أنواع الكيانات المشاركة في العلاقة.

  • ثنائية (درجة 2): نوعان من الكيانات - الأكثر شيوعاً
  • ثلاثية (درجة 3): ثلاثة أنواع من الكيانات
  • متعددة (درجة n): n نوع من الكيانات

3. Recursive Relationships

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)

العلاقة التكرارية: عندما يشارك نفس نوع الكيان أكثر من مرة في نفس نوع العلاقة بأدوار مختلفة.

4. Attributes of Relationship Types

Relationship types can also have attributes, similar to entity types.

  • 1:1 relationships: Attributes can be migrated to either participating entity
  • 1:N relationships: Attributes can be migrated to the N-side entity
  • M:N relationships: Attributes CANNOT be migrated - must stay with relationship

"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

يمكن أن يكون لأنواع العلاقات سمات أيضاً، مشابهة لأنواع الكيانات.

  • علاقات 1:1: يمكن نقل السمات لأي من الكيانين
  • علاقات 1:N: يمكن نقل السمات لكيان جانب N
  • علاقات M:N: لا يمكن نقل السمات - يجب أن تبقى مع العلاقة

1. Cardinality Ratio Constraints

Cardinality Ratio: Specifies the number of relationship instances that an entity can participate in.

One-to-One (1:1)

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.

One-to-Many (1:N) / Many-to-One (N:1)

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.

Many-to-Many (M:N)

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.

نسبة العدد: تحدد عدد حالات العلاقة التي يمكن أن يشارك فيها الكيان.

واحد لواحد (1:1)

كيان في A مرتبط بكيان واحد على الأكثر في B، والعكس صحيح.

واحد لكثير (1:N) / كثير لواحد (N:1)

كيان في A مرتبط بأي عدد من الكيانات في B، لكن كيان في B مرتبط بكيان واحد على الأكثر في A.

كثير لكثير (M:N)

كيان في A مرتبط بأي عدد من الكيانات في B، والعكس صحيح.

2. Participation Constraints

Participation Constraint: Specifies whether an entity MUST participate in a relationship instance or can exist independently.

Total Participation (Existence Dependency)

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)

Partial Participation

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)

قيد المشاركة: يحدد ما إذا كان الكيان يجب أن يشارك في حالة علاقة أو يمكنه الوجود بشكل مستقل.

المشاركة الكلية (اعتماد الوجود)

كل كيان من نوع الكيان يجب أن يشارك في العلاقة.

الرمز: خط مزدوج يربط الكيان بالعلاقة

المشاركة الجزئية

يمكن للكيانات أن توجد بشكل مستقل دون المشاركة في العلاقة.

الرمز: خط مفرد يربط الكيان بالعلاقة

3. Identifying Relationship (Weak Entity)

Identifying Relationship: A relationship between a strong entity and a weak entity type.

  • The key of the strong entity is required to uniquely identify instances of the weak entity
  • The weak entity has a partial key that, combined with the owner's key, uniquely identifies it
  • Notation: Double diamond for identifying relationship

COURSE (strong) --- HAS --- SECTION (weak)

Section's full key = Course_No + Section_No + Year + Semester

العلاقة المحددة: علاقة بين كيان قوي وكيان ضعيف.

  • مفتاح الكيان القوي مطلوب لتحديد حالات الكيان الضعيف بشكل فريد
  • الكيان الضعيف له مفتاح جزئي يُحدد هويته مع مفتاح المالك
  • الرمز: ماسة مزدوجة للعلاقة المحددة

Summary of ER Notations

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:

  • مستطيل = نوع كيان
  • مستطيل مزدوج = كيان ضعيف
  • ماسة = علاقة
  • ماسة مزدوجة = علاقة محددة
  • شكل بيضاوي = سمة
  • بيضاوي مسطر = سمة مفتاحية
  • بيضاوي مزدوج = سمة متعددة القيم
  • بيضاوي متقطع = سمة مشتقة
  • خط مزدوج = مشاركة كلية
  • خط مفرد = مشاركة جزئية

Alternative Notations for ER Diagrams

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. قد تستخدم الأدوات والكتب المختلفة رموزاً مختلفة، لكنها جميعاً تمثل نفس المفاهيم.

1. Chen Notation (Original)

The original notation proposed by Peter Chen in 1976:

  • Entities: Rectangles
  • Relationships: Diamonds
  • Attributes: Ovals connected to entities/relationships
  • Cardinality: Numbers (1, M, N) on relationship lines
  • Participation: Single line (partial) or double line (total)

This is the notation used throughout this course.

الرمز الأصلي الذي اقترحه بيتر تشن في 1976:

  • الكيانات: مستطيلات
  • العلاقات: ماسات
  • السمات: أشكال بيضاوية متصلة بالكيانات/العلاقات
  • العددية: أرقام (1، M، N) على خطوط العلاقة
  • المشاركة: خط مفرد (جزئية) أو خط مزدوج (كلية)

هذا هو الرمز المستخدم في هذا المقرر.

2. Crow's Foot Notation (IE Notation)

Also known as Information Engineering (IE) notation. Popular in industry tools:

  • Entities: Rectangles (with attributes listed inside)
  • Relationships: Lines connecting entities (no diamond)
  • Cardinality symbols at line ends:
    • Circle (○) = Zero (optional)
    • Line (|) = One (mandatory)
    • Crow's foot (⋈) = Many
Symbol Meaning
|| One and only one (mandatory)
○| Zero or one (optional)
|⋈ One or many (mandatory)
○⋈ Zero or many (optional)

يُعرف أيضاً برمز هندسة المعلومات (IE). شائع في أدوات الصناعة:

  • الكيانات: مستطيلات (مع السمات مدرجة بداخلها)
  • العلاقات: خطوط تربط الكيانات (بدون ماسة)
  • رموز العددية في نهايات الخطوط:
    • دائرة (○) = صفر (اختياري)
    • خط (|) = واحد (إلزامي)
    • قدم الغراب (⋈) = متعدد

3. (Min, Max) Notation

Specifies the minimum and maximum number of relationship instances:

  • Written as (min, max) next to the entity
  • min = 0: Partial participation
  • min = 1: Total participation
  • max = 1: Each entity participates in at most one relationship
  • max = N: No limit on participation

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)

يحدد الحد الأدنى والأقصى لعدد حالات العلاقة:

  • يُكتب كـ (min, max) بجانب الكيان
  • min = 0: مشاركة جزئية
  • min = 1: مشاركة كلية
  • max = 1: كل كيان يشارك في علاقة واحدة على الأكثر
  • max = N: لا حد للمشاركة

4. UML Class Diagram Notation

UML (Unified Modeling Language) can also represent ER concepts:

  • Entities: Classes (rectangles with 3 compartments: name, attributes, methods)
  • Relationships: Associations (lines with multiplicity)
  • Cardinality: Written as multiplicity (1, 0..1, 1..*, 0..*)
  • Composition: Filled diamond (strong ownership)
  • Aggregation: Empty diamond (weak ownership)
UML Multiplicity Meaning
1 Exactly one
0..1 Zero or one
1..* One or more
0..* or * Zero or more

يمكن لـ UML (لغة النمذجة الموحدة) أيضاً تمثيل مفاهيم ER:

  • الكيانات: فئات (مستطيلات بـ 3 أقسام: الاسم، السمات، الطرق)
  • العلاقات: ارتباطات (خطوط مع التعددية)
  • العددية: تُكتب كتعددية (1، 0..1، 1..*، 0..*)
  • التركيب: ماسة ممتلئة (ملكية قوية)
  • التجميع: ماسة فارغة (ملكية ضعيفة)

Comparison of Notations

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 Diagram Symbols

Entity Strong Entity Weak Weak Entity Relation Relationship Identify Identifying Rel. Name Simple Attribute SSN Key Attribute Phone Multi-valued Age Derived Name First Last Composite EMPLOYEE N WORKS_FOR 1 DEPARTMENT Many-to-One (N:1) Cardinality EMPLOYEE WORKS_FOR DEPARTMENT Total Participation (double lines) on both sides

COMPANY Database ER Diagram

EMPLOYEE SSN Name Salary DEPARTMENT Number Name Locations N WORKS_FOR 1 1 MANAGES 1 PROJECT Number Name Location 1 CONTROLS N M WORKS_ON N Hours DEPENDENT DEP_OF 1 N Name Sex Legend = Total Participation = Partial Participation

مخطط ER لقاعدة بيانات الشركة يُظهر:

  • الكيانات: EMPLOYEE, DEPARTMENT, PROJECT, DEPENDENT
  • العلاقات: WORKS_FOR (N:1), MANAGES (1:1), CONTROLS (1:N), WORKS_ON (M:N)
  • DEPENDENT كيان ضعيف مرتبط بـ EMPLOYEE عبر علاقة محددة
  • الخط المزدوج يدل على المشاركة الكلية

Chapter 3 Questions

Multiple Choice Questions

1. What is an Entity in ER Model?

A) A "thing" in real world that can be identified
B) A programming variable
C) A SQL query
D) A database table only
Answer: A - An entity is a "thing" in real world such as an object, subject, or event that can be identified and distinguished from others.

2. Which notation represents a Weak Entity in ER Diagram?

A) Single rectangle
B) Double rectangle
C) Diamond shape
D) Oval shape
Answer: B - Weak entity is represented by double rectangle. It depends on a strong entity for identification.

3. What type of attribute can have multiple values?

A) Simple attribute
B) Key attribute
C) Multi-valued attribute
D) Derived attribute
Answer: C - Multi-valued attributes can have a set of values for a particular entity (e.g., Phone numbers).

4. A derived attribute is represented by:

A) Solid oval
B) Double oval
C) Underlined text in oval
D) Dashed oval
Answer: D - Derived attributes are shown with dashed/dotted ovals because they can be calculated from other attributes.

5. In a 1:N relationship, where can relationship attributes be migrated?

A) To the 1-side entity only
B) To the N-side entity only
C) To either entity
D) Cannot be migrated at all
Answer: B - In 1:N relationships, attributes can only be migrated to the N-side entity.

6. Total participation is shown with:

A) Double line
B) Single line
C) Dashed line
D) Arrow
Answer: A - Total participation (existence dependency) is shown with double line connecting entity to relationship.

7. What is the degree of a ternary relationship?

A) 1
B) 2
C) 3
D) Many
Answer: C - A ternary relationship involves 3 entity types. Degree = number of participating entities.

8. NULL value means:

A) Zero
B) Empty string
C) Space character
D) Unknown or not applicable
Answer: D - NULL represents unknown value or not applicable. It is NOT zero and NOT blank string.

9. A recursive relationship is when:

A) Two different entities are related
B) Same entity participates twice in different roles
C) Three entities are related
D) No entities are related
Answer: B - Recursive relationship: same entity type participates more than once in different roles (e.g., Employee supervises Employee).

10. In M:N relationship, attributes:

A) Can be migrated to any entity
B) Must be on the M-side
C) Must be on the N-side
D) Cannot be migrated - must stay with relationship
Answer: D - In M:N relationships, attributes cannot be migrated because they depend on the combination of both entities.

True/False Questions

1. A key attribute can have NULL value.

A) True
B) False
Answer: False - Key attributes cannot be NULL because they must uniquely identify each entity.

2. A weak entity has a partial key.

A) True
B) False
Answer: True - Weak entity has partial key (discriminator) that combined with owner's key uniquely identifies it.

3. In relational model, attributes can be multi-valued.

A) True
B) False
Answer: False - In relational model, attributes are always single-valued (atomic). Values must be atomic!

4. An identifying relationship connects a weak entity to its owner entity.

A) True
B) False
Answer: True - Identifying relationship connects weak entity to strong (owner) entity and is shown as double diamond.

5. Partial participation means every entity must participate in the relationship.

A) True
B) False
Answer: False - Partial participation means entities CAN exist without participating. Total participation means MUST participate.

Chapter 4: Relational Data Model

Chapter 4: Relational Data Model

EN: The Relational Model of Data was introduced by E.F. Codd (IBM Research) in 1970. It is based on the concept of a mathematical Relation. The relational model is now the most popular data model for commercial DBMSs. SQL is the standard relational database language based on the relational data model.
AR: تم تقديم النموذج العلائقي للبيانات من قبل E.F. Codd من شركة IBM للأبحاث في عام 1970. يعتمد على مفهوم العلاقة الرياضية (Mathematical Relation). النموذج العلائقي هو الآن أكثر نماذج البيانات شيوعاً في أنظمة إدارة قواعد البيانات التجارية. SQL هي لغة قواعد البيانات العلائقية القياسية المبنية على هذا النموذج.

Relation (Table) Definition

EN: A Relation (or Table) R is defined as: R(A₁, A₂, ..., Aₙ) where A₁, A₂, ..., Aₙ are attributes. A relation is a set of tuples (rows). Each tuple is an ordered list of n values t = <v₁, v₂, ..., vₙ> where each value vᵢ is an element of dom(Aᵢ) (the domain of attribute Aᵢ) or NULL.
AR: يتم تعريف العلاقة (أو الجدول) R على النحو التالي: R(A₁, A₂, ..., Aₙ) حيث A₁, A₂, ..., Aₙ هي السمات (Attributes). العلاقة هي مجموعة من الصفوف (Tuples). كل صف هو قائمة مرتبة من n قيمة t = <v₁, v₂, ..., vₙ> حيث كل قيمة vᵢ هي عنصر من dom(Aᵢ) (نطاق السمة Aᵢ) أو NULL.

Key Terminology

EN:
  • Relation (Table): A set of tuples representing entities/relationships
  • Attribute (Column): A property or characteristic of an entity
  • Tuple (Row/Record): A single instance with values for each attribute
  • Domain: The set of allowable values for an attribute
  • Degree (Arity): Number of attributes in a relation
  • Cardinality: Number of tuples in a relation
AR:
  • العلاقة (الجدول): مجموعة من الصفوف تمثل الكيانات أو العلاقات
  • السمة (العمود): خاصية أو ميزة لكيان
  • الصف (السجل): مثيل واحد يحتوي على قيم لكل سمة
  • النطاق (Domain): مجموعة القيم المسموح بها للسمة
  • الدرجة (Degree): عدد السمات في العلاقة
  • العدد الأساسي (Cardinality): عدد الصفوف في العلاقة

Domain

EN: A Domain D is a set of atomic (indivisible) values. Each domain has a logical definition (meaning) and a data type or format. Examples: USA_phone_numbers (10-digit character string), SSN (9-digit string), Names (character strings). A domain is typically specified by: data-type, format, and range/values.
AR: النطاق D هو مجموعة من القيم الذرية (غير القابلة للتجزئة). لكل نطاق تعريف منطقي (معنى) ونوع بيانات أو تنسيق. أمثلة: أرقام الهواتف الأمريكية (سلسلة أحرف من 10 أرقام)، SSN (سلسلة من 9 أرقام)، الأسماء (سلاسل أحرف). يتم تحديد النطاق عادةً بواسطة: نوع البيانات، التنسيق، والنطاق/القيم.

Attribute

EN: An Attribute A is the name of a role played by some domain D in the relation. D is called the domain of A, denoted as dom(A). Different attributes can have the same domain. Example: Home_phone and Office_phone can both have the domain Phone_numbers but play different roles.
AR: السمة A هي اسم دور يلعبه نطاق معين D في العلاقة. يُسمى D نطاق السمة A، ويُرمز له بـ dom(A). يمكن أن يكون لسمات مختلفة نفس النطاق. مثال: هاتف المنزل وهاتف المكتب يمكن أن يكون لهما نفس نطاق أرقام الهواتف لكنهما يلعبان أدواراً مختلفة.

Tuple

EN: A tuple t is an ordered set of values <v₁, v₂, ..., vₙ> corresponding to the attributes of a relation. Each value vᵢ is an element of dom(Aᵢ) or is a special NULL value. The ith value in tuple t, which corresponds to attribute Aᵢ, is referred to as t[Aᵢ] or t.Aᵢ.
AR: الصف t هو مجموعة مرتبة من القيم <v₁, v₂, ..., vₙ> تتوافق مع سمات العلاقة. كل قيمة vᵢ هي عنصر من dom(Aᵢ) أو قيمة NULL خاصة. يُشار إلى القيمة i في الصف t التي تتوافق مع السمة Aᵢ بـ t[Aᵢ] أو t.Aᵢ.

Relation Schema vs Relation Instance

EN:
  • Relation Schema R(A₁, A₂, ..., Aₙ): The "intention" - describes the structure including relation name, attribute names, and their domains. It is relatively static (rarely changes).
  • Relation Instance r(R): The "extension" - the actual set of tuples (rows) in the relation at any given time. It changes frequently as data is inserted, deleted, or updated.
  • Degree: The number of attributes n in the schema
  • Cardinality: The number of tuples in the current instance
AR:
  • مخطط العلاقة R(A₁, A₂, ..., Aₙ): "النية" - يصف الهيكل بما في ذلك اسم العلاقة وأسماء السمات ونطاقاتها. نادراً ما يتغير.
  • مثيل العلاقة r(R): "الامتداد" - المجموعة الفعلية من الصفوف في العلاقة في أي وقت. يتغير بشكل متكرر عند إدخال البيانات أو حذفها أو تحديثها.
  • الدرجة (Degree): عدد السمات n في المخطط
  • العدد الأساسي (Cardinality): عدد الصفوف في المثيل الحالي

Characteristics of Relations

EN:
  1. Ordering of tuples: Tuples in a relation are NOT ordered (a relation is a SET of tuples)
  2. Ordering of values in a tuple: Values within a tuple ARE ordered (alternative: self-describing tuples)
  3. Values in tuples: All values are considered atomic (indivisible) - flat relational model
  4. No duplicate tuples: A relation cannot have two identical tuples (key constraint)
  5. NULL values: Represent unknown, not available, or not applicable values
AR:
  1. ترتيب الصفوف: الصفوف في العلاقة ليست مرتبة (العلاقة هي مجموعة SET من الصفوف)
  2. ترتيب القيم في الصف: القيم داخل الصف مرتبة
  3. القيم في الصفوف: جميع القيم تعتبر ذرية (غير قابلة للتجزئة) - النموذج العلائقي المسطح
  4. لا توجد صفوف مكررة: لا يمكن أن تحتوي العلاقة على صفين متطابقين (قيد المفتاح)
  5. قيم NULL: تمثل قيماً غير معروفة أو غير متاحة أو غير قابلة للتطبيق

Superkey

EN: A Superkey (SK) of a relation R is a set of attributes that contains at least one unique identifier for every tuple. No two tuples can have the same values for SK. Any superset of a superkey is also a superkey. A superkey may contain redundant attributes.
AR: المفتاح الفائق (Superkey) للعلاقة R هو مجموعة من السمات تحتوي على معرف فريد واحد على الأقل لكل صف. لا يمكن لصفين أن يكون لهما نفس القيم للمفتاح الفائق. أي مجموعة شاملة للمفتاح الفائق هي أيضاً مفتاح فائق. قد يحتوي المفتاح الفائق على سمات زائدة عن الحاجة.

Candidate Key

EN: A Candidate Key (CK) is a minimal superkey - a superkey with no redundant attributes. If we remove any attribute from a candidate key, it is no longer a superkey. A relation may have multiple candidate keys. Example: In EMPLOYEE, both SSN and {Fname, Lname, Bdate} could be candidate keys.
AR: المفتاح المرشح (Candidate Key) هو مفتاح فائق أدنى - مفتاح فائق بدون سمات زائدة. إذا أزلنا أي سمة من المفتاح المرشح، لن يعد مفتاحاً فائقاً. قد تحتوي العلاقة على عدة مفاتيح مرشحة. مثال: في جدول الموظفين، كل من SSN و{الاسم الأول، الاسم الأخير، تاريخ الميلاد} يمكن أن يكونا مفاتيح مرشحة.

Primary Key

EN: The Primary Key (PK) is the candidate key chosen by the database designer to uniquely identify tuples. It is underlined in the relation schema. Primary key values cannot be NULL (Entity Integrity Constraint). Each relation must have a primary key. Example: EMPLOYEE(SSN, Fname, Lname, Bdate, Address).
AR: المفتاح الأساسي (Primary Key) هو المفتاح المرشح الذي يختاره مصمم قاعدة البيانات لتحديد الصفوف بشكل فريد. يتم وضع خط تحته في مخطط العلاقة. لا يمكن أن تكون قيم المفتاح الأساسي NULL (قيد سلامة الكيان). يجب أن تحتوي كل علاقة على مفتاح أساسي. مثال: EMPLOYEE(SSN, Fname, Lname, Bdate, Address).

Composite Key

EN: A Composite Key is a key that consists of two or more attributes. Example: In a relation ENROLLMENT(Student_ID, Course_ID, Grade), the combination of {Student_ID, Course_ID} forms a composite primary key.
AR: المفتاح المركب (Composite Key) هو مفتاح يتكون من سمتين أو أكثر. مثال: في علاقة التسجيل ENROLLMENT(Student_ID, Course_ID, Grade)، يشكل مزيج {Student_ID, Course_ID} مفتاحاً أساسياً مركباً.

Foreign Key

EN: A Foreign Key (FK) is a set of attributes in one relation that references the primary key of another relation (or the same relation). It creates a link between two relations. The FK must either contain a value matching a PK value in the referenced relation, or be NULL. Example: Dno in EMPLOYEE references Dnumber in DEPARTMENT.
AR: المفتاح الأجنبي (Foreign Key) هو مجموعة من السمات في علاقة واحدة تشير إلى المفتاح الأساسي لعلاقة أخرى (أو نفس العلاقة). ينشئ رابطاً بين علاقتين. يجب أن يحتوي المفتاح الأجنبي إما على قيمة تطابق قيمة المفتاح الأساسي في العلاقة المرجعية، أو يكون NULL. مثال: Dno في جدول الموظفين يشير إلى Dnumber في جدول الأقسام.

Keys Summary Table

EN:
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)
AR: جدول ملخص المفاتيح: المفتاح الفائق يمكن أن يحتوي على سمات زائدة ويمكن أن يحتوي على NULL. المفتاح المرشح هو مفتاح فائق أدنى ولا يمكن أن يكون NULL. المفتاح الأساسي هو المفتاح المختار ولا يمكن أن يكون NULL أبداً (قيد سلامة الكيان). المفتاح الأجنبي يشير إلى مفتاح أساسي في علاقة أخرى ويمكن أن يكون NULL في حالة المشاركة الجزئية.

Relational Integrity Constraints

EN: Constraints are conditions that must hold on all valid relation instances. There are three main types of relational integrity constraints:
  1. Key Constraints: No duplicate tuples (uniqueness)
  2. Entity Integrity Constraints: No NULL primary keys
  3. Referential Integrity Constraints: Foreign keys must match existing primary keys or be NULL
Another type is Domain Constraints which specifies that values must be from the attribute's domain.
AR: القيود هي شروط يجب أن تتحقق في جميع مثيلات العلاقة الصالحة. هناك ثلاثة أنواع رئيسية من قيود السلامة العلائقية:
  1. قيود المفتاح: لا توجد صفوف مكررة (التفرد)
  2. قيود سلامة الكيان: لا يمكن أن يكون المفتاح الأساسي NULL
  3. قيود السلامة المرجعية: المفاتيح الأجنبية يجب أن تطابق مفاتيح أساسية موجودة أو تكون NULL
نوع آخر هو قيود النطاق التي تحدد أن القيم يجب أن تكون من نطاق السمة.

Domain Constraints

EN: Domain Constraints specify that within each tuple, the value of each attribute A must be an atomic value from the domain dom(A). The data types associated with domains include: integers, real numbers, characters, fixed-length strings, variable-length strings, date, time, timestamp, etc.
AR: تحدد قيود النطاق أنه داخل كل صف، يجب أن تكون قيمة كل سمة A قيمة ذرية من النطاق dom(A). تشمل أنواع البيانات المرتبطة بالنطاقات: الأعداد الصحيحة، الأعداد الحقيقية، الأحرف، السلاسل ذات الطول الثابت، السلاسل ذات الطول المتغير، التاريخ، الوقت، الطابع الزمني، إلخ.

Key Constraints

EN: Key Constraints state that no two tuples in a relation can have the same combination of values for all their attributes. This is based on the definition that a relation is a SET of tuples, and sets do not allow duplicate elements. The superkey uniquely identifies any tuple. A candidate key is a minimal superkey.
AR: تنص قيود المفتاح على أنه لا يمكن لصفين في علاقة أن يكون لهما نفس مجموعة القيم لجميع سماتهما. هذا مبني على التعريف بأن العلاقة هي مجموعة (SET) من الصفوف، والمجموعات لا تسمح بعناصر مكررة. المفتاح الفائق يحدد أي صف بشكل فريد. المفتاح المرشح هو مفتاح فائق أدنى.

Entity Integrity Constraint

EN: The primary key cannot be NULL in any tuple of a relation. This is because the primary key is used to identify individual tuples. If PK = NULL, we cannot identify certain tuples. Note: Other attributes (non-PK) can be NULL. For composite primary keys, no individual attribute can be NULL.
AR: المفتاح الأساسي لا يمكن أن يكون NULL في أي صف من العلاقة. هذا لأن المفتاح الأساسي يُستخدم لتحديد الصفوف الفردية. إذا كان PK = NULL، لا يمكننا تحديد صفوف معينة. ملاحظة: السمات الأخرى (غير المفتاح الأساسي) يمكن أن تكون NULL. للمفاتيح الأساسية المركبة، لا يمكن لأي سمة فردية أن تكون NULL.

Referential Integrity Constraint

EN: A referential integrity constraint is specified between two relations and is used to maintain consistency among tuples. It states: A tuple in the referencing relation (R1) that refers to another relation (R2) must refer to an existing tuple in R2.

If a foreign key FK in R1 references PK in R2, then for each tuple t1 in R1:
  • Either t1[FK] = NULL
  • Or there exists a tuple t2 in R2 such that t1[FK] = t2[PK]
AR: يُحدد قيد السلامة المرجعية بين علاقتين ويُستخدم للحفاظ على الاتساق بين الصفوف. ينص على: الصف في العلاقة المرجعية (R1) الذي يشير إلى علاقة أخرى (R2) يجب أن يشير إلى صف موجود في R2.

إذا كان المفتاح الأجنبي FK في R1 يشير إلى PK في R2، فلكل صف t1 في R1:
  • إما t1[FK] = NULL
  • أو يوجد صف t2 في R2 بحيث t1[FK] = t2[PK]

Constraint Violations

EN: Database operations that can cause constraint violations:
  • INSERT: Can violate domain constraints, key constraints, entity integrity (NULL PK), or referential integrity
  • DELETE: Can violate referential integrity (if tuple is referenced by FK)
  • UPDATE (MODIFY): Can violate any constraint depending on what is changed
When referential integrity is violated on DELETE/UPDATE, options include: RESTRICT (reject), CASCADE (propagate), SET NULL, or SET DEFAULT.
AR: عمليات قاعدة البيانات التي يمكن أن تسبب انتهاكات للقيود:
  • الإدراج (INSERT): يمكن أن ينتهك قيود النطاق، قيود المفتاح، سلامة الكيان (PK فارغ)، أو السلامة المرجعية
  • الحذف (DELETE): يمكن أن ينتهك السلامة المرجعية (إذا كان الصف مُشاراً إليه بمفتاح أجنبي)
  • التحديث (UPDATE): يمكن أن ينتهك أي قيد حسب ما يتم تغييره
عند انتهاك السلامة المرجعية عند الحذف/التحديث، تشمل الخيارات: RESTRICT (رفض)، CASCADE (نشر)، SET NULL، أو SET DEFAULT.

Update Operations on Relations

There are three basic operations that can change the states of relations in a database:

  • INSERT: Add a new tuple to a relation
  • DELETE: Remove an existing tuple from a relation
  • UPDATE (MODIFY): Change values of some attributes in an existing tuple

Each operation may cause constraint violations, and the DBMS must handle them appropriately.

هناك ثلاث عمليات أساسية يمكنها تغيير حالات العلاقات في قاعدة البيانات:

  • الإدراج (INSERT): إضافة صف جديد إلى العلاقة
  • الحذف (DELETE): إزالة صف موجود من العلاقة
  • التحديث (UPDATE/MODIFY): تغيير قيم بعض السمات في صف موجود

كل عملية قد تسبب انتهاكات للقيود، ويجب على DBMS التعامل معها بشكل مناسب.

1. INSERT Operation

Purpose: Add a new tuple to a relation R

Syntax: INSERT INTO R VALUES (v1, v2, ..., vn)

Possible Constraint Violations:

  • Domain Constraint: If an attribute value is not in the correct domain
    Example: Inserting Age = 'Twenty' when Age must be integer
  • Key Constraint: If the key value already exists in the relation
    Example: Inserting a student with SSN that already exists
  • Entity Integrity: If the primary key value is NULL
    Example: Inserting a student with NULL Student_ID
  • Referential Integrity: If a foreign key value does not exist in the referenced relation
    Example: Inserting an employee with Dept_No = 9 when no department 9 exists

If violation occurs: REJECT the insertion

الغرض: إضافة صف جديد إلى العلاقة R

الصيغة: INSERT INTO R VALUES (v1, v2, ..., vn)

انتهاكات القيود المحتملة:

  • قيد النطاق: إذا كانت قيمة السمة ليست في النطاق الصحيح
  • قيد المفتاح: إذا كانت قيمة المفتاح موجودة بالفعل
  • سلامة الكيان: إذا كانت قيمة المفتاح الأساسي NULL
  • السلامة المرجعية: إذا كانت قيمة المفتاح الأجنبي غير موجودة في العلاقة المُشار إليها

عند حدوث انتهاك: رفض الإدراج

2. DELETE Operation

Purpose: Remove a tuple from a relation R

Syntax: DELETE FROM R WHERE condition

Possible Constraint Violations:

  • Referential Integrity ONLY: If the tuple being deleted is referenced by a foreign key in another relation
    Example: Deleting a department that has employees assigned to it

Note: DELETE cannot violate domain, key, or entity integrity constraints.

الغرض: إزالة صف من العلاقة R

الصيغة: DELETE FROM R WHERE condition

انتهاكات القيود المحتملة:

  • السلامة المرجعية فقط: إذا كان الصف المحذوف مُشاراً إليه بمفتاح أجنبي في علاقة أخرى

ملاحظة: الحذف لا يمكن أن ينتهك قيود النطاق أو المفتاح أو سلامة الكيان.

3. UPDATE (MODIFY) Operation

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:

  • If updating a NON-KEY attribute:
    • Domain Constraint - if new value not in domain
  • If updating a PRIMARY KEY:
    • Similar to DELETE + INSERT
    • Key Constraint - if new PK value already exists
    • Entity Integrity - if new PK value is NULL
    • Referential Integrity - if old PK was referenced by FK
  • If updating a FOREIGN KEY:
    • Referential Integrity - if new FK value doesn't exist in referenced relation

الغرض: تغيير قيمة سمة أو أكثر في صف موجود

الصيغة: UPDATE R SET attribute = value WHERE condition

انتهاكات القيود المحتملة:

  • إذا تم تحديث سمة غير مفتاحية: قيد النطاق فقط
  • إذا تم تحديث المفتاح الأساسي: مشابه للحذف + الإدراج (قيد المفتاح، سلامة الكيان، السلامة المرجعية)
  • إذا تم تحديث المفتاح الأجنبي: السلامة المرجعية

Dealing with Constraint Violations

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 التعامل معه باستخدام أحد الخيارات التالية:

  • RESTRICT (تقييد): رفض العملية التي تسبب الانتهاك
  • CASCADE (تتالي): نشر العملية إلى الصفوف المُشيرة
  • SET NULL: تعيين قيمة FK إلى NULL في الصفوف المُشيرة
  • SET DEFAULT: تعيين قيمة FK إلى القيمة الافتراضية

Summary: Operations vs Constraints

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

Examples of Update Operations

Given: EMPLOYEE(SSN, Name, Dept_No) and DEPARTMENT(Dept_No, Dept_Name)

1. INSERT Example:

INSERT INTO EMPLOYEE VALUES ('123456789', 'John Smith', 5)
  • ✓ Valid if SSN is unique and Department 5 exists
  • ✗ Violates Key Constraint if SSN already exists
  • ✗ Violates Referential Integrity if Department 5 doesn't exist

2. DELETE Example:

DELETE FROM DEPARTMENT WHERE Dept_No = 5
  • ✓ Valid if no employees reference Department 5
  • ✗ Violates Referential Integrity if employees work in Department 5
  • Solution: Use CASCADE, SET NULL, or SET DEFAULT

3. UPDATE Example:

UPDATE EMPLOYEE SET Dept_No = 9 WHERE SSN = '123456789'
  • ✓ Valid if Department 9 exists
  • ✗ Violates Referential Integrity if Department 9 doesn't exist

معطى: EMPLOYEE(SSN, Name, Dept_No) و DEPARTMENT(Dept_No, Dept_Name)

مثال INSERT: إدراج موظف جديد - يجب أن يكون SSN فريداً والقسم موجوداً

مثال DELETE: حذف قسم - قد ينتهك السلامة المرجعية إذا كان هناك موظفون

مثال UPDATE: تحديث قسم الموظف - يجب أن يكون القسم الجديد موجوداً

Relation Example: STUDENT

Student_ID Name Email Major S001 Ahmad Ali ahmad@uni.edu CS S002 Sara Omar sara@uni.edu Math S003 Khalid Noor khalid@uni.edu CS = Primary Key (underlined)
AR: جدول الطلاب: Student_ID هو المفتاح الأساسي (مُسطَّر). الدرجة (Degree) = 4 سمات. العدد الأساسي (Cardinality) = 3 صفوف.

Foreign Key Relationship

DEPARTMENT Dnumber Dname Mgr_SSN 1 Research 333445555 4 Admin 987654321 5 Headquarters 888665555 EMPLOYEE SSN Fname Salary Dno 123456789 John 30000 5 333445555 Franklin 40000 5 987654321 Jennifer 43000 4 Dno references Dnumber (Foreign Key → Primary Key)
AR: يوضح هذا المخطط العلاقة بين جدولي DEPARTMENT و EMPLOYEE. السمة Dno في جدول الموظفين هي مفتاح أجنبي يشير إلى Dnumber (المفتاح الأساسي) في جدول الأقسام. هذا يضمن أن كل موظف ينتمي إلى قسم موجود فعلاً.

Key Hierarchy Diagram

All Attributes of Relation Superkey (may have redundant attributes) Candidate Key(s) (minimal superkey - no redundancy) Primary Key (chosen)
AR: التسلسل الهرمي للمفاتيح: جميع السمات ← المفتاح الفائق (قد يحتوي على سمات زائدة) ← المفتاح المرشح (مفتاح فائق أدنى) ← المفتاح الأساسي (المفتاح المختار للتعريف).

📝 Chapter 4 Quiz

Multiple Choice Questions

1. Who introduced the Relational Model?

A) E.F. Codd at IBM (1970)
B) Charles Bachman
C) Peter Chen
D) Oracle Corporation
Answer: A - The Relational Model was introduced by E.F. Codd at IBM Research in 1970.

2. What is the Degree of a relation?

A) Number of tuples
B) Number of attributes
C) Number of domains
D) Number of keys
Answer: B - Degree (Arity) is the number of attributes n in a relation schema R(A₁, A₂, ..., Aₙ).

3. What is the Cardinality of a relation?

A) Number of attributes
B) Number of domains
C) Number of tuples (rows)
D) Number of keys
Answer: C - Cardinality is the number of tuples in the current relation instance.

4. A Candidate Key is:

A) A minimal superkey with no redundant attributes
B) Any superkey
C) A foreign key
D) A key that may have NULL values
Answer: A - A Candidate Key is a minimal superkey - removing any attribute would make it no longer a superkey.

5. Entity Integrity Constraint states that:

A) Foreign keys must exist
B) All attributes must have values
C) Domains must be defined
D) Primary key cannot be NULL
Answer: D - Entity Integrity states that the primary key cannot be NULL because it is used to identify individual tuples.

6. Referential Integrity Constraint means:

A) All keys must be numeric
B) A foreign key must match an existing primary key or be NULL
C) All attributes must have unique values
D) Tables must have at least one row
Answer: B - Referential Integrity states that a FK must either contain a value matching a PK in the referenced relation, or be NULL.

7. In a relation, tuples are:

A) Always sorted by primary key
B) Ordered alphabetically
C) NOT ordered (relation is a SET)
D) Ordered by insertion time
Answer: C - Tuples in a relation are NOT ordered because a relation is defined as a SET of tuples.

8. What is a Domain in the relational model?

A) A set of atomic (indivisible) values
B) A set of tuples
C) A table name
D) A foreign key
Answer: A - A Domain D is a set of atomic (indivisible) values. Each domain has a logical definition and data type.

9. The difference between Relation Schema and Relation Instance is:

A) Schema has data, instance has structure
B) They are the same thing
C) Instance is always empty
D) Schema describes structure (rarely changes), Instance is actual data (changes frequently)
Answer: D - Schema is the "intention" (structure), Instance is the "extension" (actual data). Schema is static, Instance changes frequently.

10. Which operation can violate ALL types of integrity constraints?

A) INSERT
B) DELETE
C) SELECT
D) None of the above
Answer: A - INSERT can violate domain constraints, key constraints, entity integrity (NULL PK), and referential integrity.

True/False Questions

1. A superkey can have redundant attributes.

A) True
B) False
Answer: True - A superkey may contain redundant attributes. A candidate key is a superkey with NO redundant attributes (minimal).

2. A relation can have duplicate tuples.

A) True
B) False
Answer: False - A relation is a SET of tuples, and sets do not allow duplicate elements (Key Constraint).

3. A foreign key can have NULL value.

A) True
B) False
Answer: True - A foreign key can be NULL (if partial participation) OR must match an existing PK value in the referenced relation.

4. Values in tuples must be atomic (indivisible) in the relational model.

A) True
B) False
Answer: True - This is called the flat relational model. All values are considered atomic (indivisible). Multi-valued and composite attributes from ER must be decomposed.

5. A relation can have multiple primary keys.

A) True
B) False
Answer: False - A relation can have multiple CANDIDATE keys, but only ONE PRIMARY key is chosen by the database designer.

Chapter 5: ER-to-Relational Mapping

Chapter 5: ER-to-Relational Mapping

EN: This chapter covers the method for mapping a conceptual schema developed using the ER model to a relational database schema. The mapping algorithm comprises 7 steps that systematically convert ER constructs (entities, relationships, attributes) into relational tables with appropriate keys and constraints.
AR: يغطي هذا الفصل طريقة تحويل المخطط المفاهيمي المطور باستخدام نموذج ER إلى مخطط قاعدة بيانات علائقية. تتكون خوارزمية التحويل من 7 خطوات تقوم بشكل منهجي بتحويل مكونات ER (الكيانات، العلاقات، السمات) إلى جداول علائقية مع المفاتيح والقيود المناسبة.

The 7 Mapping Steps

EN:
  1. Step 1: Mapping of Regular Entity Types
  2. Step 2: Mapping of Weak Entity Types
  3. Step 3: Binary 1:1 Relationship Type Mapping
  4. Step 4: Binary 1:N Relationship Type Mapping
  5. Step 5: Binary M:N Relationship Type Mapping
  6. Step 6: Multi-valued Attribute Mapping
  7. Step 7: N-ary Relationship Type Mapping
AR:
  1. الخطوة 1: تحويل أنواع الكيانات العادية
  2. الخطوة 2: تحويل أنواع الكيانات الضعيفة
  3. الخطوة 3: تحويل علاقات 1:1 الثنائية
  4. الخطوة 4: تحويل علاقات 1:N الثنائية
  5. الخطوة 5: تحويل علاقات M:N الثنائية
  6. الخطوة 6: تحويل السمات متعددة القيم
  7. الخطوة 7: تحويل العلاقات N-ary

ER to Relational Correspondence

EN:
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
AR: جدول التوافق بين نموذج ER والنموذج العلائقي: نوع الكيان → جدول، العلاقة 1:1 أو 1:N → مفتاح أجنبي، العلاقة M:N → جدول علاقة مع مفتاحين أجنبيين، السمة البسيطة → عمود، السمة المركبة → مجموعة أعمدة بسيطة، السمة متعددة القيم → جدول جديد مع مفتاح أجنبي.

Step 1: Mapping Regular Entity Types

EN: For each regular (strong) entity type E, create a relation R that includes all the simple attributes of E.
  • Include only simple component attributes of a composite attribute (not the composite attribute itself)
  • Choose one key attribute of E as primary key for R
  • If the key of E is composite, the set of simple attributes together form the primary key
AR: لكل نوع كيان عادي (قوي) E، أنشئ علاقة R تتضمن جميع السمات البسيطة لـ E.
  • قم بتضمين السمات البسيطة المكونة للسمة المركبة فقط (وليس السمة المركبة نفسها)
  • اختر سمة مفتاح واحدة من E كمفتاح أساسي لـ R
  • إذا كان مفتاح E مركباً، فإن مجموعة السمات البسيطة معاً تشكل المفتاح الأساسي

Step 1 Example: Company Database

EN: Entity Types: EMPLOYEE, DEPARTMENT, PROJECT

Results:
  • EMPLOYEE (Ssn, Fname, Minit, Lname, Bdate, Address, Sex, Salary)
  • DEPARTMENT (Dnumber, Dname)
  • PROJECT (Pnumber, Pname, Plocation)

Note: Composite attribute "Name" is decomposed into Fname, Minit, Lname. The underlined attributes are primary keys.
AR: أنواع الكيانات: EMPLOYEE، DEPARTMENT، PROJECT

النتائج:
  • EMPLOYEE (Ssn, Fname, Minit, Lname, Bdate, Address, Sex, Salary)
  • DEPARTMENT (Dnumber, Dname)
  • PROJECT (Pnumber, Pname, Plocation)

ملاحظة: السمة المركبة "Name" تم تفكيكها إلى Fname، Minit، Lname. السمات المسطرة هي المفاتيح الأساسية.

Step 2: Mapping Weak Entity Types

EN: For each weak entity type W with owner entity type E:
  • Create a relation R and include all simple attributes of W as attributes of R
  • Include the primary key of the owner entity E as a foreign key in R
  • The primary key of R is the combination of:
    • The primary key of the owner entity (as FK)
    • The partial key (discriminator) of the weak entity
AR: لكل نوع كيان ضعيف W مع نوع كيان مالك E:
  • أنشئ علاقة R وضمّن جميع السمات البسيطة لـ W كسمات في R
  • ضمّن المفتاح الأساسي للكيان المالك E كمفتاح أجنبي في R
  • المفتاح الأساسي لـ R هو مزيج من:
    • المفتاح الأساسي للكيان المالك (كمفتاح أجنبي)
    • المفتاح الجزئي (المميز) للكيان الضعيف

Step 2 Example: DEPENDENT

EN: Weak Entity: DEPENDENT (depends on EMPLOYEE)

Result:
  • DEPENDENT (Essn, Dependent_name, Sex, Bdate, Relationship)

Where:
  • Essn = Primary key of EMPLOYEE (as FK) - renamed from Ssn
  • Dependent_name = Partial key of DEPENDENT
  • Primary Key = {Essn, Dependent_name} (combination)
AR: الكيان الضعيف: DEPENDENT (يعتمد على EMPLOYEE)

النتيجة:
  • DEPENDENT (Essn, Dependent_name, Sex, Bdate, Relationship)

حيث:
  • Essn = المفتاح الأساسي لـ EMPLOYEE (كمفتاح أجنبي) - أُعيدت تسميته من Ssn
  • Dependent_name = المفتاح الجزئي لـ DEPENDENT
  • المفتاح الأساسي = {Essn, Dependent_name} (مزيج)

Step 3: Binary 1:1 Relationship Mapping

EN: For each binary 1:1 relationship type:
  1. Identify relations S and T that correspond to the participating entity types
  2. Choose one relation (say S) and include the primary key of T as a foreign key in S
  3. It is better to choose S as the entity type with TOTAL participation (to minimize NULL values)
  4. Include all simple attributes of the 1:1 relationship as attributes of S
AR: لكل نوع علاقة ثنائية 1:1:
  1. حدد العلاقات S و T التي تتوافق مع أنواع الكيانات المشاركة
  2. اختر علاقة واحدة (مثلاً S) وضمّن المفتاح الأساسي لـ T كمفتاح أجنبي في S
  3. يُفضل اختيار S ككيان له مشاركة كلية (TOTAL) (لتقليل قيم NULL)
  4. ضمّن جميع السمات البسيطة لعلاقة 1:1 كسمات في S

Step 3 Example: MANAGES

EN: Binary 1:1 Relationship: MANAGES (between EMPLOYEE and DEPARTMENT)

Result:
  • DEPARTMENT (Dnumber, Dname, Mgr_ssn, Mgr_start_date)

Why DEPARTMENT?
  • DEPARTMENT has total participation (every department has a manager)
  • Mgr_ssn = FK referencing EMPLOYEE(Ssn)
  • Mgr_start_date = Relationship attribute
AR: علاقة ثنائية 1:1: MANAGES (بين EMPLOYEE و DEPARTMENT)

النتيجة:
  • DEPARTMENT (Dnumber, Dname, Mgr_ssn, Mgr_start_date)

لماذا DEPARTMENT؟
  • DEPARTMENT لديه مشاركة كلية (كل قسم له مدير)
  • Mgr_ssn = مفتاح أجنبي يشير إلى EMPLOYEE(Ssn)
  • Mgr_start_date = سمة العلاقة

Step 4: Binary 1:N Relationship Mapping

EN: For each binary 1:N relationship type:
  1. Identify relation S that represents the entity type at the N-side
  2. Include as foreign key in S the primary key of relation T (the 1-side entity)
  3. Include any simple attributes of the 1:N relationship as attributes of S

Key Rule: FK goes to the N-side (many side)!
AR: لكل نوع علاقة ثنائية 1:N:
  1. حدد العلاقة S التي تمثل نوع الكيان على جانب N
  2. ضمّن المفتاح الأساسي للعلاقة T (كيان جانب 1) كمفتاح أجنبي في S
  3. ضمّن أي سمات بسيطة لعلاقة 1:N كسمات في S

القاعدة الأساسية: المفتاح الأجنبي يذهب إلى جانب N (الجانب المتعدد)!

Step 4 Examples

EN: Binary 1:N relationships: WORKS_FOR, CONTROLS, SUPERVISION

Results:
  • WORKS_FOR (N:1): EMPLOYEE (Ssn, ..., Dno)
    → Dno is FK referencing DEPARTMENT(Dnumber)
  • CONTROLS (1:N): PROJECT (Pnumber, ..., Dnum)
    → Dnum is FK referencing DEPARTMENT(Dnumber)
  • SUPERVISION (1:N Recursive): EMPLOYEE (Ssn, ..., Super_ssn)
    → Super_ssn is FK referencing EMPLOYEE(Ssn) - same table!
AR: علاقات ثنائية 1:N: WORKS_FOR، CONTROLS، SUPERVISION

النتائج:
  • WORKS_FOR (N:1): EMPLOYEE (Ssn, ..., Dno)
    → Dno مفتاح أجنبي يشير إلى DEPARTMENT(Dnumber)
  • CONTROLS (1:N): PROJECT (Pnumber, ..., Dnum)
    → Dnum مفتاح أجنبي يشير إلى DEPARTMENT(Dnumber)
  • SUPERVISION (1:N تكرارية): EMPLOYEE (Ssn, ..., Super_ssn)
    → Super_ssn مفتاح أجنبي يشير إلى EMPLOYEE(Ssn) - نفس الجدول!

Step 5: Binary M:N Relationship Mapping

EN: For each binary M:N relationship type:
  1. Create a NEW relation S to represent the relationship
  2. Include as foreign keys the primary keys of BOTH participating entity relations
  3. The combination of these foreign keys forms the PRIMARY KEY of S
  4. Include any simple attributes of the M:N relationship as attributes of S

M:N relationships ALWAYS create a new table!
AR: لكل نوع علاقة ثنائية M:N:
  1. أنشئ علاقة جديدة S لتمثيل العلاقة
  2. ضمّن المفاتيح الأساسية لكلا علاقتي الكيانات المشاركة كمفاتيح أجنبية
  3. مزيج هذه المفاتيح الأجنبية يشكل المفتاح الأساسي لـ S
  4. ضمّن أي سمات بسيطة لعلاقة M:N كسمات في S

علاقات M:N دائماً تُنشئ جدولاً جديداً!

Step 5 Example: WORKS_ON

EN: Binary M:N Relationship: WORKS_ON (EMPLOYEE works on PROJECT)

Result:
  • WORKS_ON (Essn, Pno, Hours)

Where:
  • Essn = FK referencing EMPLOYEE(Ssn)
  • Pno = FK referencing PROJECT(Pnumber)
  • Primary Key = {Essn, Pno} (combination of both FKs)
  • Hours = Relationship attribute
AR: علاقة ثنائية M:N: WORKS_ON (الموظف يعمل على مشروع)

النتيجة:
  • WORKS_ON (Essn, Pno, Hours)

حيث:
  • Essn = مفتاح أجنبي يشير إلى EMPLOYEE(Ssn)
  • Pno = مفتاح أجنبي يشير إلى PROJECT(Pnumber)
  • المفتاح الأساسي = {Essn, Pno} (مزيج المفتاحين الأجنبيين)
  • Hours = سمة العلاقة

Step 6: Multi-valued Attribute Mapping

EN: For each multi-valued attribute A:
  1. Create a NEW relation R
  2. Include an attribute corresponding to A
  3. Include the primary key K of the entity that has A as a foreign key
  4. The primary key of R = {A, K} (combination)
  5. If A is composite, include its simple components
AR: لكل سمة متعددة القيم A:
  1. أنشئ علاقة جديدة R
  2. ضمّن سمة تتوافق مع A
  3. ضمّن المفتاح الأساسي K للكيان الذي يملك A كمفتاح أجنبي
  4. المفتاح الأساسي لـ R = {A, K} (مزيج)
  5. إذا كانت A مركبة، ضمّن مكوناتها البسيطة

Step 6 Example: Locations

EN: Multi-valued Attribute: Locations (of DEPARTMENT)

Result:
  • DEPT_LOCATIONS (Dnumber, Dlocation)

Where:
  • Dnumber = FK referencing DEPARTMENT(Dnumber)
  • Dlocation = The multi-valued attribute value
  • Primary Key = {Dnumber, Dlocation}
AR: السمة متعددة القيم: Locations (للقسم DEPARTMENT)

النتيجة:
  • DEPT_LOCATIONS (Dnumber, Dlocation)

حيث:
  • Dnumber = مفتاح أجنبي يشير إلى DEPARTMENT(Dnumber)
  • Dlocation = قيمة السمة متعددة القيم
  • المفتاح الأساسي = {Dnumber, Dlocation}

Step 7: N-ary Relationship Mapping

EN: For each n-ary relationship type (where n > 2):
  1. Create a NEW relation S to represent the relationship
  2. Include as foreign keys the primary keys of ALL n participating entity relations
  3. The combination of all foreign keys forms the PRIMARY KEY of S
  4. Include any simple attributes of the n-ary relationship

Example: A ternary relationship SUPPLY(Supplier, Part, Project) would create a table with 3 FKs.
AR: لكل نوع علاقة n-ary (حيث n > 2):
  1. أنشئ علاقة جديدة S لتمثيل العلاقة
  2. ضمّن المفاتيح الأساسية لجميع علاقات الكيانات n المشاركة كمفاتيح أجنبية
  3. مزيج جميع المفاتيح الأجنبية يشكل المفتاح الأساسي لـ S
  4. ضمّن أي سمات بسيطة للعلاقة n-ary

مثال: علاقة ثلاثية SUPPLY(Supplier, Part, Project) ستُنشئ جدولاً بـ 3 مفاتيح أجنبية.

Mapping Steps Summary Diagram

ER to Relational Mapping - Decision Flowchart Step 1: Regular Entity → Create table with all simple attributes + PK Step 2: Weak Entity → Table + Owner's PK as FK PK = {Owner PK + Partial Key} Step 3: 1:1 Relationship → Add FK to total participation side Step 4: 1:N Relationship → Add FK to N-side (many side) Step 5: M:N Relationship → NEW TABLE with PK = {FK1 + FK2} Step 6: Multi-valued Attr → NEW TABLE with PK = {Attr + Owner PK} Step 7: N-ary Relationship → NEW TABLE with PK = {all n FKs} Key Rules to Remember: • 1:1 → FK goes to TOTAL participation side • 1:N → FK goes to N-side (many side) • M:N → Always creates NEW table • Multi-valued → Always creates NEW table

Company Database - Final Schema

EMPLOYEE (Ssn, Fname, Minit, Lname, Bdate, Address, Sex, Salary, Dno, Super_ssn) DEPARTMENT (Dnumber, Dname, Mgr_ssn, Mgr_start_date) DEPT_LOCATIONS (Dnumber, Dlocation) PROJECT (Pnumber, Pname, Plocation, Dnum) WORKS_ON (Essn, Pno, Hours) DEPENDENT (Essn, Dependent_name, Sex, Bdate, Relationship) Foreign Key References: • EMPLOYEE.Dno → DEPARTMENT.Dnumber (WORKS_FOR: 1:N) • EMPLOYEE.Super_ssn → EMPLOYEE.Ssn (SUPERVISION: 1:N recursive) • DEPARTMENT.Mgr_ssn → EMPLOYEE.Ssn (MANAGES: 1:1) • PROJECT.Dnum → DEPARTMENT.Dnumber (CONTROLS: 1:N) • WORKS_ON.Essn → EMPLOYEE.Ssn, WORKS_ON.Pno → PROJECT.Pnumber (M:N) • DEPENDENT.Essn → EMPLOYEE.Ssn (Weak entity)

Chapter 5 Quiz

Multiple Choice Questions

1. How many steps are in the ER-to-Relational mapping algorithm?

A) 5
B) 6
C) 7
D) 8
Answer: C - The ER-to-Relational mapping algorithm comprises 7 steps.

2. In Step 1 (Regular Entity mapping), composite attributes are:

A) Included as-is in the table
B) Decomposed into simple component attributes
C) Stored in a separate table
D) Ignored completely
Answer: B - Only simple component attributes of a composite attribute are included, not the composite attribute itself.

3. For a weak entity type, the primary key is:

A) Only the partial key
B) Only the owner's primary key
C) A new surrogate key
D) Combination of owner's PK and partial key
Answer: D - The PK of a weak entity relation is the combination of the owner's primary key (as FK) and the partial key of the weak entity.

4. In a 1:1 relationship, where should the foreign key be placed?

A) In the entity with total participation
B) In both entities
C) In a new separate table
D) In the entity with partial participation
Answer: A - It is better to place the FK in the entity with total participation to minimize NULL values.

5. In a 1:N relationship, the foreign key goes to:

A) The 1-side (one side)
B) The N-side (many side)
C) Both sides
D) A new table
Answer: B - In 1:N relationships, the FK is placed in the relation on the N-side (many side).

6. M:N relationships are mapped by:

A) Adding FK to one side
B) Adding FK to both sides
C) Creating a new relationship table
D) Merging both entities
Answer: C - M:N relationships require creating a new relation (table) with foreign keys from both participating entities forming the primary key.

7. The primary key of an M:N relationship table is:

A) A new auto-generated key
B) One of the foreign keys
C) A relationship attribute
D) Combination of both foreign keys
Answer: D - The PK of an M:N relationship table is the combination of the foreign keys referencing both participating entities.

8. Multi-valued attributes are mapped by:

A) Creating a new table with the attribute and owner's PK
B) Storing as comma-separated values
C) Creating multiple columns
D) Ignoring them
Answer: A - Multi-valued attributes require a new relation with the attribute value and the owner's primary key as foreign key.

9. In SUPERVISION (recursive 1:N), where does the FK go?

A) To a new table
B) Same table as another attribute (Super_ssn)
C) To both supervisor and supervisee
D) Cannot be mapped
Answer: B - In recursive relationships, the FK references the same table. Super_ssn in EMPLOYEE references EMPLOYEE(Ssn).

10. Which mapping step handles relationship attributes?

A) Step 1 only
B) Step 2 only
C) Steps 3, 4, and 5 (relationship mapping steps)
D) Step 7 only
Answer: C - Relationship attributes are included during relationship mapping (Steps 3-5). They go with the FK or into the relationship table.

True/False Questions

1. A 1:1 relationship always requires creating a new table.

A) True
B) False
Answer: False - 1:1 relationships are mapped by adding a FK to one of the existing entity tables, not by creating a new table.

2. M:N relationships always require a new table.

A) True
B) False
Answer: True - M:N relationships cannot be represented with just foreign keys; they always require a new relationship table.

3. In 1:N mapping, the FK goes to the 1-side entity.

A) True
B) False
Answer: False - In 1:N relationships, the FK goes to the N-side (many side), not the 1-side.

4. Multi-valued attributes are stored in the same table as other attributes.

A) True
B) False
Answer: False - Multi-valued attributes require a separate table because the relational model only allows atomic (single) values.

5. The partial key of a weak entity becomes part of the primary key in the mapped relation.

A) True
B) False
Answer: True - The PK of a weak entity's relation is the combination of the owner's PK (as FK) and the partial key (discriminator).

Chapter 7: Functional Dependencies & Normalization

Chapter 7: Functional Dependencies & Normalization

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:

  • Problems in bad database design (redundancy, anomalies)
  • Functional Dependencies (FDs)
  • Armstrong's Inference Rules
  • Normal Forms: 1NF, 2NF, 3NF

الهدف: تصميم مخططات علائقية جيدة من خلال فهم وإزالة المشاكل الناتجة عن التكرار. نستخدم الاعتماديات الوظيفية والأشكال الطبيعية لتوجيه تفكيك الجداول سيئة التصميم.

المواضيع الرئيسية:

  • مشاكل تصميم قواعد البيانات السيء (التكرار، الشذوذات)
  • الاعتماديات الوظيفية (FDs)
  • قواعد أرمسترونج للاستدلال
  • الأشكال الطبيعية: 1NF, 2NF, 3NF

Problems in Bad Database Design

Redundant Data: The same information is stored multiple times, wasting storage and causing inconsistency.

Update Anomalies:

  • Insertion Anomaly: Cannot insert data about one entity without having data about another. Example: Cannot add a new department unless an employee is assigned to it.
  • Deletion Anomaly: Deleting one piece of information unintentionally deletes other valuable data. Example: Deleting the last employee in a department removes all department info.
  • Modification Anomaly: Updating one fact requires updating multiple rows. Example: Changing department location requires updating all employee rows in that department.

NULL Values: Excessive NULL values waste storage and make queries more complex.

البيانات المكررة: نفس المعلومات مخزنة عدة مرات، مما يهدر المساحة ويسبب عدم التناسق.

شذوذات التحديث:

  • شذوذ الإدراج: لا يمكن إدراج بيانات عن كيان دون وجود بيانات عن كيان آخر. مثال: لا يمكن إضافة قسم جديد إلا إذا تم تعيين موظف له.
  • شذوذ الحذف: حذف معلومة واحدة يحذف معلومات قيمة أخرى بدون قصد. مثال: حذف آخر موظف في قسم يزيل جميع معلومات القسم.
  • شذوذ التعديل: تحديث حقيقة واحدة يتطلب تحديث عدة صفوف. مثال: تغيير موقع القسم يتطلب تحديث جميع صفوف الموظفين في ذلك القسم.

قيم NULL: القيم الفارغة الزائدة تهدر المساحة وتجعل الاستعلامات أكثر تعقيداً.

Functional Dependencies (FD)

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 functionally determines Y (or Y is functionally dependent on X)
  • X is called the determinant (left-hand side, LHS)
  • Y is called the dependent (right-hand side, RHS)

التعريف: الاعتماد الوظيفي X → Y يعني أن قيمة مجموعة السمات X تحدد بشكل فريد قيمة مجموعة السمات Y.

التعريف الرسمي: X → Y يتحقق إذا وفقط إذا كلما كان لصفين t1 و t2 نفس القيمة t1[X] = t2[X]، فيجب أن يكون لديهما أيضاً t1[Y] = t2[Y].

الترميز:

  • X → Y: X يحدد وظيفياً Y (أو Y يعتمد وظيفياً على X)
  • X يسمى المحدد (الجانب الأيسر، LHS)
  • Y يسمى التابع (الجانب الأيمن، RHS)

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.

Types of Functional Dependencies

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 vs Partial Dependency

Full Functional Dependency: Y is fully functionally dependent on X if:

  • X → Y, AND
  • Removing any attribute from X breaks the dependency

Partial Functional Dependency: Y is partially dependent on X if:

  • X → Y, AND
  • Some proper subset of X can still determine Y

الاعتماد الوظيفي الكامل: Y يعتمد اعتماداً وظيفياً كاملاً على X إذا:

  • X → Y، و
  • إزالة أي سمة من X تكسر الاعتماد

الاعتماد الوظيفي الجزئي: Y يعتمد جزئياً على X إذا:

  • X → Y، و
  • بعض المجموعة الجزئية الحقيقية من X لا تزال تحدد Y

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)

Transitive Dependency

Definition: Z is transitively dependent on X if:

  • X → Y (X determines Y)
  • Y → Z (Y determines Z)
  • Y ↛ X (Y does not determine X)
  • Therefore: X → Z (through Y)

The dependency goes through an intermediate attribute Y that is not a superkey.

التعريف: Z يعتمد اعتماداً متعدياً على X إذا:

  • X → Y (X يحدد Y)
  • Y → Z (Y يحدد Z)
  • Y ↛ X (Y لا يحدد X)
  • بالتالي: X → Z (من خلال Y)

الاعتماد يمر عبر سمة وسيطة 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 Inference Rules (Axioms)

Armstrong's rules are sound (only derive correct FDs) and complete (can derive all FDs that hold).

Primary Rules (RAT):

  • IR1 - Reflexive Rule: If Y ⊆ X, then X → Y
  • IR2 - Augmentation Rule: If X → Y, then XZ → YZ (for any Z)
  • IR3 - Transitive Rule: If X → Y and Y → Z, then X → Z

قواعد أرمسترونج سليمة (تشتق فقط الاعتماديات الصحيحة) وكاملة (يمكنها اشتقاق جميع الاعتماديات التي تتحقق).

القواعد الأساسية (RAT):

  • IR1 - قاعدة الانعكاس: إذا كان Y ⊆ X، فإن X → Y
  • IR2 - قاعدة التوسيع: إذا كان X → Y، فإن XZ → YZ (لأي Z)
  • IR3 - قاعدة التعدي: إذا كان X → Y و Y → Z، فإن X → Z

Derived Inference Rules

Secondary Rules (derived from RAT):

  • IR4 - Decomposition (Splitting): If X → YZ, then X → Y and X → Z
  • IR5 - Union (Combining): If X → Y and X → Z, then X → YZ
  • IR6 - Pseudo-transitive: If X → Y and WY → Z, then WX → Z

القواعد الثانوية (مشتقة من RAT):

  • IR4 - التفكيك: إذا كان X → YZ، فإن X → Y و X → Z
  • IR5 - الاتحاد: إذا كان X → Y و X → Z، فإن X → YZ
  • IR6 - شبه التعدي: إذا كان X → Y و WY → Z، فإن WX → Z

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}

First Normal Form (1NF)

Definition: A relation is in 1NF if and only if:

  • All attributes contain only atomic (indivisible) values
  • No repeating groups or arrays
  • Each row is unique (has a primary key)

Violations:

  • Multi-valued attributes (e.g., multiple phone numbers in one cell)
  • Composite attributes stored as single values
  • Repeating groups (e.g., Item1, Item2, Item3 columns)

التعريف: العلاقة تكون في الشكل الطبيعي الأول إذا وفقط إذا:

  • جميع السمات تحتوي فقط على قيم ذرية (غير قابلة للتجزئة)
  • لا توجد مجموعات متكررة أو مصفوفات
  • كل صف فريد (له مفتاح أساسي)

الانتهاكات:

  • سمات متعددة القيم (مثل: أرقام هواتف متعددة في خلية واحدة)
  • سمات مركبة مخزنة كقيم مفردة
  • مجموعات متكررة (مثل: أعمدة Item1, Item2, Item3)

Not in 1NF: STUDENT(ID, Name, Phones: "555-1234, 555-5678")

In 1NF: Create separate STUDENT_PHONE(Student_ID, Phone) table

Second Normal Form (2NF)

Definition: A relation is in 2NF if and only if:

  • It is in 1NF, AND
  • No partial dependencies: Every non-prime attribute is fully functionally dependent on the entire primary key

Key Terms:

  • Prime attribute: An attribute that is part of any candidate key
  • Non-prime attribute: An attribute that is not part of any candidate key
  • Partial dependency: A non-prime attribute depends on only part of the primary key

Note: 2NF issues only occur when the primary key is composite (multiple attributes).

التعريف: العلاقة تكون في الشكل الطبيعي الثاني إذا وفقط إذا:

  • تكون في 1NF، و
  • لا توجد اعتماديات جزئية: كل سمة غير أساسية تعتمد اعتماداً وظيفياً كاملاً على المفتاح الأساسي بأكمله

مصطلحات مهمة:

  • السمة الأساسية: سمة هي جزء من أي مفتاح مرشح
  • السمة غير الأساسية: سمة ليست جزءاً من أي مفتاح مرشح
  • الاعتماد الجزئي: سمة غير أساسية تعتمد على جزء فقط من المفتاح الأساسي

ملاحظة: مشاكل 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)

Third Normal Form (3NF)

Definition: A relation is in 3NF if and only if for every non-trivial FD X → Y:

  • X is a superkey, OR
  • Y is a prime attribute (part of some candidate key)

Alternative Definition: A relation is in 3NF if:

  • It is in 2NF, AND
  • No transitive dependencies: No non-prime attribute depends on another non-prime attribute

التعريف: العلاقة تكون في الشكل الطبيعي الثالث إذا وفقط إذا لكل اعتماد غير بديهي X → Y:

  • X مفتاح فائق، أو
  • Y سمة أساسية (جزء من مفتاح مرشح ما)

تعريف بديل: العلاقة تكون في 3NF إذا:

  • تكون في 2NF، و
  • لا توجد اعتماديات متعدية: لا سمة غير أساسية تعتمد على سمة غير أساسية أخرى

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)

Summary: Normal Forms Comparison

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 أساسية) الاعتماديات المتعدية

Normalization Process

Normalization Hierarchy 1NF: Atomic Values Only 2NF: No Partial Dependencies 3NF No Transitive Dependencies (Eliminates transitive dependencies on non-prime attributes) More Normalized →

Functional Dependency Diagram

FD Types in EMP_PROJ(Ssn, Pnumber, Ename, Pname, Hours) Ssn Pnumber Ename Pname Hours Legend Primary Key Attributes Non-Key Attributes Partial Dependency (violates 2NF) Full Dependency (OK) Problem: • Ssn → Ename (partial: only part of PK) • Pnumber → Pname (partial: only part of PK) Solution: Decompose into 3 tables EMPLOYEE, PROJECT, WORKS_ON

Transitive Dependency Diagram

Transitive Dependency: Ssn → Dnumber → Dname Ssn (PK) Dnumber Dname Transitive: Ssn → Dname Direct FD Violates 3NF (Dnumber not superkey) Solution: Decompose EMPLOYEE(Ssn, Ename, Dnumber) DEPARTMENT(Dnumber, Dname, Dmgr_ssn)

Armstrong's Inference Rules

Armstrong's Axioms & Derived Rules Primary Rules (RAT) IR1 Reflexive: If Y ⊆ X, then X → Y IR2 Augmentation: If X → Y, then XZ → YZ IR3 Transitive: If X → Y, Y → Z, then X → Z Derived Rules IR4 Decomposition: If X → YZ, then X → Y IR5 Union: If X → Y, X → Z, then X → YZ IR6 Pseudo-trans: If X → Y, WY → Z, then WX → Z derive

Chapter 7 Questions

Multiple Choice Questions

1. Which anomaly occurs when we cannot add a new department unless we assign an employee to it?

A) Insertion Anomaly
B) Deletion Anomaly
C) Modification Anomaly
D) Update Anomaly
Answer: A - Insertion anomaly occurs when we cannot insert data about one entity without having data about another related entity.

2. In the functional dependency X → Y, what is X called?

A) Dependent
B) Determinant
C) Superkey
D) Prime attribute
Answer: B - In X → Y, X is called the determinant (left-hand side) and Y is the dependent (right-hand side).

3. Which Armstrong rule states: "If X → Y and Y → Z, then X → Z"?

A) Reflexive Rule
B) Augmentation Rule
C) Transitive Rule
D) Union Rule
Answer: C - The Transitive Rule (IR3) states that if X → Y and Y → Z, then X → Z.

4. A relation is in 1NF if:

A) All attributes contain atomic values only
B) There are no partial dependencies
C) There are no transitive dependencies
D) Every determinant is a superkey
Answer: A - 1NF requires that all attributes contain only atomic (indivisible) values, with no repeating groups.

5. 2NF specifically eliminates:

A) Transitive dependencies
B) Partial dependencies
C) Multi-valued attributes
D) All functional dependencies
Answer: B - 2NF eliminates partial dependencies where a non-prime attribute depends on only part of the primary key.

6. A partial dependency can only occur when:

A) The table has no primary key
B) The primary key is a single attribute
C) The primary key is composite
D) The table has foreign keys
Answer: C - Partial dependencies can only occur when the primary key consists of multiple attributes (composite key).

7. In 3NF, for every non-trivial FD X → Y:

A) X must be a superkey only
B) Y must be a superkey
C) X must be a prime attribute
D) X is a superkey OR Y is a prime attribute
Answer: D - 3NF requires that either X is a superkey or Y is a prime attribute (part of some candidate key).

8. If Ssn → Dnumber and Dnumber → Dname, this creates a:

A) Partial dependency
B) Transitive dependency
C) Full dependency
D) Trivial dependency
Answer: B - This is a transitive dependency: Dname is transitively dependent on Ssn through Dnumber.

True/False Questions

1. Armstrong's inference rules are both sound and complete.

A) True
B) False
Answer: True - Armstrong's rules are sound (only derive correct FDs) and complete (can derive all FDs that hold).

2. A relation with a single-attribute primary key can violate 2NF.

A) True
B) False
Answer: False - 2NF violations (partial dependencies) can only occur with composite primary keys. A single-attribute PK cannot have partial dependencies.

4. The Union rule (IR5) is one of the primary Armstrong axioms.

A) True
B) False
Answer: False - The Union rule is a derived rule, not a primary axiom. The primary axioms are Reflexive, Augmentation, and Transitive (RAT).

Chapter 6: Creating and Managing Tables

Introduction to SQL

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:

  • DDL (Data Definition Language): CREATE, ALTER, DROP - for defining database structure
  • DML (Data Manipulation Language): INSERT, UPDATE, DELETE - for manipulating data

SQL (لغة الاستعلام الهيكلية) هي اللغة القياسية للتفاعل مع قواعد البيانات العلائقية. تُستخدم لإنشاء وتعديل والاستعلام عن البيانات المخزنة في نظام إدارة قواعد البيانات العلائقية.

يمكن تقسيم عبارات SQL إلى:

  • DDL (لغة تعريف البيانات): CREATE، ALTER، DROP - لتعريف هيكل قاعدة البيانات
  • DML (لغة معالجة البيانات): INSERT، UPDATE، DELETE - لمعالجة البيانات

SQL Capabilities

SQL can be used to perform the following operations:

  • Create, modify, and drop database objects (tables, views, indexes)
  • Insert, update, and delete rows in tables
  • Query data from single or multiple tables
  • Control access to the database and its objects

يمكن استخدام SQL لتنفيذ العمليات التالية:

  • إنشاء وتعديل وحذف كائنات قاعدة البيانات (الجداول، العروض، الفهارس)
  • إدراج وتحديث وحذف الصفوف في الجداول
  • الاستعلام عن البيانات من جدول واحد أو عدة جداول
  • التحكم في الوصول إلى قاعدة البيانات وكائناتها

CREATE TABLE Statement

The CREATE TABLE statement is used to create a new table in the database.

Syntax:

CREATE TABLE table_name (
  column1 datatype [constraint],
  column2 datatype [constraint],
  ...
);

تُستخدم عبارة CREATE TABLE لإنشاء جدول جديد في قاعدة البيانات.

الصيغة:

CREATE TABLE table_name (
  column1 datatype [constraint],
  column2 datatype [constraint],
  ...
);
CREATE TABLE employee (
    emp_id      NUMBER(4),
    emp_name    VARCHAR2(30),
    hire_date   DATE,
    salary      NUMBER(8,2)
);

Common Data Types

Data TypeDescription
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
DATEDate and time values
نوع البياناتالوصف
VARCHAR2(n)بيانات نصية متغيرة الطول (حتى n حرف)
CHAR(n)بيانات نصية ثابتة الطول (بالضبط n حرف)
NUMBER(p,s)بيانات رقمية بدقة p ومقياس s
DATEقيم التاريخ والوقت

PRIMARY KEY Constraint

The PRIMARY KEY constraint uniquely identifies each row in a table. It must contain unique values and cannot contain NULL values.

Column-level syntax:

column_name datatype PRIMARY KEY

Table-level syntax:

PRIMARY KEY (column_name)

قيد PRIMARY KEY يُحدد بشكل فريد كل صف في الجدول. يجب أن يحتوي على قيم فريدة ولا يمكن أن يحتوي على قيم NULL.

صيغة مستوى العمود:

column_name datatype PRIMARY KEY

صيغة مستوى الجدول:

PRIMARY KEY (column_name)
-- 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)
);

FOREIGN KEY Constraint

The FOREIGN KEY constraint establishes a relationship between two tables. It references the PRIMARY KEY of another table.

Syntax:

FOREIGN KEY (column_name) REFERENCES parent_table(column_name)

A FOREIGN KEY constraint enforces referential integrity: you cannot insert a value that does not exist in the parent table.

قيد FOREIGN KEY يُنشئ علاقة بين جدولين. يُشير إلى المفتاح الرئيسي لجدول آخر.

الصيغة:

FOREIGN KEY (column_name) REFERENCES parent_table(column_name)

قيد 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)
);

INSERT Statement

The INSERT statement adds new rows to a table.

Syntax:

INSERT INTO table_name (column1, column2, ...)
VALUES (value1, value2, ...);

Rules:

  • Character and date values must be enclosed in single quotes
  • Column list is optional if values are provided for all columns in order

عبارة INSERT تُضيف صفوفاً جديدة إلى الجدول.

الصيغة:

INSERT INTO table_name (column1, column2, ...)
VALUES (value1, value2, ...);

القواعد:

  • يجب وضع قيم النصوص والتواريخ بين علامات اقتباس مفردة
  • قائمة الأعمدة اختيارية إذا تم توفير قيم لجميع الأعمدة بالترتيب
INSERT INTO department (dept_id, dept_name)
VALUES (10, 'Accounting');

INSERT INTO employee
VALUES (101, 'John Smith', '15-JAN-2020', 5000);

Inserting NULL Values

There are two methods to insert NULL values:

  • Implicit method: Omit the column from the column list
  • Explicit method: Specify NULL in the VALUES list

هناك طريقتان لإدراج قيم NULL:

  • الطريقة الضمنية: حذف العمود من قائمة الأعمدة
  • الطريقة الصريحة: تحديد 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);

UPDATE Statement

The UPDATE statement modifies existing rows in a table.

Syntax:

UPDATE table_name
SET column1 = value1, column2 = value2, ...
WHERE condition;

Important: Without a WHERE clause, ALL rows in the table will be updated!

عبارة UPDATE تُعدّل الصفوف الموجودة في الجدول.

الصيغة:

UPDATE table_name
SET column1 = value1, column2 = value2, ...
WHERE condition;

مهم: بدون جملة 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;

DELETE Statement

The DELETE statement removes rows from a table.

Syntax:

DELETE FROM table_name
WHERE condition;

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 تُزيل الصفوف من الجدول.

الصيغة:

DELETE FROM table_name
WHERE condition;

مهم: بدون جملة 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;

Integrity Constraint Errors

When performing DML operations, you may encounter integrity constraint violations:

  • INSERT: Cannot insert a FOREIGN KEY value that doesn't exist in the parent table
  • UPDATE: Cannot update a PRIMARY KEY value if it's referenced by a FOREIGN KEY
  • DELETE: Cannot delete a row if its PRIMARY KEY is referenced by a FOREIGN KEY in a child table

عند تنفيذ عمليات DML، قد تواجه انتهاكات قيود التكامل:

  • INSERT: لا يمكن إدراج قيمة FOREIGN KEY غير موجودة في الجدول الأصلي
  • UPDATE: لا يمكن تحديث قيمة PRIMARY KEY إذا كانت مُشار إليها بواسطة FOREIGN KEY
  • DELETE: لا يمكن حذف صف إذا كان PRIMARY KEY الخاص به مُشاراً إليه بواسطة FOREIGN KEY في جدول فرعي

SQL Statement Categories

SQL Statement Categories SQL DDL (Data Definition) DML (Data Manipulation) CREATE ALTER DROP INSERT UPDATE DELETE Defines database structure (tables, constraints) Manipulates data in tables (insert, modify, delete)

Multiple Choice Questions

1. Which SQL statement is used to add new rows to a table?

A) UPDATE
B) INSERT
C) CREATE
D) DELETE
Answer: B) INSERT - The INSERT statement is used to add new rows to a table. UPDATE modifies existing rows, CREATE defines new tables, and DELETE removes rows.

2. What happens when you omit the WHERE clause in a DELETE statement?

A) An error occurs
B) No rows are deleted
C) All rows are deleted
D) Only the first row is deleted
Answer: C) All rows are deleted - Without a WHERE clause, DELETE removes all rows from the table. This is why the WHERE clause is critical for targeted deletions.

3. Which data type is used to store variable-length character strings?

A) CHAR
B) VARCHAR2
C) NUMBER
D) DATE
Answer: B) VARCHAR2 - VARCHAR2 stores variable-length character strings, using only the space needed. CHAR is fixed-length and pads with spaces.

4. What constraint ensures that a column cannot contain NULL values?

A) NOT NULL
B) UNIQUE
C) PRIMARY KEY
D) CHECK
Answer: A) NOT NULL - The NOT NULL constraint ensures that a column must always contain a value and cannot be left empty.

5. Which SQL statement is used to modify existing data in a table?

A) INSERT
B) CREATE
C) UPDATE
D) ALTER
Answer: C) UPDATE - The UPDATE statement modifies existing rows in a table. INSERT adds new rows, CREATE defines tables, and ALTER modifies table structure.

6. What type of integrity constraint does FOREIGN KEY enforce?

A) Entity Integrity
B) Referential Integrity
C) Domain Integrity
D) User-Defined Integrity
Answer: B) Referential Integrity - FOREIGN KEY ensures referential integrity by requiring values to match a primary key in the referenced table.

True or False Questions

1. VARCHAR2 and CHAR store the same type of data but differ in storage behavior.

A) True
B) False
Answer: True - Both store character data, but VARCHAR2 is variable-length (stores only actual characters), while CHAR is fixed-length (pads with spaces to fill the specified length).

2. You can delete a row from a parent table if it is referenced by a foreign key in a child table.

A) True
B) False
Answer: False - You cannot delete a row if its primary key is referenced by a foreign key in another table. This would violate referential integrity.

3. PRIMARY KEY automatically enforces both NOT NULL and UNIQUE constraints.

A) True
B) False
Answer: True - A PRIMARY KEY constraint automatically ensures that the column(s) cannot contain NULL values and must be unique across all rows.

4. INSERT INTO can add multiple rows in a single statement using a subquery.

A) True
B) False
Answer: True - You can insert multiple rows at once by using INSERT INTO table_name SELECT ... to copy data from another table or query result.

5. UPDATE statement without WHERE clause will modify only the first row.

A) True
B) False
Answer: False - UPDATE without WHERE clause will modify ALL rows in the table, not just the first row. Always use WHERE to target specific rows.

Chapter 8: Functional Dependencies & Normalization

Problems in Bad Database Design

Poor database design leads to several problems:

  • Redundant data: More space required, slower system, complexity in updates
  • Update anomalies: Insertion, Deletion, and Update anomalies
  • NULL values: Attributes with many NULL values cause ambiguity

Ambiguous meaning of NULL:

  • Existed but unknown at present (e.g., Address)
  • Not applicable (e.g., student average before grades)
  • Applicable but not assigned yet (e.g., student mark)

التصميم السيء لقاعدة البيانات يؤدي إلى عدة مشاكل:

  • البيانات المكررة: مساحة أكبر، نظام أبطأ، تعقيد في التحديثات
  • شذوذ التحديث: شذوذ الإدراج والحذف والتحديث
  • قيم NULL: السمات ذات قيم NULL الكثيرة تسبب الغموض

المعنى الغامض لـ NULL:

  • موجود لكن غير معروف حالياً (مثل: العنوان)
  • غير قابل للتطبيق (مثل: معدل الطالب)
  • قابل للتطبيق لكن لم يُعيَّن بعد (مثل: درجة الطالب)

Update Anomalies

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#StudentNameCourse#CourseName
100AliCS100C++
100AliCS101Java
200AhmadCS200OS

Note: "Ali" is repeated - this causes update anomalies!

Functional Dependency Definition

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 is a property of the meaning or semantics of the attributes
  • The FD specifies a restriction on the possible tuples that can form a relation instance r of R

الاعتماد الوظيفي (FD) هو قيد بين مجموعتين من السمات في مخطط العلاقة.

إذا كان X و Y مجموعتين من السمات في نفس مخطط العلاقة R، فإن X → Y تعني أن X يحدد وظيفياً Y.

  • FD هو خاصية للمعنى أو دلالات السمات
  • يحدد FD قيداً على الصفوف الممكنة التي يمكن أن تشكل مثيل علاقة r من R

FD Constraint - Formally

The FD constraint is that for any two tuples t1 and t2 in the relation instance r(R) that have:

t1[X] = t2[X] ⟹ t1[Y] = t2[Y]

This means:

  • The values of the Y component of a tuple depend on, or are determined by, the X component
  • The values of X uniquely (or functionally) determine the values of Y
  • If X → Y holds, then Y is functionally dependent on X

X is termed the left-hand-side (LHS) or determinant

Y is termed the right-hand-side (RHS)

قيد FD هو أنه لأي صفين t1 و t2 في مثيل العلاقة r(R) اللذان لديهما:

t1[X] = t2[X] ⟹ t1[Y] = t2[Y]

هذا يعني:

  • قيم مكون Y من الصف تعتمد على مكون X
  • قيم X تحدد بشكل فريد (أو وظيفي) قيم Y
  • إذا تحقق X → Y، فإن Y يعتمد وظيفياً على X

X يسمى الجانب الأيسر (LHS) أو المحدد

Y يسمى الجانب الأيمن (RHS)

Example: A relation DEPARTMENT (DNO, DNAME, DLOC) can have:

FD1: DNO → DLOC
FD2: DNO → DNAME

Armstrong's Inference Rules (IR1-IR3)

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 منطقي ممكن).

IR1: Reflexive Rule

If Y ⊆ X then X → Y

A set of attributes always determines itself or any of its subsets.

If Y ⊆ X then X → Y

مجموعة السمات تحدد دائماً نفسها أو أي من مجموعاتها الفرعية.

If ESSN ⊆ {ESSN, Dependent_Name} then

{ESSN, Dependent_Name} → ESSN holds

IR2: Augmentation Rule

If X → Y then XZ → YZ

Adding the same set of attributes to both the LHS & RHS of a FD results in another valid FD.

If X → Y then XZ → YZ

إضافة نفس مجموعة السمات إلى كل من LHS و RHS لـ FD ينتج FD صالح آخر.

If SSN → Ename then

{SSN, Address} → {Ename, Address}

IR3: Transitive Rule

If X → Y and Y → Z then X → Z

FDs are transitive.

If X → Y and Y → Z then X → Z

الاعتمادات الوظيفية متعدية.

If SSN → Dno and Dno → Dlocation then

SSN → Dlocation

Additional Inference Rules (IR4-IR6)

IR4: Decomposition Rule

If X → YZ then X → Y and X → Z

We can remove attributes from the RHS and decompose the FD.

IR5: Additive (Union) Rule

If X → Y and X → Z then X → YZ

We can union attributes from the RHS and combine FDs into a single FD (reverse of IR4).

IR6: Pseudo Transitive Rule

If X → Y and WY → Z then WX → Z

A variant of IR3.

IR4: قاعدة التحليل

If X → YZ then X → Y and X → Z

يمكننا إزالة السمات من RHS وتحليل FD.

IR5: قاعدة الاتحاد

If X → Y and X → Z then X → YZ

يمكننا دمج السمات من RHS وتجميع FDs في FD واحد (عكس IR4).

IR6: قاعدة التعدي الزائف

If X → Y and WY → Z then WX → Z

متغير من IR3.

Closure of a Set of FDs (F+)

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+:

  1. Let F+ = F
  2. Apply the inference rules repeatedly until no more changes occur in F+

التعريف: بمعرفة مجموعة F من الاعتمادات الوظيفية على R، فإن إغلاق F المرمز له بـ F+ هو مجموعة جميع الاعتمادات الوظيفية المستنتجة من F عبر قواعد الاستدلال.

لحساب F+:

  1. لتكن F+ = F
  2. طبق قواعد الاستدلال بشكل متكرر حتى لا تحدث تغييرات في 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)

Closure of Attribute Set (α+)

Given a relation schema R and a set of FDs that hold on R. Let α be a set of attributes in R. Then:

α+ = α plus all attributes that can be implied directly or indirectly from α

Use: If α+ contains all attributes of R, then α is a candidate key.

بمعرفة مخطط علاقة R ومجموعة من FDs تنطبق على R. لتكن α مجموعة سمات في R. إذن:

α+ = α plus all attributes implied from α

الاستخدام: إذا احتوى α+ على جميع سمات 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!

What is Normalization?

Normalization is a method for organizing data elements in a database into tables to minimize duplication.

Why Normalization?

  • Reduce redundant data
  • Remove inconsistent data
  • Reduce anomalies
  • Increase data integrity
  • Simplify data maintenance
  • Take less disk space

Goal: In each table, all non-key attributes should be dependent on the primary key.

التطبيع هو طريقة لتنظيم عناصر البيانات في قاعدة بيانات إلى جداول لتقليل التكرار.

لماذا التطبيع؟

  • تقليل البيانات المكررة
  • إزالة البيانات غير المتسقة
  • تقليل الشذوذ
  • زيادة سلامة البيانات
  • تبسيط صيانة البيانات
  • استهلاك مساحة أقل

الهدف: في كل جدول، يجب أن تعتمد جميع السمات غير المفتاحية على المفتاح الرئيسي.

First Normal Form (1NF)

A relation schema is in 1NF if:

  • Domains of attributes include only atomic (simple, indivisible) values
  • The value of an attribute is a single value from the domain of that attribute

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!

Second Normal Form (2NF)

A relation schema is in 2NF if for all FDs (α→β), one of the following is satisfied:

  1. β ⊆ α (trivial FD), or
  2. α is a super key, or
  3. Each attribute in β is prime, or
  4. α is not a proper subset of a key (no partial dependency)

Key point: Remove partial dependencies to achieve 2NF.

مخطط العلاقة يكون في 2NF إذا كان لجميع FDs (α→β)، أحد الشروط التالية متحقق:

  1. β ⊆ α (FD تافه)، أو
  2. α مفتاح فائق، أو
  3. كل سمة في β أولية، أو
  4. α ليست مجموعة جزئية حقيقية من مفتاح (لا اعتماد جزئي)

النقطة الرئيسية: أزل الاعتمادات الجزئية لتحقيق 2NF.

Third Normal Form (3NF)

A relation schema is in 3NF if for all FDs (α→β), one of the following is satisfied:

  1. β ⊆ α (trivial FD), or
  2. α is a super key, or
  3. Each attribute in β is prime (part of some candidate key)

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 (α→β)، أحد الشروط التالية متحقق:

  1. β ⊆ α (FD تافه)، أو
  2. α مفتاح فائق، أو
  3. كل سمة في β أولية (جزء من مفتاح مرشح)

سمة أولية: سمة هي عضو في أي مفتاح مرشح.

سمة غير أولية: سمة ليست عضو في أي مفتاح مرشح.

النقطة الرئيسية: أزل الاعتمادات المتعدية لتحقيق 3NF.

Boyce-Codd Normal Form (BCNF)

A relation schema is in BCNF if for all FDs (α→β), one of the following is satisfied:

  1. β ⊆ α (trivial FD), or
  2. α is a super key

BCNF is stricter than 3NF - it removes the third condition about prime attributes.

Decomposition for BCNF:

  • R1 = α ∪ β (contains the violating FD)
  • R2 = R - β (remaining attributes)

مخطط العلاقة يكون في BCNF إذا كان لجميع FDs (α→β)، أحد الشروط التالية متحقق:

  1. β ⊆ α (FD تافه)، أو
  2. α مفتاح فائق

BCNF أكثر صرامة من 3NF - تزيل الشرط الثالث حول السمات الأولية.

التحليل لـ BCNF:

  • R1 = α ∪ β (تحتوي على FD المنتهك)
  • R2 = R - β (السمات المتبقية)

Normalization Steps Summary

  1. 1NF: Remove repeating groups and multivalued attributes
  2. 2NF: Remove partial dependencies
  3. 3NF: Remove transitive dependencies
  4. BCNF: Every determinant must be a super key

When a relation satisfies 3NF:

  • Partial dependencies are removed
  • Transitive dependencies are removed
  • All attributes are dependent on the primary key
  • Tables are small and well-formed
  1. 1NF: أزل المجموعات المتكررة والسمات متعددة القيم
  2. 2NF: أزل الاعتمادات الجزئية
  3. 3NF: أزل الاعتمادات المتعدية
  4. BCNF: كل محدد يجب أن يكون مفتاح فائق

عندما تحقق العلاقة 3NF:

  • الاعتمادات الجزئية مُزالة
  • الاعتمادات المتعدية مُزالة
  • جميع السمات تعتمد على المفتاح الرئيسي
  • الجداول صغيرة ومُشكَّلة بشكل جيد

Normal Forms Hierarchy

Normal Forms Hierarchy BCNF (Strongest) 3NF 2NF 1NF Atomic values only No repeating groups Each level removes more anomalies Increasing Strength

Types of Dependencies

Types of Dependencies Full Dependency AB → C C depends on the ENTIRE key (AB) ✓ Good for 2NF Partial Dependency A → C (where AB is key) C depends on PART of the key only ✗ Violates 2NF Transitive Dependency A → B → C C depends on A through B (non-key) ✗ Violates 3NF Example: R(A, B, C, D, E, F) A B C D E F Primary Key Full Partial Transitive

Armstrong's Inference Rules

Inference Rules Summary Armstrong's Rules (Primary) IR1: Reflexive If Y ⊆ X then X → Y "Self-determination" IR2: Augmentation If X → Y then XZ → YZ "Add same to both sides" IR3: Transitive If X → Y, Y → Z then X → Z "Chain rule" Sound & Complete Derived Rules IR4: Decomposition If X → YZ then X → Y, X → Z "Split RHS" IR5: Union If X → Y, X → Z then X → YZ "Combine RHS" IR6: Pseudo-transitive If X → Y, WY → Z then WX → Z "Extended chain" Derived from IR1-IR3

Multiple Choice Questions

1. Which of the following is NOT a problem caused by bad database design?

A) Redundant data
B) Update anomalies
C) Insertion anomalies
D) Faster query performance
Answer: D) Faster query performance - Bad database design causes redundancy and anomalies, not faster performance. In fact, redundant data can slow down the system.

2. What does X → Y mean in functional dependency notation?

A) Y determines X
B) X functionally determines Y
C) X and Y are equal
D) X is a subset of Y
Answer: B) X functionally determines Y - The arrow notation X → Y means that X determines Y, i.e., for any two tuples with the same X value, they must have the same Y value.

3. Which inference rules are known as Armstrong's Inference Rules?

A) Reflexive, Augmentation, Transitive
B) Decomposition, Union, Pseudo-transitive
C) IR4, IR5, IR6
D) All six rules
Answer: A) Reflexive, Augmentation, Transitive - Armstrong's Inference Rules are IR1 (Reflexive), IR2 (Augmentation), and IR3 (Transitive). These three rules are sound and complete.

4. A relation is in 1NF if:

A) It has no transitive dependencies
B) It has no partial dependencies
C) All attributes have atomic values only
D) Every determinant is a super key
Answer: C) All attributes have atomic values only - 1NF requires that all attribute values be atomic (indivisible) with no composite or multivalued attributes.

5. Which normal form removes partial dependencies?

A) 1NF
B) 2NF
C) 3NF
D) BCNF
Answer: B) 2NF - Second Normal Form (2NF) is achieved by removing partial dependencies, where non-key attributes depend on only part of the primary key.

6. Which normal form removes transitive dependencies?

A) 1NF
B) 2NF
C) 3NF
D) BCNF
Answer: C) 3NF - Third Normal Form (3NF) is achieved by removing transitive dependencies, where non-key attributes depend on other non-key attributes.

7. In BCNF, for every FD X → Y:

A) X must be a super key (or Y ⊆ X)
B) Y must be a super key
C) Both X and Y must be keys
D) X can be any attribute
Answer: A) X must be a super key (or Y ⊆ X) - BCNF requires that for every non-trivial FD, the determinant (X) must be a super key.

8. If A+ contains all attributes of relation R, then A is:

A) A foreign key
B) A candidate key
C) A non-prime attribute
D) A dependent attribute
Answer: B) A candidate key - If the closure of A (A+) contains all attributes of R, it means A can determine every attribute in the relation, making A a candidate key (or super key).

True or False Questions

1. Armstrong's inference rules (IR1-IR3) are sound and complete.

A) True
B) False
Answer: True - Armstrong's rules are sound (anything implied is logically correct) and complete (can derive any possible valid FD from a given set of FDs).

2. BCNF is weaker than 3NF.

A) True
B) False
Answer: False - BCNF is stronger than 3NF. BCNF has stricter requirements - it removes the condition that allows prime attributes in the RHS of FDs where the LHS is not a super key.

3. A relation in 1NF can have multivalued attributes.

A) True
B) False
Answer: False - 1NF requires all attributes to have atomic (single, indivisible) values. Multivalued attributes violate 1NF.

4. The Union rule (IR5) states: If X → Y and X → Z then X → YZ.

A) True
B) False
Answer: True - The Union rule (IR5) allows us to combine two FDs with the same left-hand side into a single FD.

5. It is always possible to decompose a relation into BCNF without losing any functional dependencies.

A) True
B) False
Answer: False - Sometimes decomposing to BCNF may result in losing some functional dependencies. In such cases, we may choose to stay at 3NF to preserve dependency preservation.