Add Table, Remove Table

These commands provide a programmatic way to control what tables are in a database. ADD TABLE adds an existing table to the current database, while REMOVE TABLE removes a table from the database, leaving it a free table.

Usage

ADD TABLE TableName | ?
        [ NAME LongName ]
TableName is the filename of the table (DBF) to add to the current database. If you specify ? instead, the standard Open File dialog appears and you can choose a table to add. (Choosing a file other than a table leads to an error message.)The table to add must not be open. Presumably, this is because the "backlink" to the DBC must be added to the table header, and this kind of change requires exclusive access. (See "DBC, DBF, FPT, CDX—Hike!" in Section 2 for more on this.)Only Visual FoxPro tables can be added to a database. FoxPro 2.x tables aren't eligible. If the specified table is not a Visual FoxPro table, you're prompted to permit conversion. If you don't allow it, the table isn't added. Even SET SAFETY OFF doesn't dispense with this dialog, so be sure you've got Visual FoxPro tables if you're adding to a database in an application. (You can open the files and test them with the SYS(2029).) The dialog that appears is sure to result in support calls otherwise.The NAME clause lets you specify a "long name" for the table. This is a more readable and possibly longer than eight-character name by which the table can be referenced. You can even use names with embedded spaces if you want, but if you do so, you'll have to surround the name with quotes every time you refer to it. We don't recommend using embedded spaces.The database must be open. There's no way to automatically open it or to be prompted as part of this command. If more than one database is open, the table is added to the current database (either the last one opened or the one most recently chosen with SET DATABASE, whichever comes later).In VFP 7, the BeforeAddTable and AfterAddTable events of the database fire if database events are turned on. BeforeAddTable fires after the Open File dialog (if you specified ?) but before the conversion dialog if you're adding a FoxPro 2.x table.

Example

OPEN DATABASE Test
ADD TABLE Test1
ADD TABLE ?

Usage

REMOVE TABLE TableName | ?
       [ DELETE ] [ RECYCLE ]
TableName is the table to be removed from the database. If the table has a long name, you must use it in the REMOVE TABLE command. The DBF name is not recognized. If the long name has embedded spaces, you need to surround it with quotes.If you specify ? instead, you're prompted with a list of tables in the database and can choose one to delete.The specified table must not be open to remove it. Again, this is probably because the header must be changed. Once the table is removed, it's a free table and can be added to another database (unless you add the DELETE clause, discussed below).In versions prior to VFP 7, if SAFETY is ON, a warning dialog appears, reminding you that removing a table from its database loses all long names. Oddly, though, you're not warned about all the other stuff you lose, such as rules, triggers, persistent relations and so forth. Frankly, we think they're far more important than long names. On the other hand, because indexes involving long field names don't get fixed either, and that makes it impossible to open the table, we can sort of see why they point it out. We're very glad that VFP 7 shows a warning pointing out all the problems rather than just picking on one.A table that's the "one" side of a one-to-many persistent relationship can't be removed from a database. First you must delete the relationship (using ALTER TABLE or the Database Designer).The DELETE clause lets you remove the table and discard it in one step. If you specify DELETE, the table is deleted from the disk as well. This pretty much duplicates what DROP TABLE does. If you specify the RECYCLE clause, the table and its associated files aren't deleted from disk, but are placed in the Windows Recycle Bin instead.

Although it seems like the RECYCLE clause shouldn't be legal if the DELETE clause isn't specified, it is. It just doesn't do anything.


In VFP 7, the BeforeRemoveTable and AfterRemoveTable events of the database fire if database events are turned on. BeforeRemoveTable fires after the Remove dialog (if you specified ?) but before the dialog warning about losing information.

See Also

Alter Table, AfterAddTable, AfterRemoveTable, BeforeAddTable, BeforeRemoveTable, Create, Create Database, Create Table, Drop Table, Set Safety, Sys(2029)


Back to Table of Contents

Copyright © 2002-2018 by Tamar E. Granor, Ted Roche, Doug Hennig, and Della Martin. Click for license .