Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>If your table is part of a database container (*.DBC) and not a free table, then you may find code to auto-increment the key in the database container.</p> <p>If you open the table and use the MODIFY STRUCTURE command, you can see the table's properties on the third tab. If the table is a member of a database container, the name of the database will be displayed, along with any rules including triggers and the record rule, which executes on every write to the table.</p> <p>In the days before auto-incrementing fields were added I implemented auto-incrementing fields by adding a record rule as follows:</p> <pre><code>PROCEDURE TABLE_RR LOCAL nOldArea,nUnique IF PK=0 &amp;&amp;Only update primary key field when not already set STORE SELECT() TO nOldArea &amp;&amp;Open table again in another work area, we can't move the record pointer in a Rule IF !USED("TABLE_RR") USE TABLE.DBF IN 0 NOUPDATE AGAIN ALIAS TABLE_RR ENDIF &amp;&amp;Find current maximum value of primary key field, add 1 as next primary key field SELECT TABLE_RR SET ORDER TO PKTable GO BOTT STORE 1+PK TO nUnique &amp;&amp;Update primary key field for our record SELECT(nOldArea) REPLACE PK WITH nUnique ENDIF &amp;&amp;Record rule must return true or changes are rejected RETURN .T. ENDPROC </code></pre> <p>It's normal custom and practice to store such rules in the database container stored procedure code area, however, providing the running application can find the function, it will execute. Clearly, the above code sample assumes that the table is called TABLE, has a numeric (Int is preferable) field called PK with an index tag of PKTable.</p> <p>Because the code is in the record rule, it executes on the record during any update and therefore whilst the record is locked, so there's no collision. Any record written without a value in PK gets 1 + the current highest value of that field. The record rule is intended to validate the record and reject invalid values, and must return true (.T.) for the write to be accepted. Other code can occur in the procedure including setting current values to timestamp fields etc.</p> <p>The record rule can be applied to the table by the ALTER TABLE command:</p> <pre><code>ALTER TABLE 'TABLENAME' SET CHECK PROCEDURENAME() </code></pre> <p>Or through the interactive table editor from MODIFY STRUCTURE</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload