1. Home
  2. Explore
  3. Computer Science
  4. Relational Algebra

Study methods

Flashcards
Quiz
Fill in the blanks
Written test
Lesson
Notes
Podcast
Content

Relational Algebra

Transcript

hi my name is raymond and welcome to module 9  relational algebra together with my group mates   we will be teaching you about what relational  algebra is and the operations that we can perform also for every section we will provide  an exercise to test your understanding at the end of the module we will give challenges  for you to apply what you have learned let's first understand what relational algebraics

relational algebra is a data manipulation language  for relational model it is a procedural language   in which user needs to specify how to retrieve the  expected data it consists of a set of operations   that take one or two deletions as input  and produce a new relation as the result   multiple operations of relational  algebra together form an expression the components of relational algebra includes operands in algebra a mathematical operation  consists of operands and operators in relational   algebra the operands are the relations  or the variables that represent relations

operators this maps relations to the  relations rules for combining operands   and operators to the relational algebra expression  and those for evaluating those expressions for this module we will be using relax an online  relational algebra calculator to facilitate   your learning of relational algebra relax we  will guide you through the usage of this tool go ahead and open the link in your browser when you first load the deluxe calculator the  default data set is displayed at the left side on your right side you can input your relational  algebra expression and then execute the query

let's try to load a different data set click  on this part then let's select the imdb sample now it loads all the relations  under the imdb sample data set let's try to view one of the directions  you can either type actors relation here or simply click on the actual civilization  name and then execute the query then it loads all the records  under the actors relation there are four types of operations in relational  algebra set operations unary operations

binary operations and extended operations i'll hand you over to jay to  discuss about the set operations thanks raymond for set operations we  will take a look at the different set   theory operations available for us we are also  going to check available samples and exercises   to be able to understand these operations  properly please be advised that the topics of   set theory will not be covered in this video  only the operations in set are the subject   of this video my name is joran gauten  and i'll be your top leader for this unit

before we proceed i don't need to  review some terminologies that we   are going to use throughout this section set  is a collection of anything of the same type in this section it will be  the doubles and the rows tuple is an immutable ordered sequence of values  it is equivalent to grow in a relational database relation is a group of tuples it is also  equivalent to a table in our relational database while attribute is a field in relation or table decrease the number of  attributes given in a relation

domain is a data type of value that is only  permitted for a given attribute while union   compatible is a relation pair that has the same  degree of attributes and each attributes are   on the same domain set theory operations are  the standard mathematical operations and set these operations are binary operation because  these are operated in two relations unlike unary   operation that will be discussed later on this  chapter set operations has four operations and   these are union intersection set difference  and cartesian product let's start with unit

a union is a set of tuples from both relation r   and relation s resultant relation will have a  degree equals to the degree of relation r and s   meaning there is resultant relation the  number of attributes in a resulting relation   is the same of the relations this is equals  to the attributes of the relations r and s duplicates are eliminated in this operation  relation r and s must be union compatible   it is denoted as r union s so from this  diagram you can see that the result of any   union operation is consists of elements of  relation r plus the elements of religion s

whereas the duplicates are  removed let's take a look at an   example in this example we will have  a relation rider and relation client   from our user data set by vision inspection we can  say that these relations are a union compatible   because it it has the same degree writers has  four attributes and clients has four attributes and their attributes are of the same domain   so for example the writer's tag is a type of  hint and the client's type is a type of it if you execute a union operation and these two  relations what do you think will be the result it just combined the relation

now let's check this example  in action by relax calculator if you're not familiar with relax calculator  please watch raymond's video about the relax   okay so moving on first we need to load our data  set this data set will be provided to you later just copy the contents inside the group editor tab hit the preview and hit the use grouping  editor if there is no error in importing so in the left in your left panel you can see  the three relations we have in our data sets

let's go back to the relational algebra tab and  perform the union operation first select the first   relation you want to perform the union and then  from our kitchen sink select the union operation remember to put a space on  on each side of the operation   then select the second or second relation  hit the execute query and see the result if you notice that we have the yield  the same result as our example earlier now let's proceed to our next  section it is which is intersection intersection is a set of all  tuples that belongs to r and s

resulting relation will have a degree equals to  the degree of relations r and s similar to what a   union is relation r and s must be union compatible  you must remember that not all relations   are union compatible they denoted as r intersects  s so from this diagram we can expect that the   result of an intersection is are the elements  from r that is present that is also in relations whereas duplicates are also removed  let's take a look at the example so let's use our example from youtube   to execute the intersection in this  relation it will give the result

john cena only because john cena is the  only record that that is present from   in both relation are are relation  writers and relation clients because as per definition of  intersection if the only yield   a result that is present on both relation  let's verify this example in order else so let's clear the previous query select the first  relation select the intersect select operation   from our kitchen sink and then select  the second relation hit the execute   query if there's no error in your syntax and  check there is a that's that symbol right okay so let's move to set difference

a set difference is a set of all tuples that  belongs to r but not in s the order of the   operands affects the result let's check  an example later to see what that means resultant relation will have a degree  equals to the degree of relations   r and s similar to union and intersect  relations r and s must be union compatible it is denoted as relation r minus  relation s so from this diagram we can see that the results are  consists of elements from relation r   that are not elements of relations  so from the example from the example   from the previous operations we  have the writers and the clients

we execute the set difference from in these two  relations what do you think will be the result so as expected we are only seeing  the tuples from righteous relations   that are not in the clients so donations so john cena is present in  writers and also present in clients relation so it was excluded in  the rel resulting relation what if we   swap the position of this true relation  what do you think will be the result we said earlier that the order of operands in set  difference affects the result if we execute the   set difference and destroy relations the result  will be a much different from our previous one

yeah that is because the base relation we use  now is the client relation so the statement is get all the tuples that are from  clients but not from writers relation   so we have the mary harold joy so we can verify this example in reacts calculator so let's remove again the previous query so  as as before select the first installation   select our operation in our kitchen  sync then select the second relation

let me execute it if there's no error please  verify the result we have the same result   as our first example right so let's swap  the position so select your first relation   this time it should be the clients  our minus operation and then writers it's too simple right thanks for the author of  this relax and we can uh play along with the set   operations correctly yeah so they need execute  query and then we can see the results right so there's another operation that we need to  discuss the cartesian product partition product   has much difference from the first three  operations what are those let's have a look

so first let's define the cartesian product   it is a concatenation of each tuple from  relation r with every tuple of relation s the resultant relation will have the degree  equals to the sum of the degrees of r and s so if r and s has both four attributes  the resultant relation will have an   eight attributes so we can see that later in  the example the size of the relation will be   the product of the tuples of r and s and and this  time it is not required to be a union compatible

cartesian product is denoted as relation r cross  relations so from this diagram we can expect that   the result should be each element of relation r is  paired with each element of relation or set yes so did you get all the concept of  criticism product if not let's have   an example so from our previous example we  have two relations drivers and the clients and we execute the product operations and  these two relations it will yield to a result

that is a concatenation of each double of riders   to each of the tables of the clients so as you  notice that john cena iterates from the riders   relation and then each item from the writer's  relation is paired with rid of each the tables of clients relation right and there and  their uh attributes is combined also so the i think the use case for these operations  is when we are performing a joint operation   from sql let's see in action okay so riders let's select  the cross join and then

the second relation hit the execute  query and you can see the results so it's it is paginated because it's just  limited to 10 results each display okay i hope you got all the information  about the set theory operations   for any questions feel free to post it into  our forum and we'll happy to assist you   and also assistant professor  quesado will clarify it for us   that concludes our set theory operations thank you  for watching but before i hand you over to raymond   let's have a few exercises to check what you  have to understand with the set jury operations

okay for these exercises you need to have  a knowledge about unary operations or any   operations that may yield to another relation  such as but not limited to select and projection the prerequisite you should load the data set  provided in relax calculator and then perform or   i think you need to find and a set operation or  operations that yields to the following results   so a or the first name and last name of the  writers and admin b list of clients that are admin   c list of admins that are not writer and d product  riders clients and admins okay so the goal of this   exercise is to let you know that we can mix and  match any operations to get the results we want

so one tip uh to to perform this exercise is to  have a first the unary operations or at least   any other operations so let's have an example  first so you know what what i mean okay from our   our data sets let's select the imdb sample  so let's for example may perform a union operation on the actors and  directors or let's swap directors and actors so you can see there's an error because  directors and actors are not union compatible   so how are we going to do that by  the help of immunity operations

let's use the projection and then limit  the the attributes that we're going to use   from the actors relation so in  this case you need to id first name last name to match the director's relation right so as you can see the error is  gone and we can execute the the query   right you can see there's it right so i'm coding

okay so i hope you got your  answers for our previous exercises   and i'm showing you the that one of the possible  answers or the possible solutions for our exercise   uh remember that there is a a lot of  possibilities that will yield the same result but uh yeah this one is the  one of those possibilities   i hope you got everything right and handing  you back to raymond for unity operations thanks jade i will now be discussing about unity  operations a unity operation means that the input   of the operation is only one relation  and its output is also a relation

in this section we will be looking at four unary  operations select projection rename end assignment the select operation returns  a subset of the tables   from a relation that satisfies  a selection condition it is the horizontal filter of devolation meaning  it selects some rows and discards the other rows the notation for select is sigma followed  by the condition and then the relation for the condition you can use any of the  comparison operators including and or and like

let's say we want to select all records  with last name ending with beach so our expression will be  sigma followed by last name equals [ __ ] followed by the actors  variation and this is the output   let's say we want to retrieve all movies in  the year 2003 we drank greater than seven so first we select on the  we click on the selection type here equals 2003 space and followed by rank greater than 7 and then followed by the movie situation

then execute now we get all movies in the year  2003 with the rank greater than seven next step projection this operation returns a set  of tuples containing a subset of the attributes   in the original relation if select is a horizontal  filter projection is the vertical filter   of the relation that is it selects some  columns and discards the other columns the notation for projection is i followed by  the list of attributes followed by the relation

if we want to retrieve only the first  names from the actors relation we'll write   i followed by first name and then the  actors relation and this is the output let's say we want to get the first name and last  name of app first with first name equal to michael first let's try to filter the  records from the actor's relation with first name equals to michael so  we do that by clicking on the sigma then click on the first name type equals micro followed by the actors relation then execute

so as you can see we have all the records  whose first name is equal to michael now we want to project only the first name  and the last name attributes so we do that by clicking on the pi symbol  followed by the first name   comma last name and for clarity we just enclose  this inside the parenthesis then click on execute now we only project the  first name and the last name rename operation can be used to the name  adelation the relations attributes or both

for renaming the validation the notation is raw followed by the new relation name followed  by the relation that you want to the name for renaming the attributes the notation is  followed by the list of new attribute names   inside parentheses followed by the  relation lastly the notation for the   naming both the relation and its attributes  is though followed by the new relation name followed by the list of new attribute names  inside parenthesis followed by the relation let's say we want to rename the  relation directors to producers

we write raw then producers  followed by the directors variation as you can see the table name  prefix is changed to producers uh first let's load the director's variation  so click on the directors then execute as we can see the direct consideration  has the following attributes id   first name and last name let's try to rename them  to director id f name and l name respectively so in the relaxed calculator it has a different  syntax on the naming relations and attributes so first we click on the row symbol

followed by the id then to the name  it we either select the right arrow   or the left arrow but since we're  going to the name it from old   to the new name then we use the  right arrow then we type in the new attitude name comma then let's the name the  first name then click on the right arrow then the new name f name comma then last name  followed by the right apple then the new name   element space then the director's relation then execute now we can see that we have renamed all the  attributes of the directory acceleration

assignment provides a convenient  way to express complex queries it must always be made to a  temporary liberation variable the idea is to write the query a sequential  program consisting of a series of assignments   followed by an expression whose value  is displayed as the result of the query   the notation is temporary variable followed by the  back arrow followed by evaluation or expression in this example we first do a select on actors

relation where gender is equal to m then  we assign the result of this expression   to a variable named male actors now we can use  the male actors variable and do a projection on it let's get the combination of relations upwards  and movies journalists and assign the expression   to variable t after that we're going to select  records whose genre is action from the temporary   variable t okay first let's try to project the  first name and last name from the actors iteration so we do that by clicking on the file click in the  first name comma last name space upwards relation

