| Let us attempt to explain a cartesian join in the | | | | The (*) means return all records. The records are |
| simplest of terms, so that even a beginner could | | | | coming from the customer table and the |
| understand. | | | | customer_address table. So, each row returned |
| First, it is helpful to understand how databases | | | | will have all the columns from the customer table |
| store data. They use tables, with rows and | | | | AND from the customer_address table:first |
| columns. Each row is a unique record, containing | | | | name, last name, cust_id, address, city, state, zip, |
| the same types of data, each stored in a column. | | | | address_id, cust_id |
| For example, a table of customer data might | | | | Note the 'where' clause in the query above. That |
| contain columns for the first name, the last name, | | | | specifies to ONLY join the record in the |
| and a customer ID (unique to each customer, to | | | | customer_address table to the record in the |
| help separate two John Smiths, for example). In | | | | customer table where the cust_ids match. That is |
| that table, then, when a new customer is added | | | | a correct way to join. |
| to the database, a new row is added with a new | | | | A Cartesian join, also called a product join, results |
| ID, and the first and last name in the appropriate | | | | when all of the records from one table are joined |
| column. | | | | to all of the records in another. In the example |
| Next, it is useful to know that data can be | | | | above, we can create a Cartesian join by |
| retrieved from the database using something | | | | removing the 'where' clause. |
| called Structured Query Language, or SQL | | | | Let's assume that there are 200 records in the |
| (see-kwul) for short. Different types of databases | | | | customer table, and that each customer has a |
| (Oracle, Teradata, SQL Server, etc.) each have | | | | record in the customer_address table (so, |
| their own individual SQL syntax, but they are | | | | another 200 records in the customer_address |
| generally similar. | | | | table). With the query uses the correct syntax to |
| All forms of SQL allow you to pull data from the | | | | join the two tables, the result set should be 200 |
| table(s) of a database into a set of results, and | | | | rows, with one row per customer also containing |
| they all allow you to JOIN more than one table. | | | | the address information. |
| For example, in addition to the customer table | | | | However, if we remove the where clause, then |
| mentioned above, there could also be a Customer | | | | the SQL will return EACH customer attached to |
| Address table. That table could contain a column | | | | EACH address. Customer 1 will have a record for |
| for address, one for city, one for state, one for | | | | address 1, and address 2, and address 3, etc., up |
| zip, and an Address ID (unique number for each | | | | to address 200. So will customer 2, 3, etc. all the |
| address). And, for the sake of keeping this simple, | | | | way through customer 200. |
| even though you would rarely, if ever, design a | | | | The result set would be 40,000 records long, |
| database this way, it would also contain the | | | | instead of 200. It would display each of the 200 |
| Customer ID of the customer whose address is | | | | customers for 200 times, and 200*200=40,000. |
| in that record. | | | | That is why a cartesian join is also called a |
| That way, you could write a query that selects | | | | product join...because it is the multiplication of the |
| the name information from the Customer table, | | | | records instead of just the joining of records. |
| and JOIN the Customer table to the Customer | | | | To avoid cartesian joins, always specify the |
| Address table, using some kind of syntax similar | | | | criteria for joining tables so that any given record |
| to this:select * from customer, | | | | joins ONLY to the proper matching records in the |
| customer_addresswhere customer.cust_id = | | | | second table. |
| customer_address.cust_id | | | | |