CS 3723
 Programming Languages 
    Database Design   
  with Prolog  


Database Design


Excerpt from a database book (.pdf)
 
SUP_INFO
SNAMESADDRITEM PRICE
Sunshine Produce 16 River St Granola129
Sunshine Produce 16 River St Lettuce89
Sunshine Produce 16 River St Walnuts109
Purity Foodstuffs180 Industry RdWhey 70
Purity Foodstuffs180 Industry RdCurds 80
Purity Foodstuffs180 Industry RdGranola125
Purity Foodstuffs180 Industry RdFlour 65
Tasti Supply Co 17 River St Lettuce79
Tasti Supply Co 17 River St Whey 79
Tasti Supply Co 17 River St Walnuts119
SUPPLIERS
SNAMESADDR
Sunshine Produce16 River St
Purity Foodstuffs180 Industry Rd
Tasti Supply Co17 River St
SUPPLIES
SNAMEITEMPRICE
Sunshine Produce Granola 129
Sunshine Produce Lettuce 89
Sunshine Produce Walnuts 109
Purity FoodstuffsWhey 70
Purity FoodstuffsCurds 80
Purity FoodstuffsGranola 125
Purity FoodstuffsFlour 65
Tasti Supply Co Lettuce 79
Tasti Supply Co Whey 79
Tasti Supply Co Walnuts 119


Example Translated to Prolog:

The Prolog below reproduces the situation described above. The two sets of Prolog "facts": suppliers and supplies are the same as the "proper" way to do database management in the write-up above. These are the Prolog analog to "normalized" relations in database terms. The code below illustrates that one can easily construct the "bad" relation sup_info from the two that are provided, using the Prolog equivalent of relational join. The Prolog code also illustrates how to print information. (In the code below, Prolog tries to match up the information in a rule. The the final presense of fail in the rule tells Prolog to try to match a different way. Because there are write in these rules, a side-effect of the matching is to print the data.)

In the listing below, boldface black is user input, blue is the Prolog source, and red is the output from the Prolog program. (In the final listing, I added few extra tabs by hand.)

SUPPLIES
elk01:~> cat supply2.pl
/* SUPPLIERS (SNAME, SADDR) */
suppliers(sunshine_produce, a_16_river_st).
suppliers(purity_foodstuffs, a_180_industry_rd).
suppliers(tasti_supply_co, a_17_river_st).
/* SUPPLIES (SNAME, ITEM, PRICE) */
supplies(sunshine_produce, granola, 129).
supplies(sunshine_produce, lettuce, 89).
supplies(sunshine_produce, walnuts, 109).
supplies(purity_foodstuffs, whey, 70).
supplies(purity_foodstuffs, curds, 80).
supplies(purity_foodstuffs, granola, 125).
supplies(purity_foodstuffs, flour, 65).
supplies(tasti_supply_co, lettuce, 79).
supplies(tasti_supply_co, whey, 79).
supplies(tasti_supply_co, walnuts, 119).

items(ITEM, SNAME, SADDR) :-
   supplies(SNAME, ITEM, _),
   suppliers(SNAME, SADDR).
   
items_print(ITEM) :-
   items(ITEM, SNAME, SADDR), 
   write(ITEM), write('  '),
   write(SNAME), write('  '),
   write(SADDR), write('  '), nl, fail.
   
sup_info(SNAME, SADDR, ITEM, PRICE) :-
   suppliers(SNAME, SADDR),
   supplies(SNAME, ITEM, PRICE).
   
sup_info_print(_) :-
   sup_info(SNAME, SADDR, ITEM, PRICE),
   write(SNAME), write(' \t'),
   write(SADDR), write(' \t'),
   write(ITEM), write(' \t'),
   write(PRICE), nl, fail.
elk01:~> prolog
Welcome to SWI-Prolog blah, blah, blah ...
?- consult(supply3).
% supply3 compiled 0.00 sec, 3,396 bytes

?- items(granola, SNAME, SADDR).

SNAME = sunshine_produce
SADDR = a_16_river_st ;

SNAME = purity_foodstuffs
SADDR = a_180_industry_rd ;

No
?- items_print(granola).
granola  sunshine_produce  a_16_river_st  
granola  purity_foodstuffs  a_180_industry_rd  

No
?- sup_info(X, Y, Z, W).

X = sunshine_produce
Y = a_16_river_st
Z = granola
W = 129 ;
  (7 items deleted)
X = tasti_supply_co
Y = a_17_river_st
Z = walnuts
W = 119.

?- sup_info_print(_).

sunshine_produce   a_16_river_st      granola  129
sunshine_produce   a_16_river_st      lettuce  89
sunshine_produce   a_16_river_st      walnuts  109
purity_foodstuffs  a_180_industry_rd  whey     70
purity_foodstuffs  a_180_industry_rd  curds    80
purity_foodstuffs  a_180_industry_rd  granola  125
purity_foodstuffs  a_180_industry_rd  flour    65
tasti_supply_co    a_17_river_st      lettuce  79
tasti_supply_co    a_17_river_st      whey     79
tasti_supply_co    a_17_river_st      walnuts  119
false.
?- halt.


Revision date: 2013-04-12. (Please use ISO 8601, the International Standard Date and Time Notation.)