May 3, 2008

Joining Tables

Joining Tables

Databases can have redundant data eliminated by having multiple tables established to eliminate repetition of data within a single table. For example every city has a unique address for each house or building. In our example of a table with a cityname, state, zipcode, and population if we add an address, the name of the state, city, zipcode and population is repeated for every address in the city making the database many times larger than necessary. Therefore it is best to create the following two tables:

  • cityname state zipcode population
  • zipcode address

The zipcode is used to join the tables.

Example

SELECT citytable.cityname, citytable.state, addresstable.address
FROM citytable, addresstable
WHERE citytable.zipcode = addresstable.zipcode;
SELECT citytable.cityname, citytable.state, addresstable.address
FROM citytable INNER JOIN addresstable
WHERE citytable.zipcode = addresstable.zipcode;

No comments:

Post a Comment

Popular Posts