Auto-incrementing a primary key field 
Author Message
 Auto-incrementing a primary key field

Hello all,

Sorry, I'm a newbie to VFP 5.0 ...

I'm wondering how to auto-increment a primary key field for every record
I add in a table, and if it is possible to prefix this key with
something like "tv_" followed by the value.

Thanks very much in advance for your great help and greetings from
Switzerland

Rgis Piccand



Sun, 27 Aug 2000 03:00:00 GMT  
 Auto-incrementing a primary key field

Quote:

> I'm wondering how to auto-increment a primary key field for every record
> I add in a table, and if it is possible to prefix this key with
> something like "tv_" followed by the value.

Rather than autoincrement directly from the table, you should have a table
that contains the last value of each primary key, and a function that will
increment the key related to a table or field.

Example :

Keys table
Description (Table, field...) C(20)
LastKey  I

I prefer Integer type primary keys, because generally they provide
sufficient range (> 2.10**9 values), and provide better performance

The function could be something like this :

check if keys table is open, otherwise open keys table
seek record corresponding to table or field
lock record
increment lastkey value
unlock record
return new lastkey value

exemple :

insert into mytable (PKfield, otherfield1, otherfield2,...) ;
                    Values ;
                    (newkey("mytable"), value1, value2...)

This method ensures you have no duplicate keys, even in a multiuser
environment

Hoping this helps

Christian Desbourse (Belgium)



Mon, 28 Aug 2000 03:00:00 GMT  
 Auto-incrementing a primary key field

Complete agree, escpecially if last record is deleted and even pack!!
Avoid miss reuse the same key!!
2nd benefit is if more than one users want to add new record!
The HoldLastKey Table can reply the latest value of Key
This can avoid unwanted duplicate key and later one fail in update!!

Of cos, already assume the buffering is the most open one!! ( 5 )
Os.. Table Buffering!!

Quote:


> > I'm wondering how to auto-increment a primary key field for every record
> > I add in a table, and if it is possible to prefix this key with
> > something like "tv_" followed by the value.

> Rather than autoincrement directly from the table, you should have a table
> that contains the last value of each primary key, and a function that will
> increment the key related to a table or field.

> Example :

> Keys table
> Description (Table, field...) C(20)
> LastKey  I

> I prefer Integer type primary keys, because generally they provide
> sufficient range (> 2.10**9 values), and provide better performance

> The function could be something like this :

> check if keys table is open, otherwise open keys table
> seek record corresponding to table or field
> lock record
> increment lastkey value
> unlock record
> return new lastkey value

> exemple :

> insert into mytable (PKfield, otherfield1, otherfield2,...) ;
>                     Values ;
>                     (newkey("mytable"), value1, value2...)

> This method ensures you have no duplicate keys, even in a multiuser
> environment

> Hoping this helps

> Christian Desbourse (Belgium)

--
Quote:
>^.^< ????|b?q??A?p?pjs?I?I


??????????Y?G http://members.tripod.com/~justok/frames.html
ICQ UIN:       2560423


Fri, 01 Sep 2000 03:00:00 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Auto Incrementing Primary Key

2. auto-incrementing a primary key

3. auto-incrementing a primary key

4. Auto Increment Field

5. Auto Increment Field

6. Auto Increment Field

7. Auto-Incremented field

8. Auto-increment Field in Foxpro

9. auto increment a field

10. auto-increment field

11. auto-generating primary keys

12. Field Validation For Primary Key On A Form

 

 
Powered by phpBB® Forum Software