OLE DB Programmer's Reference |
Adds a new constraint to a base table.
HRESULT AddConstraint( DBID *pTableID, DBCONSTRAINTDESC *pConstraintDesc);
Parameters
typedef struct tagDBCONSTRAINTDESC { DBID *pConstraintID; DBCONSTRAINTTYPE ConstraintType; ULONG cColumns; DBID rgColumnList[]; DBID *pReferencedTableID; ULONG cForeignKeyColumns; DBID rgForeignKeyColumnList[]; OLECHAR *pwszConstraintText; DBUPDELRULE UpdateRule; DBUPDELRULE DeleteRule; DBMATCHTYPE MatchType; DBDEFERRABILITY Deferrability; DB_URESERVE cReserved; DBPROPSET rgReserved[]; } DBCONSTRAINTDESC;
The elements of this structure are used as described in the following table.
Element | Description |
---|---|
pConstraintID | The constraint identifier. If this is null, the provider generates a unique ID for the constraint. This ID is not returned to the consumer through the DBCONSTRAINTDESC structure. Consumers should generally specify a value for the constraint and not set pConstraintID to null, because there is no easy way to get back the ID for the added constraint. |
ConstraintType | The type of the constraint as defined in the DBCONSTRAINTTYPE enum. One of the following values: DBCONSTRAINTTYPE_UNIQUE |
cColumns | The number of elements in the array passed in rgColumnList. For check constraints, this is always zero. If cColumns is zero, rgColumnList is ignored. |
rgColumnList | For unique and primary key constraints, this contains the list of columns that comprise the constraint. For foreign key constraints, this defines the list of column IDs (DBIDs in the referenced table) that make up the referenced key in a relationship. The order of the elements in this array comprise the key columns in descending order of significance. As such, the first element is the most significant key column and the last element in the array is the least significant key column. |
pReferencedTableID | For foreign keys, this contains the referenced table in a relationship. For all other types of constraints, this is null. |
cForeignKeyColumns | The number of elements in the array passed in rgForeignKeyColumnList. If cForeignKeyColumns is zero, rgForeignKeyColumnList is ignored. This field must be zero if pReferencedTableID is a null pointer. |
rgForeignKeyColumnList | The columns (DBIDs in the base table) that comprise the foreign key in a relationship. The order of the elements in this array comprise the key columns in descending order of significance. As such, the first element is the most significant key column and the last element in the array is the least significant key column. This field is ignored if cForeignKeyColumns is zero. |
pwszConstraintText | The constraint clause. If ConstraintType is not DBCONSTRAINTTYPE_CHECK, this must be a null pointer. |
UpdateRule | The update rule (as defined in SQL-92). One of the following values: DBUPDELRULE_NOACTION This field is ignored unless ConstraintType is DBCONSTRAINTTYPE_FOREIGNKEY. |
DeleteRule | The delete rule (as defined in SQL-92). One of the following values: DBUPDELRULE_NOACTION This field is ignored unless ConstraintType is DBCONSTRAINTTYPE_FOREIGNKEY. |
MatchType | The match type (as defined in SQL-92). This field is ignored unless ConstraintType is DBCONSTRAINTTYPE_FOREIGNKEY. One of the following values: DBMATCHTYPE_NONE |
Deferrability | A bitmask that describes whether application or checking of the constraint is immediate or deferred. Values are as follows:
Not specifying DBDEFERRABILITY_DEFERRED implies that the constraint is immediate. Not specifying DBDEFERRABILITY_ DEFERRABLE implies that the constraint is not deferrable. If DBDEFERRABILITY_DEFERRABLE is not specified, the DBDEFERRABILITY_DEFERRED bit is ignored and the constraint is immediate. If the constraint is immediate, the constraint is applied or checked at the end of each SQL statement. If the constraint is deferred, the constraint is applied or checked when the constraint is changed to immediate, either explicitly by command execution or implicitly at the end of the current transaction. |
cReserved | The number of DBPROPSET structures in rgReserved. If this is zero, the provider ignores rgReserved. This parameter applies to constraint properties, and consumers should set this element to 0. |
rgReserved | An array of DBPROPSET structures containing properties and values to be set. If cReserved is zero, this argument is ignored. This parameter applies to constraint properties, and consumers should set this element to NULL. |
Return Code
cForeignKeyColumns was not zero, and rgForeignKeyColumnList was null.
cForeignKeyColumns was not zero and was not equal to cColumns.
cColumns was not zero, and rgColumnList was null.
cReserved was not zero, or rgReserved was not a null pointer.
ConstraintType was not DBCONSTRAINTTYPE_FOREIGNKEY, and pReferencedTableID was not a null pointer.
ConstraintType was DBCONSTRAINTTYPE_FOREIGNKEY, and pReferencedTableID was NULL.
The type of the constraint was not a check constraint, and pwszConstraintText was not NULL.
ConstraintType was DBCONSTRAINTTYPE_CHECK, and pwszConstraintText was a null pointer.
ConstraintType was DBCONSTRAINTTYPE_CHECK, and cColumns was not zero.
ConstraintType was DBCONSTRAINTTYPE_FOREIGNKEY, and cForeignKeyColumns was zero.
ConstraintType was DBCONSTRAINTTYPE_FOREIGNKEY, and the column type of a column in rgForeignKeyColumnList does not match the type of the corresponding column in rgColumnList.
ConstraintType was DBCONSTRAINTTYPE_FOREIGNKEY, DBCONSTRAINTTYPE_PRIMARYKEY, or DBCONSTRAINTTYPE_UNIQUE; and cColumns was zero.
ConstraintType was DBCONSTRAINTTYPE_PRIMARYKEY, and the base table already had a primary key.
The table specified by *pReferencedTableID in the DBCONSTRAINTDESC structure does not exist.
The constraint conflicts with current contents of the table.
Comments
If AddConstraint returns any errors, the constraint is not created.
If the session is participating in a transaction, if DBPROP_SUPPORTEDTXNDDL is DBPROPVAL_TC_DDL_IGNORE, and if the method succeeds, the operation is complete and is unaffected by subsequent calls to abort or commit the transaction.
If the session is participating in a transaction, if DBPROP_SUPPORTEDTXNDDL is DBPROPVAL_TC_DDL_COMMIT, and if the method succeeds, the transaction is committed without retention. No new transaction is created. Any new work done on the session is outside the scope of a transaction. Attempting to explicitly commit or abort when there is no outstanding transaction returns an error.
The following example illustrates which parameters should contain the base table and referenced table DBIDs when defining constraints in a foreign key relationship: Let Orders be the base table, and Customers the referenced table. The CustID field of Orders has a foreign key constraint on the CustID field of Customers. The rgColumnList parameter contains the DBID for Customers.CustID and rgForeignKeyColumnList contains the DBID for Orders.CustID.
ITableDefinitionWithConstraints::CreateTableWithConstraints | ITableDefinitionWithConstraints::DropConstraint
1998-2001 Microsoft Corporation. All rights reserved.