Islamic University of Gaza Faculty of Engineering Department of Computer Engineering Fall 2011 ECOM 4113: Database System Lab Eng. Ahmed Abumarasa Database Lab Lab 6 DML part 3
Data Manipulation Language: Introduction: In the Previous labs we see how to get data from one table, now in this lab we will see how to make a complex two or more table. CARTESIAN PRODUCT: The Cartesian product of two tables specifies all possible combinations of values from the tables rows. The CARTESIAN PRODUCT creates tuples with the combined attributes of two tables. We can then SELECT only related tuples from the two tables by specifying an appropriate selection condition. We can see the result of Cartesian product of table 1 and table 2, table3 = table1 X table2 id Name type SSN Name 1 Java Soft 123 Ahmed 4 C# Hard 268 Sara 5 Data Com CD Table 1 Table 2 SSN Name id Name type 123 Ahmed 1 Java Soft 123 Ahmed 4 C# Hard 123 Ahmed 5 Data Com CD 268 Sara 1 Java Soft 268 Sara 4 C# Hard 268 Sara 5 Data Com CD Table 3 How it work. The Cartesian product of two tables A and B give us table C with number of attributes equal to number of attributes of A pulse number of attributes of B. And the number of rows of C equal to number of rows of A multiplies with number of rows of B. Now for every row of A combine it with every row from B. o <123, Ahmed> combined with <1, Java, soft> o <123, Ahmed> combined with <1, C#, Hard> o <123, Ahmed> combined with <1, Data Com, CD> o <268, Sara> combined with <1, Java, soft> o And so on. A X B = C: Attribute C = Attribute A + Attribute b; Rows C = Rows A * Rows B
CARTESIAN PRODUCT in SQL: We can do it in SQL server by adding the table name in FROM clause. Formula: select coulmn1,...coulmnn from table1,table2 E.g. To get the Cartesian product of STUDENT and COURSE table select * from student,course o We can get specific column of the result. select s.sno,s.fname, c.cno,c.name from student as s,course as c o We can put a condition for the result. select s.sno,s.fname, c.cno,c.name from student as s,course as c where c.cno like 'math%' Note: the School Database file in the appendix.
SQL JOIN: An SQL join clause combines records from two or more tables in a database. It creates a set that can be saved as a table or used as it. A JOIN is a means for combining fields from two tables by using values common to each. ANSI standard SQL specifies four types of JOINs: INNER, OUTER, LEFT, and RIGHT. As a special case, a table (base table, view, or joined table) can JOIN to itself in a self-join. INNER JOIN (JOIN) An inner join is the most common join operation used in applications and can be regarded as the default join-type. Inner join creates a new result table by combining column values of two tables (A and B) based upon the join-predicate. The query compares each row of A with each row of B to find all pairs of rows which satisfy the join-predicate. When the join-predicate is satisfied, column values for each matched pair of rows of A and B are combined into a result row. Formula: select * from table1 inner join table2 on table1.coulmna = table2.coulmnb E.g. get the teacher with their class and year. from teacher as t inner join teacherclass as sc Note: for any record in table1 has not a Partner in table2 will not be inserted. Teacher Table teacherclass
LEFT JOIN The result of a left outer join (or simply left join) for table A and B always contains all records of the "left" table (A), even if the join-condition does not find any matching record in the "right" table (B). This means that if the ON clause matches 0 (zero) records in B, the join will still return a row in the result but with NULL in each column from B. This means that a left outer join returns all the values from the left table, plus matched values from the right table (or NULL in case of no matching join predicate). Formula: select * from table1 Left join table2 on table1.coulmna = table2.coulmnb e.g. from teacher as t left join teacherclass as sc Note the NULL value in the result, and see the different from inner join.
RIGHT JOIN A right outer join (or right join) closely resembles a left outer join, except with the treatment of the tables reversed (right table instead of left table). E.g. from teacher as t right join teacherclass as sc FULL OUTER JOIN. A FULL OUTER JOIN combines the effect of applying both left and right outer joins. e.g. from teacher as t full outer join teacherclass as sc
Lab exercises: For the school Database: Get all students and the course they take. Get the all class name and the leader teacher of these classes.