Lecture5 Relational Algebra and SQL

download Lecture5 Relational Algebra and SQL

of 39

Transcript of Lecture5 Relational Algebra and SQL

  • 8/9/2019 Lecture5 Relational Algebra and SQL

    1/39

    1

    Relational Algebra & SQL

    Lecture 5

  • 8/9/2019 Lecture5 Relational Algebra and SQL

    2/39

    2

    Relational Algebra

    The Relational Algebra is used to define the

    ways in which relations (tables) can be

    operated to manipulate their data.

    It is used as the basis of SQL for relational

    databases, and illustrates the basic operations

    required of any DML.

    This Algebra is composed of Unary operations

    (involving a single table) and Binary

    operations (involving multiple tables).

  • 8/9/2019 Lecture5 Relational Algebra and SQL

    3/39

    3

    SQL

    Structured Query Language (SQL) Standardised by ANSI

    Supported by modern RDBMSs

    Commands fall into three groups Data Definition Language (DLL)

    Create tables, etc

    Data Manipulation Language (DML)

    Retrieve and modify data Data Control Language

    Control what users can do grant and revoke privileges

  • 8/9/2019 Lecture5 Relational Algebra and SQL

    4/39

    4

    Unary Operations

    Selection

    Projection

  • 8/9/2019 Lecture5 Relational Algebra and SQL

    5/39

    5

    Selection

    The selection orW operation selects rows from a tablethat satisfy a condition:

    W < condition > < tablename >

    Example: W course = CM Students

    Students

    s

    tud# name cour s

    e100 Fred PH stud# name cour se

    200 Dave CM 200 Dave CM

    300 Bob CM 300 Bob CM

  • 8/9/2019 Lecture5 Relational Algebra and SQL

    6/39

    6

    Projection

    The projection or T operation selects a list of columns from a table.

    T < column list> < tablename >

    Example: T stud#, name Students

    Students

    stud# name course stud# name

    100 Fred PH 100 Fred

    200 Dave CM 200 Dave

    300 Bob CM 300 Bob

  • 8/9/2019 Lecture5 Relational Algebra and SQL

    7/39

    7

    Selection / Projection

    Selection and Projection are usually combined:

    T stud#, name (W course = CM Students)

    Students

    stud# name course

    100 Fred PH stud# name

    200 Dave CM 200 Dave

    300 Bob CM 300 Bob

  • 8/9/2019 Lecture5 Relational Algebra and SQL

    8/39

    8

    Binary Operations

    Cartesian ProductTheta Join

    Inner Join

    Natural Join

    Outer Joins

    Semi Joins

  • 8/9/2019 Lecture5 Relational Algebra and SQL

    9/39

    Cartesian Product

    Concatenation of every row in the first

    relation (R) with every row in the second

    relation (S):

    R X S

  • 8/9/2019 Lecture5 Relational Algebra and SQL

    10/39

    10

    Cartesian Product - Example

    Students Courses

    stud# name cour se cour se# name

    100 Fred PH PH Pharmacy

    200 Dave CM CM Computing

    300 Bob CM

    Students X Courses =

    stud# Students.name course course# Cour ses.name

    100 Fred PH PH Pharmacy

    100 Fred PH CM Computing

    200 Dave CM PH Pharmacy

    200 Dave CM CM Computing

    300 Bob CM PH Pharmacy

    300 Bob CM CM Computing

  • 8/9/2019 Lecture5 Relational Algebra and SQL

    11/39

    11

    Theta Join A Cartesian product with a condition applied:

    R S

  • 8/9/2019 Lecture5 Relational Algebra and SQL

    12/39

    12

    Theta Join - Example

    Students Courses

    stud# name course cour se# name

    100 Fred PH PH Pharmacy

    200 Dave CM CM Computing

    300 Bob CM

    Students stud# = 200 Courses

    stud# Students.name course course# Cour ses.name

    200 Dave CM PH Pharmacy

    200 Dave CM CM Computing

  • 8/9/2019 Lecture5 Relational Algebra and SQL

    13/39

    13

    Inner Join (Equijoin)

    A Theta join where the is the

    match (=) of the primary and foreign keys.

    R S

  • 8/9/2019 Lecture5 Relational Algebra and SQL

    14/39

    14

    Inner Join - Example

    Students Courses

    stud# name course cour se# name

    100 Fred PH PH Pharmacy

    200 Dave CM CM Computing

    300 Bob CM

    Students course = course# Courses

    stud# Students.name course course# Cour ses.name100 Fred PH PH Pharmacy

    200 Dave CM CM Computing

    300 Bob CM CM Computing

  • 8/9/2019 Lecture5 Relational Algebra and SQL

    15/39

    15

    Natural Join

    Inner join produces redundant data (in the previousexample: course and course#). To get rid of thisduplication:

    T

    (Students Courses)

    Or

    R1= Students Courses

    R2= T R1

    The result is called the natural join of Students and Courses

  • 8/9/2019 Lecture5 Relational Algebra and SQL

    16/39

    16

    Natural Join - Example

    Students Courses

    stud# name course cour se# name

    100 Fred PH PH Pharmacy

    200 Dave CM CM Computing

    300 Bob CM

    R1= Students Courses

    R2= T R1

    stud# Students.name course Cour ses.name

    100 Fred PH Pharmacy

    200 Dave CM Computing

    300 Bob CM Computing

  • 8/9/2019 Lecture5 Relational Algebra and SQL

    17/39

    17

    Outer Joins

    Inner join + rows of one table which do not satisfythe .

    Left Outer Join: R SAll rows from R are retained and unmatched rows of S are

    padded with NULL

    Right Outer Join: R SAll rows from S are retained and unmatched rows of R are

    padded with NULL

  • 8/9/2019 Lecture5 Relational Algebra and SQL

    18/39

    18

    Left Outer Join - Example

    Students Coursesstud# name course cour se# name

    100 Fred PH PH Pharmacy

    200 Dave CM CM Computing

    400 Peter EN CH Chemistry

    Students Courses

    stud# Students.name course cour se# Cour ses.name

    100 Fred PH PH Pharmacy200 Dave CM CM Computing

    400 Peter EN NULL NULL

  • 8/9/2019 Lecture5 Relational Algebra and SQL

    19/39

    19

    Right Outer Join - Example

    Students Coursesstud# name course cour se# name

    100 Fred PH PH Pharmacy

    200 Dave CM CM Computing

    400 Peter EN CH Chemistry

    Students Courses

    stud# Students.name course course# Cour ses.name

    100 Fred PH PH Pharmacy200 Dave CM CM Computing

    NULL NULL NULL CH Chemistry

  • 8/9/2019 Lecture5 Relational Algebra and SQL

    20/39

    20

    Combination of Unary and Join Operations

    Students Coursesstud# name address course cour se# name

    100 Fred Aberdeen PH PH Pharmacy

    200 Dave Dundee CM CM Computing

    300 Bob Aberdeen CM

    Show the names of students (from Aberdeen) and the names of their coursesShow the names of students (from Aberdeen) and the names of their courses

    R1= Students Courses

    R2= W R1

    R3= T R2 Students.name Courses.nameFred Pharmacy

    Bob Computing

  • 8/9/2019 Lecture5 Relational Algebra and SQL

    21/39

    21

    Set Operations

    Union

    IntersectionDifference

  • 8/9/2019 Lecture5 Relational Algebra and SQL

    22/39

    22

    Union

    Takes the set of rows in each table and combinesthem, eliminating duplicates

    Participating relations must be compatible, ie havethe same number of columns, and the same column

    names, domains, and data types

    R S R S

    A B

    a1 b1

    a2 b2

    A B

    a2 b2

    a3 b3

    A B

    a1 b1

    a2 b2a3 b3

  • 8/9/2019 Lecture5 Relational Algebra and SQL

    23/39

    23

    Intersection

    Takes the set of rows that are common to eachrelation

    Participating relations must be compatible

    R S R S

    A B

    a1 b1

    a2 b2

    A B

    a2 b2

    a3 b3

    A B

    a2 b2

  • 8/9/2019 Lecture5 Relational Algebra and SQL

    24/39

    24

    Difference

    Takes the set of rows in the first relation but notthe second

    Participating relations must be compatible

    R S R - S

    A B

    a1 b1

    a2 b2

    A B

    a2 b2

    a3 b3

    A B

    a1 b1

  • 8/9/2019 Lecture5 Relational Algebra and SQL

    25/39

    25

    Exercise (May 2004 Exam)

    Employee WorkLoad Projectempid name empid* projid* duration projid name

    E100 Fred E100 P001 17 P001 DB

    E200 Dave E200 P001 12 P002 Access

    E300 Bob E300 P002 15 P003 SQL

    E400 Peter

    Determine the outcome of the following operations:

    A natural join between Employee and WorkLoad A left outer join between Employee and WorkLoad

    A right outer join between WorkLoad and Project

  • 8/9/2019 Lecture5 Relational Algebra and SQL

    26/39

    26

    Relational Algebra

    Operations written in SQL

  • 8/9/2019 Lecture5 Relational Algebra and SQL

    27/39

    27

    Unary Operations

    SelectionW course = Computing Students

    In SQL:

    Select *

    From StudentsWhere course = Computing;

    Projection

    T stud#, name Students

    In SQL:

    Select stud#, name

    From Students;

    Selection & Projection

    T stud#, name (W course = Computing Students)

    In SQL:

    Select stud#, name

    From students

    Where course = Computing;

  • 8/9/2019 Lecture5 Relational Algebra and SQL

    28/39

    28

    Binary Operations/Joins

    Cartesian Product: Students X Courses

    In SQL:

    Select *

    From Students, Courses;

    Theta Join: Students Courses

    In SQL:Select *

    From Students, Courses

    Where stud# = 200;

  • 8/9/2019 Lecture5 Relational Algebra and SQL

    29/39

    29

    Binary Operations/Joins

    Inner Join (Equijoin):Students Courses

    In SQL:

    Select *

    From Students, Courses

    Where course=course#;

    Natural Join:

    R1= Students Courses

    R2= T R1In SQL:

    Select stud#, Students.name, course, Courses.name

    From Students, Courses

    Where course=course#;

  • 8/9/2019 Lecture5 Relational Algebra and SQL

    30/39

    30

    Outer Joins

    Left Outer JoinStudents Courses

    In SQL:

    Select *

    From Students, CoursesWhere course = course#(+)

    Right Outer Join

    Students Courses

    In SQL:

    Select *

    From Students, Courses

    Where course(+) = course#

  • 8/9/2019 Lecture5 Relational Algebra and SQL

    31/39

    31

    Combination of Unary and Join Operations

    R1= Students Courses

    R2= W R1

    R3= T R2

    In SQL:

    Select Students.name, Courses.name

    From Students, Courses

    Where course=course#

    AND address=Aberdeen;

  • 8/9/2019 Lecture5 Relational Algebra and SQL

    32/39

    32

    Set Operations

    Union: R S

    In SQL:

    Select * From R

    Union

    Select * From S;

    Intersection: R S

    In SQL:

    Select * From R

    Intersect

    Select * From S;

    Difference: R- S

    In SQL:

    Select * From R

    Minus

    Select * From S;

  • 8/9/2019 Lecture5 Relational Algebra and SQL

    33/39

    33

    SQL Operators

    Between, In, Like, Not

  • 8/9/2019 Lecture5 Relational Algebra and SQL

    34/39

    34

    SQL Operators

    SELECT *FROM Book

    WHERE catno BETWEEN 200 AND 400;

    SELECT *FROM Product

    WHERE prod_desc BETWEEN C AND S;

    SELECT *FROM Book

    WHERE catnoNOT BETWEEN 200 AND 400;

  • 8/9/2019 Lecture5 Relational Algebra and SQL

    35/39

    35

    SQL Operators

    SELECT Catno

    FROM Loan

    WHERE Date-Returned IS NULL;

    SELECT CatnoFROM Loan

    WHERE Date-Returned IS NOT NULL;

  • 8/9/2019 Lecture5 Relational Algebra and SQL

    36/39

    36

    SQL Operators

    SELECT Name

    FROM Member

    WHERE memno IN (100, 200, 300, 400);

    SELECT Name

    FROM MemberWHERE memnoNOT IN (100, 200, 300, 400);

  • 8/9/2019 Lecture5 Relational Algebra and SQL

    37/39

    37

    SQL Operators

    SELECT Name

    FROM Member

    WHERE addressNOT LIKE %Aberdeen%;

    SELECT Name

    FROM Member

    WHERE Name LIKE _ES%;

    Note: In MS Access, use * and # instead of % and _

  • 8/9/2019 Lecture5 Relational Algebra and SQL

    38/39

    38

    Selecting Distinct Values

    Student

    stud# name address

    100 Fred Aberdeen

    200 Dave Dundee

    300 Bob Aberdeen

    SELECT Distinct address

    FROM Student;address

    Aberdeen

    Dundee

  • 8/9/2019 Lecture5 Relational Algebra and SQL

    39/39

    39

    Exercise

    Employee(empid, name)

    Project(projid, name)

    WorkLoad(empid*, projid*, duration)

    List the names of employees working on project

    name Databases.