okay then we cross part of it with the movie's  genres so space cross join space the movies genres okay let's project only the genre attribute  so click on the pi then type in the general   space let's enclose it with the parentheses okay  then execute the query so you see that we combine   the actors relation and the movies generals let's now assign this to a temporary variable  called t so we type in t equals the expression

after that we're going to select the records whose  genre is action from this temporary variable t so we do that by clicking the sigma  symbol then typing the genre equals action space the variable t then execute now we were able to filter the records  having the genre equals to action

in this exercise we will be using the imdb sample  data set in relax go ahead and open the relaxed   calculator and load the imdb sample data set for  exercise number one you need to find all actors   whose name starts with ch for the second exercise  get the first name and last name of the vectors whose first name is stanley and rename the  attributes as f name and l name respectively pause this video and try to formulate  your expressions on the velux calculator when you're done play the  video to check your answers

now i hand you over to mon  for the binary operations thank you raymond i'm juan gonzalez  and i will be talking about binary   operations what is binary operations binary  operations combines two input relations   into one output relation in this section we  will be looking at four binary operations data join equi-join natural join and division  some of the operations that will be discussed   in this section will use selection and  projection mentioned in the previous section

data join it joins two relation based on the not  equal condition represented by teta it is also   known as conditional join here are the sum of  the conditional operators and the notation is in this example our condition is pc speed   less than the laptop's speed  let's take a look at pc model 1001 and let's check our laptop relation if  there is a laptop that is greater than 2.66

there is none how about  model 1003 with speed of 1.42 the laptop model 2001 2002 and 2003 and so  on have a speed that is greater than 1.42   so those tuples are included in our  data join 1001 2002 3 4 5 6 and so on   for data join demo we will be using database  systems the complete book exercise 2.4.1 set and   then our problem is to get the pc that has better  ram than the laptop in here we set the condition

to pcrm greater than the laptop ram and when  we execute that we'll see that pc model 1001   is greater than uh laptop model 2003 pc model 1001  has a ram of 1024 while laptop ram only has 512. echo join equijoin is done when a tata  join uses only the equivalence condition   the notation for echo join is using the same relation from theta join let's  take a look at our example if we use equijoin   let's start with pc model 1001 with the speed  of 2.66 can you see a laptop with the same speed

let's move on to one and 1002 the only tuple that we can see with the same  result is 1009 and it has the same speed as   2001 2004 and 2010. for the equi-join  demo we will be using the same data set our condition is pcram equals the  laptop ram and when we execute that   we can see that 1001 and 2002  laptop model has the same ram

natural join natural join does not utilize   any of the comparison operators in this type  of joined attributes should have the same name   and domain in natural join there should be at  least one common attribute between two relations   it performs selection forming equality on  those attributes which appear in both relations   and eliminates the duplicate attribute  the notation for natural join is the example is about the natural join of a product  and a pc for the product we select that all the   attributes as for the pc we will only get the  model and price as a rule in a natural join   both relation should have a common attribute  in this case the common attribute is model product a produces pc with prices of

2014 995 and 478. maker b has 649 630 and 1049  and so on the model are one thousand one one   thousand two and three for a one thousand four one  thousand five one thousand six four b and so on for the natural natural demo we will  get the product that produce laptop first first we project the maker and  the model from the product then we   naturally join it with the laptop and  when we execute that we can see that

laptop model 2004 is produced by product  maker a and so as 2005 and 2006 model   for b it produces a 2007 model and so on division  the relation returned by division operator will   return those tuples from relation r1 which are  associated to every r2 tuple the notation is

in this example we can divide laptop relation with  pc when we divide the laptop with pc we get the   result of laptop models 2001 2004 2006 and 2010  because they are the ones with the speed of two   2001 with laptop speed of 2 2004 2006 2010  one thing to notice here is that speed is   not available to the result that is one  of the features of the division it will   not include the attributes used in the division  so the result is the attributes model and price

for the division demo we will use the same data  set and our problem is to define divide laptop   with pcram of 24. before we before we divide the  laptop with the pc first we select a pc that has 1024 rom and we project the ram attribute and  then we divide it with the laptop relation when   we execute that we get the following result as you  can see these are the laptop that has ram of 1024 2002 2005 2007 in 2008 to verify  that we can reselect the laptop by   choosing the laptop relation and execute the query as you can see here laptop 2002

