Professional Documents
Culture Documents
▪ How should data be structured to facilitate Join clauses in a one-to-many relationship? What
about many to many relationship?
Ans: Generally, one-to-many relationships are structured using a single FOREIGNKEY. Consider our
example of customers and orders above:
This is a one-to-many relationship, because one customer can place multiple orders, but
one order cannot be assigned to more than one customer. As such, we've defined it with a simple
foreign key in the orders table pointing to a given customer_id, and we can use JOIN clauses in
our SELECT queries fairly easily.
Many-to-many relationships are a bit more complicated. For example, what if we had
an orders table and a products table with a many-to-many relationship: any order can contain
multiple products, and any product can be assigned to multiple orders.
How would we structure our database?
The answer: we use an intermediary mapping table with two FOREIGN KEYs. Consider the following:
Above, we've created a separate table called products_to_orders that maps items on
the products table to items on the orders table. Each row in our products_to_orders table
represents one product-order combination, so that multiple products can be assigned to
one order — and a single product can be assigned to multiple orders.
In this example, we need to use two JOIN statements to link all these tables together: one to
link products_to_orders to products, and one to link products_to_orders with orders.
Ans: 1. Merge join If both join relations come in order, sorted by the join attribute(s), the system
can perform the join trivially, thus: It can consider the current group of tuples from the inner relation
which consists of a set of contiguous tuples in the inner relation with the same value in the join
attribute.
2.For each matching tuple in the current inner group, add a tuple to the join result. Once the inner
group has been exhausted, advance both the inner and outer scans to the next group.
Ans: 1. When two or more tables has been joined using equal to operator then this category is called
as equi join.
/*Equi Join*/
2.Just we need to concentrate on condition is equal to(=) between the columns in the table.
3.Make sure that we are using where clause to apply the condition between two tables.
4.If the condition of join misses or there is not relation between the tables in the join then Equi join
fails and the result will be the Cartesian product or cross join.
Syntax:
Select alias1.column_name1, alias1.column_name2, alias2.column_name1..
The above query will fetch the Employee name from Employee table and Department name from
department table.
Questions: How can you insert multiple rows using one insert statements.
Question: How can you swap values between the rows in a table using single- SQL statement.