When creating a table for use in Base it is required that the table have a Primary Key field, if the table is going to be updatable via the data view window and forms. Perhaps the most common way to create this Primary Key field is with the use of an Auto_Incrementing integer field. In fact the Base table designer has support for this right in the table design window:
When an Auto_increment field is created this way the following is true:
- Whenever a new record is added to the table a default value for this field will be created
- The first value will be 0, for each record afterwards the value will be incremented by 1
- Numbers will not be reused. ( until all possible values for an integer or bigint data type have been exhausted ) This means that if you add 3 records the ID field would hold the values 0,1,2. If you then deleted the 3rd record and inserted a new one the ID field would now hold the values 0,1,3
- The text in the table design window is INACCURATE, values can be assigned to these fields directly, when done with an SQL command - this should be avoided however
- The value is created WHEN the new record is inserted in the table. This means that in a form when you click on 'New Record' the field will not have the next value yet, instead it will have the string <AutoField>, telling you that a value will be created
- The Base table designer will create this field using a special type specifier - IDENTITY
- Only one of these auto_increment ( IDENTITY ) fields can used in a table
For example - assume a table Contacts that has these fields:
ContactID IDENTITY, FirstName VARCHAR(50), LastName VARCHAR(50)
You could add records to this table using an SQL INSERT COMMAND such as this:
Code: Select all
INSERT INTO "Contacts" ( "FirstName", "LastName" ) VALUES ( "Ted", "Dancen" );
Code: Select all
INSERT INTO "Contacts" VALUES ( NULL, "Ted", "Dancen" );
How about another situation - Suppose I again have the Contacts table, but this time I also have an old SpreadSheet file with Contact information, First Name and Last Name in separate columns in the spreadsheet. Can I bring this old data into my table and still get the auto_incremented field value?
Yes I can actually. It works pretty much just like that first SQL command where I simply did not supply a value for the ContactID field. What I would do then is simply this.
- Open the Calc file with my old contact list.
Highlight the values in the two columns that I want to move into the Base table and copy to the clipboard. ( Remember to insert a row before the first data if you need to, so that you can give a dummy name to each column, otherwise you will lose the first row of data on the import )
Now go to the Base window, select the Contacts table and PASTE. This will bring up the Copy Table wizard.
Select the Contacts table and 'Append data'
What I will do then is to manipulate the lists in the wizard so that I am not supplying any value for the ContactID field: