1. Setting up a simple database using EDB

I need to create a database of CUSTOMERS. Each customer has a unique number -
ID - going sequentially from #0001 to #9999. We record the SURNAME, FORENAME,
MIDDLE_NAME, DATE_OF_BIRTH, ADDRESS, and TEL_NO.

Using EDS, I think it would be correct to make CUSTOMERS the TABLE - yes?
What should the KEY be? The ID sounds ideal, but then how do we search for 
customer Smith, John James; born 01/01/2005; at ADDRESS; etc... ?

Sometimes we need to find a particular ID (Eg: "Get the details for customer
#00345"). But usually we've forgotten his ID, and our question is "Get the
details for Smith - I forget his first name". Occasionally we're left a 
garbled message to ring TEL_NO xx xxxx xxxx. Our task then becomes "Get the
details for the customer with that number".

So what should be the KEY, and what should be the DATA, in a database that
will make it possible for us to find that customer using any of his details?
And what routine(s) will make that search possible?

Thank you

Alex Caracatsanis

new topic     » topic index » view message » categorize

2. Re: Setting up a simple database using EDB

Alex Caracatsanis wrote:
> 
> I need to create a database of CUSTOMERS. Each customer has a unique number -
> ID - going sequentially from #0001 to #9999. We record the SURNAME, FORENAME,
> MIDDLE_NAME, DATE_OF_BIRTH, ADDRESS, and TEL_NO.
> 
> Using EDS, I think it would be correct to make CUSTOMERS the TABLE - yes?
> What should the KEY be? The ID sounds ideal, but then how do we search for 
> customer Smith, John James; born 01/01/2005; at ADDRESS; etc... ?
> 
> Sometimes we need to find a particular ID (Eg: "Get the details for customer
> #00345"). But usually we've forgotten his ID, and our question is "Get the
> details for Smith - I forget his first name". Occasionally we're left a 
> garbled message to ring TEL_NO xx xxxx xxxx. Our task then becomes "Get the
> details for the customer with that number".
> 
> So what should be the KEY, and what should be the DATA, in a database that
> will make it possible for us to find that customer using any of his details?
> And what routine(s) will make that search possible?

You can do this with EuSQL.  I recommend using my Euphoria Database Browser
(EDB) to set up and administer the database.

First, create a new EuSQL database (it's really just an EDS database with
some added metadata).  Then create your table named CUSTOMERS, and select
it.  Then go to the 'Table Definition' tab.

Here, you can define your fields.  The first field will automatically
be the primary key--ID is probably the best.  If you want to be able to
assign ids to customers, then choose either Integer or Atom for the 
data type.  If you're happy to let EuSQL start at 1 and increment as 
customers are added, you can use the Autonumber data type.

Then enter your other field names.  Order isn't really important, although
it will determine how the fields are physically stored in the table.
You'll probably want datatypes of Text for these fields.  You can also
use the EuSQL concept of sub-fields for some of them (name and birthdate,
for instance):

NAME.SUR
NAME.FORE
NAME.MIDDLE

DOB.YEAR
DOB.MONTH
DOB.DAY

Then, you can refer to the field as simply NAME or DOB, and the resulting
data would be either
  {"SurName", "ForeName", "MiddleName" }
or
  { Year, Month, Day }

And then click Save Table.  If you know that you'll be doing many lookups
on Surname, you might want to create an index on that field.  This will
store extra data about that field, and make queries searching for a 
specific value much quicker, but it will also slow down inserts, updates
and deletes, and will make the file size larger.  You can create or delete
an index at any time, so it's not critical that you create them up front.

You can enter data directly into the table using the Data tab.

You can try out queries on the SQL tab.  So, to look for someone named
'Smith':

   select * from customers where name.sur like 'Smith'

This will return all the entries in the table that have a surname of Smith.
I recommend looking at the EuSQL docs to see what SQL is supported, and
what you can do with it.

Matt Lewis

new topic     » goto parent     » topic index » view message » categorize

3. Re: Setting up a simple database using EDB

On Thu, 27 Jan 2005 11:41:54 -0800, Alex Caracatsanis
<guest at RapidEuphoria.com> wrote:

I recommend trying Matt's EuSQL (see his post), but I am also
compelled to write about using database.e direct.

>I need to create a database of CUSTOMERS. Each customer has a unique number -
>ID - going sequentially from #0001 to #9999. We record the SURNAME, FORENAME,
>MIDDLE_NAME, DATE_OF_BIRTH, ADDRESS, and TEL_NO.
>
>Using EDS, I think it would be correct to make CUSTOMERS the TABLE - yes?
>What should the KEY be? The ID sounds ideal, but then how do we search for 
>customer Smith, John James; born 01/01/2005; at ADDRESS; etc... ?

First you make a primary table called customers:
	key=ID, Data={Surname,Forename,Middlename,DOB,Address,Phone}

You then write significant chunks of the application, but always
update the database though a handful of trivial routines, eg:
insertCust(), updateCust(), deleteCust(), etc. Most of these are
one-liners, but the important point is they are all in one place.

>Sometimes we need to find a particular ID (Eg: "Get the details for customer
>#00345"). But usually we've forgotten his ID, and our question is "Get the
>details for Smith - I forget his first name". Occasionally we're left a 
>garbled message to ring TEL_NO xx xxxx xxxx. Our task then becomes "Get the
>details for the customer with that number".

As the project progresses, you might find that getting a customer by
telephone number is not that important, so you can just read from
start to finish, looking for a match. But you might find that reading
by surname (especially eg if you want to list in surname order) will
benefit greatly from a surname key, so add:

	key={Surname,ID} Data=0

ID makes it unique, and you should /never/ unnecessarily duplicate the
Data from the primary record (not deliberately, anyway).

You can add these secondary tables any time you like, though you will
need to write some code along the lines of (pseudocode):

	if not db_select_table(blah) then	-- first time, create index
		db_create_table(blah)
		db_select_table(CUSTOMER)
		for i=1 to db_table_size() do
			db_select_table(CUSTOMER)	--important!
			ID=db_record_key(i)
			y=db_record_data(i)
			db_select_table(blah) -- also left at end for
			db_insert({y[n],ID},0)
		end for
	end if		
	db_record_key(...)
	...

and change your insert(), update(), and delete() routines to maintain
the two tables in parallel, eg:

	deleteCust(ID)
		db_select_table(CUSTOMER)
		k=db_record_key(ID)
		y=db_record_data(k)
		db_delete_record(k)
		db_select_table(SURNAMEIDX)
		k=db_record_key({y[n],ID})
		db_delete_record(k)

High-level databases (such as EUSQL) handle a fair amount of this
automagically, but they don't actually do it any more efficiently.

>So what should be the KEY, and what should be the DATA, in a database that
>will make it possible for us to find that customer using any of his details?
>And what routine(s) will make that search possible?

You might use k=db_find_key({Surname,0}), which will set k negative,
so you can use db_record_key(-k) to find the next entry, and carry on
from there, fetching the primary data and checking it carefully.

EuSQL is almost certainly a more flexible route, but I hope at least
part of this gave some understanding of the inner workings.

Regards,
Pete

new topic     » goto parent     » topic index » view message » categorize

Search



Quick Links

User menu

Not signed in.

Misc Menu