has 10 1024 ram so as 2005 2007 and 2008 again here's the result 2002 5 7 and 8. for the  exercises load the database system the complete   book exercise 2.4.1 and relax and answer  the following question join laptop and pc   to get the pc that is more expensive than a laptop   select and project pc with each 80  hd value then divide it to laptop here are the answers in the exercises next we will talk about extended  operations i will hand you over to victoria

Thank you Mon and hello all. Extended operations in relational algebra increases power over basic relational algebra. In this section we will be looking at 4 extended operations. Generalized projection, aggregation, outer join and outer union. The first operator in extended operations that we will discuss is the generalized projection.

This operator is an extension of the operation in the unary operation which is the projection. But in this case, arithmetic functions can be used in the projection list on certain attributes. The generalized projection has the form: π, F1, F2, Fn, E. where E is a relation algebra expression, while F1, F2, F3, up to Fn are arithmetic expressions.

As an example, the following are customers of a credit company, each has their own credit limit as well as their own balances, depending on their own spendings. If the credit company wants to see the money currently consumed by each customer The formula π, customer name, credit limit minus credit balance, credit info can be used. As shown in the output, customer name appears with the difference of the credit limit and the credit balance in our query which is the credit consumed.

Let us see this example in the demonstration using relax relational algebra calculator. After loading the database of the credit company, let us try our query and see how it works. Click π customer_name, credit_limit - credit_balance -> credit_consumed (credit_info) and execute query

We will see the result of the customer name and the credit they have consumed. Aggregation is the second extended operation in this presentation. Aggregate functions take a collection of values as input and return a single value. The aggregate operations are sum, average, count, min and max.

Aggregation has the form, where the caligraphic G is used to show that aggregation is to be applied and its subscript specifies the aggregate operation to be applied. The subscript grouping_attributes can be eliminated depending on the kind of query we want to apply. In this example, a list of employees from different department is shown as well as their salaries.

If we want to get the total salary in each department, we can use the query Dept G sum (salary) (propEmp). Dept is a group attribute, caligraphic G is the symbol that aggregation is used Sum(Salary) represents the aggregation function and propEmp is the relation.

Applying this query will result to this output. Again, let us see this example in the demonstration using relax calculator. After loading our database, we can put our query as γ dept; sum(salary) -> totalDeptSalary (propEmp) And then, we can execute the query

Now, we can see the total department salary per department. Oliver will now take you through to the remaining extended operations. thanks victoria i will continue the extended  operations part and discuss how outer join   works the outer join is the same as the joint  operation but the main difference is keeping   the tuples of the relation and padding null  to the records that do not have default values there are three variations for outer join left  outer join right outer join and full outer join

for a left outer join we keep the tuples  of the left relation and then we place   null values on the right relation the two that  have its equivalent value the right outer join   keeps the tuples from the right relation and  places the null values on the left relation and for full outer join we keep  the tuples for both relations   and we place possible null values on both  relations the important point for the symbol   is the bracket portion for this will help indicate  which tuples are to be complete the next slides   will show examples of the variations and how the  result will look like based from the sample data the first variation i will show is left outer  join i will be using two relations as our example relation a gives the square value of a number  while relation b gives a cube value of a number

when we apply relation a with relation b  using left outer join we can see in the   resulting table that the tuples of relation  is complete since number 4 does not exist   in relation b the cube value through the left  outer join will have an empty or have a null value the next variation i will discuss is right outer  join i will be using the same two relations as our   example when we apply relation a with relation  b using right outer join we can see in the   resulting table that the tuples of relation b  is complete since number five does not exist   in relation a the square value through the right  outer join will have an empty or have a null value

and the last variation i will discuss is full  outer join i will still be using the same two   tables as our example when we apply relation  a with relation b using full outer join we can   see in the resulting table that the tuples  of relation a and relation b is complete   since number four does not exist in relation b  the cube value through the full outer join will   have empty or have an l value and since  number five does not exist in relation a the square value through the full outward  join will have empty or have a null value i will now show a quick and  short demonstration using   the relaxed platform and load the photodb dataset

