SQL Exercises
Create a directory in your home area and copy the customer.unload, customer_contracts.unload contracts.unload, and contract_items.unload files from /mokihana/homes/cs594ir_sp03/public into it.
Connect to your database using psql, and do the following:
| customers | customer_contracts | contracts | contract_items | ||||
|
customer_id |
int4 |
customer_id |
int4 |
contract |
int4 |
contract |
int4 |
|
name |
text |
contract |
int4 |
description |
text |
item |
int4 |
|
address |
text |
start_date |
date |
start_date |
date |
price |
float4 |
|
city |
text |
end_date |
date |
end_date |
date |
||
|
state |
char(2) |
||||||
|
zip |
varchar(9) |
||||||
|
start_date |
date |
||||||
These tables are an extension of the example used in the SQL Introduction; the idea behind them is that our fictional company has several contracts to which customers can belong to get special pricing. The customers table is where the list of customers is kept, and it is tied by the customer_id to the customer_contracts table, which specifies which contracts a customer belongs to and for what date range they belong to a contract. The customer_contracts tables is tied by the contract field to the contracts table, which contains the list of contracts and includes a description of the contract and the date range during which it was available. The contracts table is then tied by the contract field to the contract_items table, which specifies what items are part of a contract and the price of each item under the contract.
Into customers:
|
customer_id |
name |
address |
city |
state |
zip |
start_date |
|
5 |
We’ve Got It All |
17 Hickory Way |
Atlanta |
GA |
30301 |
10/20/98 |
Into customer_contracts:
|
customer_id |
contract |
start_date |
end_date |
|
5 |
9 |
1/1/00 |
12/31/01 |
|
contract |
item |
price |
|
5 |
1 |
2.30 |
To read:
|
contract |
item |
price |
|
5 |
1 |
2.45 |