ARCHIVE SERIES PART 19 : USING KEYS TO LINK DATABASES TOGETHER AND OTHER HANDY TRICKS
We will take a little break from the rather involved listings of the past few columns and look at some database ideas and techniques not yet covered in this series. Even those not following the whole series can easily participate in this part. We will explore how to keep a specific kind of information in a database and quickly be able to find each record by using a ‘key’. This technique will allow us to have one database ‘linked’ to another so we can access useful information from both at the same time. In fact we will set up a 3 level example. We will create a subscription database which has a key to an address database which has a key to a country database. Whenever we access a subscription record we will automatically also access the address information of the subscriber and the country information of the subscriber.
In database systems it works best to keep specific kinds of information in their own independent databases. Like putting addresses in a well defined address database or products to be bought or sold in a well defined product database. That way the fields needed to describe each item can be carefully set up and all entry and maintenance of the information can be done in one place. Then whenever you need information about a particular item in that database you just reference the needed record by a key and don’t have to duplicate the information already there. Of course if the information is not yet in that database you will have to be able to add it ‘on the fly’. Besides the fields needed to properly describe each item in such a database a special field is needed that will uniquely identify each record. This special field is called a ‘key’ or ‘code’. Each key will point to the unique record it identifies in an easily understandable way so the user can quickly find the information when desired. The database will be ordered (sorted) by this key and in ARCHIVE we will use the ‘locate’ command to find the record from the key. When a field is ordered in ARCHIVE only the first 8 characters of the field are used and the rest are ignored. Up to 4 fields can be ordered but we want to work with only our key field and so will only order the one field. This means that the key can only use up to 8 characters. This is really no problem because we want our key to be simple to use and anything longer than 8 characters starts to become to complicated anyway. So our basic premise is that we will have a field in our database that will be up to 8 characters long and each entry in that field will be a unique set of characters that we can easily remember and it will be used to locate that record. The key needs to be unique because in the type of linking we will be using we want it to unambiguously locate just one record.
Since we want addresses that span different countries for out subscription example we will use the following fields types :
Field # Field Description Field Name 0 Address Code acode$ 1 Creation Date date$ 2 Last Name or Company name$ 3 First Name fname$ 4 Street 1 street1$ 5 Street 2 street2$ 6 City city$ 7 State/County/Province stcypv$ 8 Postal Code postcode$ 9 County Code ccode$ 10 Phone phone$ 11 Note note$
The Field Number starts at 0 to be consistent with ARCHIVE convention. The Field Names end in ‘$’ since they are all text fields. More fields could be added but I would consider this to be a minimum. I included a date field as it often turns out to be very useful to know when a record was created. If the address is for a person then the last name would be in the name$ field and the first and middle name would be in the fname$. If the address were for a company then the whole company name would go in the name$ field and the fname$ field would be ignored. The 7th field will hold the State or County or Province depending on the country. Don’t confuse County and Country.
When we need an address we usually already know the name of the person or company in the address so the key should somehow work off the name. You might think we could use the name$ field as the key. But there are problems with this. Names can be longer than 8 characters which would be too long for our key. Also names are not unique. There could be more than one ‘Smith’ in our database. Even including first name and middle name would not eliminate the possibility of duplicate names. And some people own 2 homes and so have 2 different addresses under the same name. Also since names contain both upper and lower case letters and an ‘a’ is totally different than an ‘A’ in ordering we would have to worry about getting the case right when specifying a key.
So the first field, Address Code (acode$), has been added to be the ‘key’ field which can be used to quickly and precisely locate any particular address. Whenever an entry is made into the address database a unique Address Code would be defined so we could thereafter easily locate the address. But what should the rules be for defining each key? That is up to the user since the user is the one who has to use the key. But if the key is chosen without following any rules then it becomes very hard to locate an address later because there is no pattern to follow. When trying to locate an address one misplaced character in a key for a large address database will miss finding the right address. The rules for making a key should be simple and each key has to be unique. In my accounting program for the QL, QLerk, I suggest the following method for keys in an address database :
1. For a person take the first five characters of the last name and then a comma and then the first and middle initial (if any). So ‘Clive Sinclair’ has the key – ‘sincl,c’.
2. For a company take the first character of each word of the company name and those in upper case and then fill out the rest (up to 8 characters) with the other characters in the last word of the name. So ‘Cornish Automotive’ has the key – ‘cautomot’.
Only lower case is used in the key and a maximum of 8 characters are used. If a key has already been defined for another address like the one you wish to assign to a new address then you would have to manually make a change to the new key so it will be slightly different from the key already defined like putting a ‘2’ as the last character of the key. I have found this to be a very good way to create keys for an address database even if more than one person uses the same database. But it is best if the user can define their own keys and even in QLerk the user has final say. So in my example here I will use the above rules for the address keys but you are free to do whatever you like.
Have you ever called up a company that has you on their mailing list and they needed a customer number to find you? If you didn’t have it handy you were out of luck because they used a sequential numbering system for keys. Since the key had no relationship to you it could not be guessed at. Now many mail order companies use keys working off phone numbers or zip codes and name. So they can find you from the information you can give them. Using a sequential number as a key is not very useful since the number has no meaning in itself and if we don’t have it then we won’t be able to find the record we want.
Also notice I included a field called Postal Code (postcode$) since most countries have some internal postal coding system and it needs to be included with the address. We could have a database of postal codes and then we could omit the State/Province/County field and the City field since they are both known if the postal code is known. It would be hard to put together a complete international postal code database but within the USA we can easily understand how a Zip code database could be handy and indeed many address database systems include a zip code database so the state and city do not need to be entered by the user. This makes data entry faster and more reliable.
Also notice I included a field called Country Code (ccode$) instead of the country name. The example we are developing here uses the countries in the gazet_dbf as the country database and we will define a key which will point to the country information. The key will link our address database to the country database so we will have all the country information automatically available to us for each address.
We actually got a little ahead of ourselves because it is best to start at the lowest level of the linking process. We will link the country database to the address database which will be linked to the subscription database. So we need to first create out country database then our address database and then our subscription database. We will have to do a little ARCHIVE programming to create a new version of the gazet_dbf database (we will call it gazet2_dbf) that includes an extra key field.
Our new country database (gazet2_dbf) will be identical to gazet_dbf except we will at a Country Code field to be our country key. So it will look like this :
Field # Field Description Field Name 0 Country Code ccode$ 1 Country Name country$ 2 Continent continent$ 3 Capital City capital$ 4 Languages Spoken languages$ 5 Currency Name Currency$ 6 population in millions pop 7 Gross Domestic Product gdp 8 Area in sqkm area
Notice that we have 3 numeric fields (pop,gdp,area) and the only new field is the Country Code (ccode$). We will get all our information from the current gazet_dbf except for the Country Code for which we will construct from the Country Name and then manually change the few problems that arise. The rule for the Country Code will be the first 5 characters of the Country Name unless the Country has two words or more in the Name and then we will use the first letter of each word and fill out the rest (up to 5 characters) with the last word of the Country Name.
PREPARATION
Start ARCHIVE in your customary way so you see the ARCHIVE command prompt (>). We will assume all files are on flp1_. Have disk with a copy of gazet_dbf on it in flp1_. What to type will be given exactly as it should be typed including the <ENTER>. Coments will be in {}.
CREATING GAZET2_DBF
At the ARCHIVE prompt type the following to create the gazet2_dbf which has the same fields as gazet_dbf plus an extra field for Country Code (ccode$) :
create 'flp1_gazet2'<ENTER> {start create}
ccode$<ENTER>
country$<ENTER>
continent$<ENTER>
capital$<ENTER>
languages$<ENTER>
currency$<ENTER>
pop<ENTER>
gdp<ENTER>
area<ENTER>
<ENTER> {end create}
close<ENTER> {close file}
This created the gazet2_dbf on flp1_. We closed it and it currently has no records.
Now let us start the ARCHIVE editor and create a procedure which will add the records of gazet_dbf to gazet2_dbf. I automatically create the Country Code from the first 5 characters of the Country Name field.
edit<ENTER> {start editor}
transfer<ENTER> {procedure name}
look 'flp1_gazet' logical 's'<ENTER> {lines of procedure}
open 'flp1_gazet2' logical 't'<ENTER>
use 's' : first : use 't'<ENTER>
while not eof('s')<ENTER>
let ccode$ = lower(s.country$)<ENTER>
if len(ccode$)>5 <ENTER>
let ccode$ = ccode$(1 to 5) : endif<ENTER>
let country$ = s.country$<ENTER>
let continent$ = s.continent$<ENTER>
let capital$ = s.captial$<ENTER>
let languages$ = s.languages$<ENTER>
let currency$ = s.currency$<ENTER>
let pop = s.pop<ENTER>
let gdp = s.gdp<ENTER>
let area = s.area<ENTER>
append<ENTER>
next 's'<ENTER>
endwhile<ENTER>
close 's'<ENTER>
order ccode$;a<ENTER>
<ESC> {exit adding lines to procedure}
<ESC> {exit ARCHIVE edit}
transfer<ENTER> {start procedure}
If a problem occurred with the transfer procedure then you will have to first make sure files were not left accessed by typing : close<ENTER> and close<ENTER>. Now delete gazet2_dbf in case a record got added to it by : kill “flp1_gazet2_dbf”<ENTER>. Now create it again as above then use the editor to correct the mistake in your procedure and try again until you get no error.
We now have our new country database and it is still open. To make the Country Code conform to our rules we have to do a little work on the ones not yet right. I’ll give my fixes :
display<ENTER>
locate 'aus' : let ccode$='ausla' : update <ENTER>
next : let ccode$='austr' : update<ENTER>
locate 'cape' : let ccode$='cvisl' : update<ENTER>
locate 'centr' : let ccode$='carep' : update<ENTER>
locate 'costa' : let ccode$='crica' : update<ENTER>
locate 'domin' : let ccode$='drepu' : update<ENTER>
locate 'el' : let ccode$='esalv' : update<ENTER>
locate 'eq' : let ccode$='equin' : update<ENTER>
locate 'ger' : let country$='GERMANY' : update<ENTER>
next : delete <ENTER>
locate 'gui' : next : let ccode$='gbiss' : update<ENTER>
locate 'hong' : let ccode$='hkong' : update<ENTER>
locate 'ivory' : let ccode$='icoas' : update<ENTER>
locate 'kor' : let ccode$='nkore'
let country$ = 'NORTH KOREA' : update<ENTER>
locate 'kor' : let ccode$='skore'
let country$ = 'SOUTH KOREA' : update<ENTER>
locate 'new' : let ccode$='nzeal' : update<ENTER>
locate 'niger' : next : let ccode$='nigia' : update<ENTER>
locate 'pap' : let ccode$='pngui' : update<ENTER>
locate 'puert' : let ccode$='prico' : update<ENTER>
locate 'saud' : let ccode$='sarab' : update<ENTER>
locate 'sier' : let ccode$='sleon' : update<ENTER>
locate 'south' : let ccode$='safri' : update<ENTER>
locate 'sri' : let ccode$='slank' : update<ENTER>
locate 'u.s.a' : let ccode$='usame' : update<ENTER>
locate 'u.s.r' : let ccode$='ussre' : update<ENTER>
locate 'unite' : let ccode$='uaemi' : update<ENTER>
locate 'unite' : let ccode$='uking' : update<ENTER>
locate 'upper' : let ccode$='uvolt' : update<ENTER>
locate 'yem' : let ccode$='yarep' : update<ENTER>
locate 'yem' : let ccode$='ypdr' : update<ENTER>
locate 'mauri' : next : let ccode$='maurs' : update<ENTER>
close <ENTER> {done with changes}
Kind of a job to get all the codes set up but still much easier than doing it all from scratch. You might disagree with a few of the choices that don’t conform exactly to the rule but you can do what you like. Now you can reliably find any country in the world by typing from 1 to 5 characters. Try it and see that it is easy :
look 'flp1_gazet2' logical 'g' {access for looking}
display<ENTER>
locate 'uk'<ENTER>
locate 'usa'<ENTER>
locate 'sl'<ENTER>
locate 'can'<ENTER>
locate 'nk'<ENTER>
close<ENTER>
We now have the country database ready to go. Next time we will continue by creating the address and subscription databases and design a screen with sedit to display all 3 databases at once. Until then, Happy Archiving!
MIRACLE IN OAK RIDGE TENNESSEE
On June 10, 1995, we will be attending the 3rd Miracle show to take place in North America. The last two fun filled events were held in Newport, Rhode Island. This year to give Bob Dyl (the show organizer) a bit of a break, and to give folks in the Midwest and South a better chance at attending the show, it will be held in Tennessee. The host for the show will be the family of Mel Laverne, who lives in the area. It will be on Saturday, June 10, 1995, at the Faith Lutheran Church, 1300 Oak Ridge Turnpike, Oak Ridge, Tennessee. For more information contact Bob Dyl, 401-849-3805.
UPDATE Magazine, Mechanical Affinity, Miracle Systems, Quanta Librarians, and IQLR are definites to be there. I have also been told that TF Services from the UK will be there and possibly Jochen Merz. More in the next issue!
Note: Type-in program listings on this website use ZMAKEBAS notation for graphics characters.