let's load the employees relation we see  that the employees relation has seven records and the photographer's relation  has three rows or three records using left outer join we will retrieve  the employees that are photographers so we load employees load the symbol for left  outer join set the condition we're in the person id of the employee is equal to the  employee id of the photographer's relation when we execute the query we see that the  result will yield seven records or seven rows

upon inspection of the result table we can see  that the employee's relation has complete tuples well we can see in the photographer's relation that there are some  records that have null values that is due to having then employee id not existing with  the person id of the employee's relation next i will show how the right outer join works  and will retrieve the senior employees that has a   bonus equal to their salary so  let's load our seniors relation in our seniors relation we can see that there  are two seniors with their equivalent bonus

let's load our employees relation we can see  that the employees has seven records having their   corresponding salary let's use the right author join to retrieve  the result of having the bonus equal to their   salary so we click on senior then  load the symbol of right outer join we're in their bonus is equal  to the salary of the employees and we execute our query

the query will yield a result of having  seven records or seven rows and when we   when we inspect the result table we can see  that the employees relation has complete tuples and there are no equivalent records  in the seniors table since we were trying to retrieve the record wherein there the  bonus is equal to the salary of the employee and lastly i will show how full  outer join works by loading the   wikipedia relational algebra data set and we will  be using the both and car relations for example

let's retrieve car relation first we see  that car has three rows or three records while both also has three records now we will do a full outer join by checking  if there's a boat price that is also equal to   a car price so in let's load both load the symbol  uh full outer join set the condition where in the price of the boat happens to be  equal to the price of the car when we when we execute our query we will see  that the result will have six rows or six records

and upon inspection of the result table  we can see that the tuples for both the correlation and the both relation are included  and null values are placed for non-existing data so we can see here that there are null values for both the car relation and the vote relation  this concludes the demo for outer join

next i will discuss how outer union works  outer union is the same as union but also   considers the attributes that are common in the  relation but are not necessarily type compatible   thus outer union is equivalent to full  outer join with the common attributes let's refer to this slide for an example on outer  union relation a gives the initial value and max   value of a variable while relation b gives the  increment value and initial value of a variable based from relation a and relation b we can  identify that the common attributes are value   and var based on the column name and data  type we can also see here that the max column is either a string or an integer data

and for the increment column we see that the  data type used for storing its value is integer when we do an outer union outer  union will merge the common columns   and we will be able to see the tuples of  both relations with empty or null values   being placed to non-existing data  which will be based on the data type we can also see in the resulting  table that the max column have null values since there's no specified  data type while in the ink column we see that   instead of having the null values zero  in is placed because it has a data type let's go back to the relaxed platform  and load the photosdb dataset

so we can see how the outer union works so using the outer union we'll try retrieving  the persons that are also salespersons in order to do that let's load persons load the full outer join symbol set the condition wherein the id of the person is equal to the  employee id of the salesperson upon executing the query we will see that the  result will yield eight rows or eight records

and when we we inspect the result table we can see  that the tuples for both the person relation and   the salesperson relation are included where the  null values are also placed to non-existing data this concludes the demo for outer union Thank you Oliver. And now it's time for the extended operations exercise.

For the instruction of question number 1, load the Database Systems The Complete Book - Exercise 2.4.1 dataset in RelaX. Question is, using generalized projection, get the total cost of each laptop model after adding 200 pesos for the delivery charge. For question number 2 instruction, load the Wikipedia - Relational Algebra dataset in RelaX. And the question is, using an outer join with department relation, retrieve the list of employees.

Please go ahead and open RelaX calculator and put your query and come back to see the answers. now we have an idea and understanding  on how relational algebra is related to   database management systems before we conclude our  discussion and presentation on relational algebra   we will give a challenge for us to do load the  relaxed platform and use the imdb dataset for   the challenge let's retrieve the number of movies  created by each director that has a movie rank   equal or greater than 8 and was shown between  1960 and year 2000 don't forget to share your

answer to the challenge in the discussion board  all the best and enjoy using relational algebra this concludes the group's discussion and  presentation on relational algebra thank   you very much and let's continue to enjoy  learning in database management systems

Ask anything

We've read this whole study set, so questions about it get the best answers.

FlashcardsQuizFill in the blanksWritten testLessonNotesPodcastContent