Você está na página 1de 2

Birla Institute Of Technology & Science-Pilani,K.K.

Birla Goa Campus DBSA IS C332 MySQL-LAB4-Manual MORE RELATIONL OPERATORS The example to be considered to demonstrate all the operators is given below.(which was already discussed in the class). People(PID,name,address,gender,age) Planet(Plname,colour,shade,Galaxy) Live(PID,Plname,migratedfrom,status) (A person can be migrated from one planet to another planet at any time. Depending on that the status will be living or shifted. By birth the value of migratedfrom for a given PID is NULL) Property(PrID,PID,Plname,type) GuestHouse(GID,No.of.Bed rooms) Haveasite(SID,sqfts) Natural Join / Theta Join with key word join Example: Observe the result of the following query. select distinct name from People P ,Planet Pl,Live L where Pl.Plname='red' and Pl.Plname=L.Plname and L.PID=P.PID; (Find the names of people who live at least once on red Planet) Can be rewritten as select distinct name from People P join Planet Pl join Live L on Pl.Plname='red' and Pl.Plname=L.Plname and L.PID=P.PID; Syntax is select column name from table1 join table2 join table3 on <condition> LEFT OUTER JOIN and RIGHT OUTER JOIN select * from Planet left join Live on Planet.Plname=Live.Plname; (The result includes all the columns of Planet and Live ,where the columns PID,migratedfrom,status contains NULL values.) Syntax is select column name from table1 left join table2 on <cond> Similarly Right Join select column name from table1 left join table2 on <condition>

In the previous lab manual we have seen all basic operators and the set operator union.In MySQL there is no intersect and minus operators ,but can be implemented with the help of other operators. INTERSECTION Find the Planet names on which atleast one property is available. (It is nothing but the intersection of Plnames (Planet) and Plnames(Property) ) select distinct Pl.Plname from Planet Pl join Property Pr on Pl.Plname=Pr.Plname; MINUS Find the Planets on which nobody is having the property. (It is nothing but Plnames (Planet) Plnames(Property ) select distinct Pl.Plname from Planet Pl left join Property Pr on Pl.Plname=Pr.Plname where Pr.Plname is null; AGGREGATE FUNCTIONS sum,avg,min,max,count Syntax: select aggtfun(columnname) from table; Find the minimum no of bed rooms from GuestHouse . select min(No.of.Bed rooms) from GuestHouse; Try other operators also. Group By Syntax: select * from table group by column name; Group all the records of property by Planet name. select * from Property group by Plname; Order By Syntax : select * from table order by column name; Applying aggregate functions after grouping: Select * ,avg(No.Of.Bedrooms) from GuestHouse group by No.OfBedrooms.

Você também pode gostar