Fetching Table Constraints
RWDBTable contains methods to return the various types of constraints. Fetched constraints are stored differently, depending on the type of constraint and whether all or just specific constraints are fetched. A primary key constraint is stored with the schema. Other constraints are appended to the constraint's list object and returned, and are stored in a global cache if one has been defined.
For detail on constraints and caching, see Metadata Caching and the Cache Manager.
Primary Keys
To return a table’s primary key, first get the table’s schema using RWDBTable’s method describe(RWDBTable::PrimaryKey); then, on the returned schema, call RWDBSchema::primaryKey().
Foreign Keys
To fetch a table’s foreign key, use the RWDBTable method foreignKeys() and pass in the referenced table name:
 
RWDBStatus foreignKeys (const RWCString &refName,
RWDBForeignKeyList &keyList)
 
You can pass in an empty refName to return a list of all foreign keys in a table.
You can also fetch a list of foreign keys that have a given table as their referenced table. For example, to find out which referencing tables contain a foreign key that has the table customers as their referenced table:
 
RWDBForeignKeyList list;
customers.referredToBy(list);
Unique and Check Constraints
Use methods on RWDBTable to return a list of a table’s unique or check constraints. These methods take an RWCString representing the constraint name, and a list object for each constraint type. Each list object is an ordered collection of constraint instances.
 
RWDBStatus checkConstraints (const RWCString &constraintName,
RWDBCheckConstraintList &list)
RWDBStatus uniqueConstraints (const RWCString &constraintName,
RWDBUniqueConstraintList &list)
Overloads are provided for each constraint type that also take an explicit RWDBConnection.
If the provided constraint name is empty, the populated list contains all constraints in the table; otherwise, the list contains just the requested constraint. For instance, this code populates a list with all check constraints in the Invoice table:
 
RWDBTable Invoice = myDbase.table("Invoice");
RWDBCheckConstraintList cList;
RWDBStatus st = Invoice.checkConstraints("", cList);
RWDBSchema also has methods that return constraints as a comma-delimited string to pass when creating SQL statements, for instance, when creating or adding constraints to a table.
 
RWCString RWDBSchema::checkConstraintsAsString(
const RWDBPhraseBook & pb) const
RWCString RWDBSchema::checkConstraintsAsString() const
RWCString uniqueConstraintsAsString (const RWDBPhraseBook &pb) const
RWCString uniqueConstraintsAsString () const
Note that the methods that take an RWDBPhraseBook instance produce an access module-specific SQL string. For constraints that are not attached to a particular RWDBDatabase object, the methods that take no phrasebook object produce a string with generic syntax.
For example, to log the SQL output sent to a database:
 
RWDBSchema sch;
... // Build up the schema for creating a table
RWFile file("myfile.txt");
 
// aDbase is assumed to be a valid RWDBDatabase object
file << sch.keysAsString(aDbase.phraseBook());
file << sch.checkConstraintsAsString(aDbase.phraseBook());
file << sch.uniqueConstraintsAsString(aDbase.phraseBook());