Default Column Constraints
A default column constraint enforces a value to use when a row is inserted into a table and no value is provided for a column. The value can be a constant or the result of a SQL function, depending on the vendor.
NOTE: SourcePro DB currently does not support stored procedure creation with a default value for a parameter or fetching the default value of a stored procedure parameter.
Use the
defaultValue() method on
RWDBColumn to set or unset default values. This method takes an
RWDBExpr object containing the value. For example, to set a value:
RWDBExpr expr(10); //1
RWDBSchema schema;
RWDBColumn col;
... // set some other column attributes
col.defaultValue(expr); //2
schema.appendColumn(col);
myDbase.createTable("myTestTbl", schema);
Line
//1 creates an
RWDBExpr object with the value 10, and line
//2 sets the default value on the column.
To unset a default value, pass an empty
RWDBExpr, like so:
col.defaultValue(RWDBExpr());
Use hasDefault() to see if a default value has been set:
bool hasDefault = col.hasDefault();
For example, to fetch a column’s default value as defined in a schema fetched from the database:
RWDBTable table = db.table("myTable");
RWDBSchema sch = table.describe(RWDBTable::DefaultValues);
RWDBColumn column = sch["col1"];
if (column.hasDefault()) {
RWDBExpr defaultVal = column.defaultValue();
}