Subject Re: creating unique key : usingprimair key and foreign key
From Mervyn Bick <invalid@invalid.invalid>
Date Wed, 3 Feb 2021 19:59:33 +0200
Newsgroups dbase.getting-started

On 2021/02/03 18:57, Akshat Kapoor wrote:

> I need some more info before I can give a confirmed suggestion.
> Are you using dbf tables or some other backend database system.
>
> If you are using level 7 dbf tables then you have a field type called
> autoincrement. Most of the backend database systems support this in some
> way or the other.
>
> If you use autoincrement then the unique number generation routine is
> not required.
>
> I think you should type
> help masterRowset
> in your command window and press enter.
>
> It should guide you how to display rows in child tables that are linked
> to the current row in main table.

As I understand it Dirk's problem is not in displaying the data but
getting the correct primary key in the main table to use as a foreign
key in the child table.

In a multi-user system one can't guarantee that the last record in the
main table is the one you've just saved.  Another user may have saved a
record a fraction of a second after you.  SQL RDBMs make provision for
a programmer to get the value of the primary key of a record just saved.
dBASE doesn't. :-(

The solution is not to use an autoinc field but rather use a separate
sequence generator.  Primary keys do not need to be in numerical order
in a table.  They just need to be unique.  If you want to list records
in the order in which they were saved add a timestamp.  Get a value
BEFORE saving the new parent record and use that.  There is code in the
dUFLP (seqval.cc) to do this.

Mervyn.