ScholarQuill logoScholarQuillUniversity Notes
  • Notes
  • Past Papers
  • Blogs
  • Todo
Login
ScholarQuill logoScholarQuillUniversity Notes
Login
NotesPast PapersBlogsTodo
More
SubjectsDiscussionCGPA CalculatorGPA CalculatorStudent PortalCourse Outline
About
About usPrivacy PolicyReportContact
Notes
Past Papers
Blogs
Todo
Analytics
    Current Subject
    🧩
    Database Systems
    CSI-308
    Progress0 / 22 topics
    Topics
    1. Basic Database Concepts2. Entity Relationship Modelling3. Relational Data Model and Algebra4. Structured Query Language (SQL)5. RDBMS6. Database Design7. Functional Dependencies8. Normal Forms9. Transaction Processing10. Optimization Concepts11. Concurrency Control12. Recovery Techniques13. Database Security and Authorization14. Small Group Project Implementing a Database15. Physical Database Design16. Storage and File Structure17. Indexed Files18. B-Trees19. Files with Dense Index20. Files with Variable Length Records21. Database Efficiency22. Database Tuning
    CSI-308›Relational Data Model and Algebra
    Database SystemsTopic 3 of 22

    Relational Data Model and Algebra

    11 minread
    1,814words
    Intermediatelevel

    Relational Data Model and Algebra

    The relational data model is the foundation of modern databases and is used to represent and manage data in a structured way using tables (relations). Relational algebra is a mathematical framework that provides a set of operations for querying and manipulating relational databases. These operations are fundamental to query languages like SQL and are used to process data stored in relational tables.

    Here’s a detailed breakdown of the relational data model and relational algebra:


    1. Relational Data Model

    The relational data model represents data as a collection of tables (also called relations), where each table consists of rows and columns. The key components of the relational model are:

    a. Relation (Table):

    • A relation is a table with rows and columns, where each column represents an attribute and each row represents a tuple (or record).
    • A relation is defined by a name, a set of attributes, and a set of tuples (the data values for each attribute).
    • Example: A Student relation might have columns StudentID, Name, Age, Major, and each row represents a student.

    b. Tuple:

    • A tuple is a single row in a table. It contains values corresponding to the attributes of the relation.
    • Example: In a Student table, a tuple might be (1, 'John Doe', 20, 'Computer Science').

    c. Attribute:

    • An attribute is a column in a relation that represents a property or characteristic of the entities represented by the relation.
    • Example: In a Student table, StudentID, Name, Age, and Major are attributes.

    d. Domain:

    • The domain of an attribute is the set of allowable values for that attribute. For example, the Age attribute might have a domain of integer values from 18 to 100.

    e. Primary Key:

    • A primary key is a unique identifier for each tuple in a relation. It is a set of one or more attributes that uniquely identify a row.
    • Example: In the Student table, StudentID could be the primary key.

    f. Foreign Key:

    • A foreign key is an attribute in one relation that points to the primary key of another relation. It creates a relationship between two tables.
    • Example: In an Enrollment table, StudentID could be a foreign key that refers to the Student relation.

    2. Relational Algebra

    Relational algebra is a procedural query language that consists of a set of operations that can be performed on relations (tables) to retrieve data. These operations are used to build complex queries, and they form the theoretical basis for SQL.

    Here are the main operations in relational algebra:


    a. Selection (σ)

    • The selection operation (denoted as σ) is used to filter rows (tuples) that satisfy a given condition.
    • It selects a subset of the relation that meets a specified condition.
    • Syntax: σcondition(R)\sigma_{condition}(R)σcondition​(R)
    • Example: To select all students who are majoring in "Computer Science" from the Student relation: σMajor=′ComputerScience′(Student)\sigma_{Major = 'Computer Science'}(Student)σMajor=′ComputerScience′​(Student) This would return all rows where the Major attribute is equal to "Computer Science."

    b. Projection (π)

    • The projection operation (denoted as π) is used to select certain columns from a relation, effectively reducing the number of attributes.
    • It creates a new relation by retaining only the specified attributes and removing the others.
    • Syntax: πattribute1,attribute2,…(R)\pi_{attribute1, attribute2, \dots}(R)πattribute1,attribute2,…​(R)
    • Example: To select only the StudentID and Name attributes from the Student relation: πStudentID,Name(Student)\pi_{StudentID, Name}(Student)πStudentID,Name​(Student) This would return a relation with just StudentID and Name columns.

    c. Union (∪)

    • The union operation (denoted as ∪) combines the results of two relations, returning all distinct tuples from both relations.
    • Both relations must have the same number of attributes and the corresponding attributes must have compatible domains.
    • Syntax: R∪SR \cup SR∪S
    • Example: If there are two relations, A and B, with the same attributes, then the union operation would combine all tuples from both relations: A∪BA \cup BA∪B This would return all unique tuples in both A and B.

    d. Set Difference (−)

    • The set difference operation (denoted as −) returns the tuples that are in one relation but not in another.
    • Syntax: R−SR - SR−S
    • Example: To find all students who are enrolled but not in the "Computer Science" major: σMajor≠′ComputerScience′(Student)\sigma_{Major \neq 'Computer Science'}(Student)σMajor=′ComputerScience′​(Student) This would return all students who are not majoring in "Computer Science."

    e. Cartesian Product (×)

    • The Cartesian product operation (denoted as ×) returns all possible combinations of tuples from two relations.
    • If relation R has m tuples and relation S has n tuples, the Cartesian product will have m × n tuples.
    • Syntax: R×SR \times SR×S
    • Example: If Student has 3 tuples and Course has 2 tuples, the Cartesian product would produce 6 tuples, each combining a student with a course.

    f. Rename (ρ)

    • The rename operation (denoted as ρ) is used to rename the attributes of a relation.
    • Syntax: ρnew_name1,new_name2,…(R)\rho_{new\_name1, new\_name2, \dots}(R)ρnew_name1,new_name2,…​(R)
    • Example: To rename the Student relation's Name attribute to StudentName: ρStudentID,StudentName,Age,Major(Student)\rho_{StudentID, StudentName, Age, Major}(Student)ρStudentID,StudentName,Age,Major​(Student) This renames the Name column to StudentName.

    g. Join (⨝)

    • Join is a combination of the selection and Cartesian product operations. It is used to combine tuples from two relations based on a common attribute (usually the foreign key in one relation and the primary key in the other).
    • The most common type of join is the inner join, where tuples are combined when there is a match between the join condition.
    • Syntax: R⋈conditionSR \bowtie_{condition} SR⋈condition​S
    • Example: To join the Student relation with the Enrollment relation on the StudentID attribute: Student⋈Student.StudentID=Enrollment.StudentIDEnrollmentStudent \bowtie_{Student.StudentID = Enrollment.StudentID} EnrollmentStudent⋈Student.StudentID=Enrollment.StudentID​Enrollment This would return a relation combining data from both Student and Enrollment where the StudentID matches.

    3. Example of Relational Algebra Operations

    Consider the following two relations:

    • Student:
      • (StudentID, Name, Age, Major)
      • Rows: (1, 'Alice', 20, 'Computer Science'), (2, 'Bob', 21, 'Math'), (3, 'Charlie', 19, 'Physics')
    • Course:
      • (CourseID, CourseName)
      • Rows: (101, 'Database Systems'), (102, 'Algorithms')

    Performing relational algebra operations:

    1. Select students majoring in "Computer Science":

      σMajor=′ComputerScience′(Student)\sigma_{Major = 'Computer Science'}(Student)σMajor=′ComputerScience′​(Student)

      Result:

      • (1, 'Alice', 20, 'Computer Science')
    2. Project only the StudentID and Name columns:

      πStudentID,Name(Student)\pi_{StudentID, Name}(Student)πStudentID,Name​(Student)

      Result:

      • (1, 'Alice'), (2, 'Bob'), (3, 'Charlie')
    3. Union of two sets of students:

      • Let Student1 contain students majoring in "Computer Science", and Student2 contain students majoring in "Math".
      Student1∪Student2Student1 \cup Student2Student1∪Student2

      Result:

      • (1, 'Alice', 20, 'Computer Science'), (2, 'Bob', 21, 'Math')

    4. Conclusion

    The relational data model provides a way to represent structured data in tables, and relational algebra provides the tools to manipulate and query that data. These operations form the basis of query languages like SQL and are essential for working with databases.

    Understanding relational algebra is fundamental for anyone working with relational databases, as it underlies the operations used to retrieve, update, and manipulate data.

    Previous topic 2
    Entity Relationship Modelling
    Next topic 4
    Structured Query Language (SQL)

    Past Papers

    Open this section to load past papers

    Click on Show Past Papers to see past papers.
    On This Page
      Reading Stats
      Est. reading time11 min
      Word count1,814
      Code examples0
      DifficultyIntermediate