Default values are preconfigured values which are inserted into table columns instead of NULL when there are no specific values provided during an INSERT operation.
CREATE TABLE telephonelist(
type VARCHAR(10) DEFAULT 'home',
alterationtime TIMESTAMP DEFAULT CURRENT_TIMESTAMP);
INSERT INTO telephonelist (name, phone_number) VALUES ('Meyer', '1234');
INSERT INTO telephonelist (name, phone_number) VALUES ('Müller','5678');
INSERT INTO telephonelist (name, type, phone_number) VALUES ('Meyer', 'work', '9999');
UPDATE telephonelist SET name='Meier', alterationtime=DEFAULT WHERE phone_number='1234';
SELECT * FROM telephonelist;
Statements that Manipulate Default Values
Default values can be created, altered, and deleted with the following statements:
- CREATE TABLE (in the column definition)
- ALTER TABLE (Column)
- ADD COLUMN (in the column definition)
- MODIFY COLUMN (in the column definition)
- ALTER COLUMN DEFAULT with SET DEFAULT or DROP DEFAULT
Default values are explicitly or implicitly used in tables with the following statements:
- INSERT: DEFAULT as a 'value' from a column (INSERT INTO t(i,j,k) VALUES (1, DEFAULT, 5)) or DEFAULT VALUES for all columns (INSERT INTO t DEFAULT VALUES) or implicitly at column selection (for the columns that were not selected)
- IMPORT: implicitly at column selection (for the columns that were not selected)
- UPDATE: DEFAULT as a 'value' for a column (SET i=DEFAULT)
- MERGE: DEFAULT as a 'value' for column in the INSERT and UPDATE parts or implicitly in the INSERT part at column selection (for the column that were not selected)
- ADD COLUMN: if a default value was specified for the inserted column with DEFAULT
Permitted Default Values
The following expressions can be used as default values:
- Constant values at evaluation time like CURRENT_USER or CURRENT_DATE
- Value expressions, which only contain the functions of the two aforementioned expressions
Note: Default values are limited to 2000 characters.
CREATE TABLE t (i DECIMAL, vc VARCHAR(100) );
ALTER TABLE t ALTER COLUMN i SET DEFAULT 1;
ALTER TABLE t ALTER COLUMN i SET DEFAULT 1+2;
ALTER TABLE t ALTER COLUMN vc SET DEFAULT 'abc'||TO_CHAR(CURRENT_DATE);
ALTER TABLE t ALTER COLUMN vc SET DEFAULT CURRENT_USER;
Display of Default Values
Default values can be displayed in the column COLUMN_DEFAULT of system tables EXA_ALL_COLUMNS, EXA_DBA_COLUMNS, and EXA_USER_COLUMNS.
Possible Sources of Error
In most cases, default values must work as expected. However, you should consider the following to ensure error-free operations:
- With the MODIFY COLUMN, as an existing default value is adopted when changing the data type. If the old default value is not appropriate for the new data type, then an error message is returned.
- If entries used as default values have different lengths, then it might not fit in the table column (for example, CURRENT_USER or CURRENT_SCHEMA). At the time of insertion, if the length of the default value is more than the specified value, then an error message is returned. The insertion will not be performed. In this case, the default value must be reduced to the length of the column using SUBSTR[ING].
- Interpretation of default values can depend on the format model (for example, a DATE value). In this case, it is advisable to explicitly enforce the format (for example, using TO_DATE with format specification).