PreviousSQL Engine Reference (v9 SP2 (9.5) revision 1) Next

DEFAULT

Chapter contents

You may use the DEFAULT keyword in the following contexts:

The default value (literal or expression) that you specify in a CREATE TABLE or ALTER TABLE statement must meet the following criteria:

Also, in INSERT and UPDATE statements, you do not have to specify values for columns that have a DEFAULT value defined. In such a case, Pervasive PSQL computes the DEFAULT expression and uses the result value as the value of the column.

Syntax

See the syntax for the following statements:

Remarks

You can specify expressions as DEFAULT values for columns of all data types as shown in the following table.

Table 3-4 Allowed DEFAULT Expression Data Types 
Relational Data Type
Allowed DEFAULT Expression Types (ODBC data types)
  • BFLOAT4, BFLOAT8
  • BIGINT, UBIGINT
  • BIT
  • CURRENCY
  • DECIMAL
  • DOUBLE
  • IDENTITY1
  • INTEGER, UINTEGER
  • NUMERIC
  • NUMERICSA
  • NUMERICSTS
  • REAL
  • SMALLIDENTITY1
  • SMALLINT, USMALLINT
  • TINYINT, UTINYINT
  • SQL_BIGINT
  • SQL_BIT
  • SQL_CHAR
  • SQL_DECIMAL
  • SQL_DOUBLE
  • SQL_FLOAT
  • SQL_INTEGER
  • SQL_LONGVARCHAR
  • SQL_NUMERIC
  • SQL_REAL
  • SQL_SMALLINT
  • SQL_TINYINT
  • SQL_VARCHAR
  • BINARY
  • LONGVARBINARY
  • SQL_BIGINT
  • SQL_BINARY
  • SQL_BIT
  • SQL_CHAR
  • SQL_GUID
  • SQL_INTEGER
  • SQL_LONGVARBINARY
  • SQL_LONGVARCHAR
  • SQL_SMALLINT
  • SQL_TINYINT
  • SQL_VARCHAR
  • CHAR
  • LONGVARCHAR
  • VARCHAR
  • SQL_BIGINT
  • SQL_BIT
  • SQL_CHAR
  • SQL_DATE
  • SQL_DECIMAL
  • SQL_DOUBLE
  • SQL_FLOAT
  • SQL_GUID
  • SQL_INTEGER
  • SQL_LONGVARCHAR
  • SQL_NUMERIC
  • SQL_REAL
  • SQL_SMALLINT
  • SQL_TIME
  • SQL_TIMESTAMP
  • SQL_TINYINT
  • SQL_VARCHAR
UNIQUEIDENTIFIER
  • SQL_CHAR
  • SQL_LONGVARCHAR
  • SQL_VARCHAR
  • SQL_GUID
DATE
  • SQL_CHAR
  • SQL_DATE
  • SQL_LONGVARCHAR
  • SQL_TIMESTAMP
  • SQL_VARCHAR
If the data type of expression is TIMESTAMP, only the date part is extracted and used.
TIME
  • SQL_CHAR
  • SQL_LONGVARCHAR
  • SQL_TIME
  • SQL_TIMESTAMP
  • SQL_VARCHAR
If the data type of expression is TIMESTAMP, only the time part is extracted and used.
TIMESTAMP
  • SQL_CHAR
  • SQL_DATE
  • SQL_LONGVARCHAR
  • SQL_TIMESTAMP
  • SQL_VARCHAR
If the data type of expression is DATE, the time part is set to '00:00:00'.
1 In a CREATE TABLE statement or an ALTER TABLE statement, you may set a default value of zero (default 0 or default '0') for an IDENTITY or a SMALLIDENTITY data type. No other default value is permissible.

Examples

The following statement creates a table named Tab1. The default value of the col1 column is the DATE part of timestamp returned by NOW ().

CREATE TABLE Tab1 
( 
    col1 DATE DEFAULT NOW() 
) 


The following statement creates a table named Tab5. The default value of the col5 column is 200.

CREATE TABLE Tab5 
( 
    col5 INT DEFAULT 200 
) 


The following statement creates a table named Tab8. The default value of the col8 column is the timestamp at the time of the INSERT or UPDATE.

CREATE TABLE Tab8 
( 
    col8 TIMESTAMP DEFAULT CURRENT_TIMESTAMP 
) 


The following statement creates a table named Tab6. The default value of the col6 column is the name of user, after executing an INSERT or UPDATE statement. Using a default user name is practical only when security is enabled. Otherwise, USER is always NULL.

CREATE TABLE Tab6 
( 
    col6 VARCHAR(20) DEFAULT USER 
) 


The following statement shows an invalid example. The statement results in a parse-time error because SQL_TIME is not an allowed data type for a DATE column.

CREATE TABLE Tab 
( 
    col DATE DEFAULT CURTIME() 
) 


The following statement shows an invalid example. The statement results in a parse-time error. Although '3.1' is convertible to a number, it is not a valid integer.

CREATE TABLE Tab 
( 
    col SMALLINT DEFAULT '3.1' 
) 


The following statement shows an invalid example. The CREATE TABLE statement succeeds but the INSERT statement fails because -60000 is outside of the range supported by SMALLINT.

CREATE TABLE Tab 
( 
    col SMALLINT DEFAULT 3 * -20000 
) 
INSERT INTO Tab values(DEFAULT) 


The following statements show valid examples of setting a default value of zero for an IDENTITY and a SMALLIDENTITY data type.

CREAT TABLE t1 ( c1 IDENTITY DEFAULT '0' ) 
ALTER TABLE t1 ALTER c1 SMALLIDENTITY DEFAULT 0 


The following statements show invalid examples of setting a default value for an IDENTITY and a SMALLIDENTITY data type.

CREATE TABLE t1 ( c1 IDENTITY DEFAULT 3 ) 
ALTER TABLE t1 ALTER c1 SMALLIDENTITY DEFAULT 1 

Expressions as Default Values for Date or Time Columns

Defaults specify the values that are used in a column if you do not specify a value for the column when inserting a row.

To assign an expression as a default on a column, define a default value using the DEFAULT keyword in the CREATE TABLE statement. For more details on creating a table, see CREATE TABLE .

In addition to literals and NULL values, Pervasive PSQL also allows you to specify date and time scalar functions and expressions as default values for columns of the appropriate data types.

CURRENT_USER() is allowed as a default value for string columns CHAR and VARCHAR.

The following table lists the specific date and time scalar functions that you can invoke to provide the default value for a column of the appropriate data type:

Table 3-5 Date and Time Scalar Functions as Default Values
Function
Default for
CURRENT_DATE( )
Date columns only
CURDATE( )
Date columns only
CURRENT_TIME( )
Time columns only
CURTIME( )
Time columns only
CURRENT_TIMESTAMP( )
Timestamp or date-time columns
NOW( )
Timestamp or date-time columns

When you specify a scalar function as the default value for a column in INSERT and UPDATE statements, Pervasive PSQL invokes the specified scalar function to set the value of such a column to the result of the function call.

See Also

ALTER TABLE

CREATE TABLE

INSERT

UPDATE


Chapter contents
Book contents

Prev topic: DECLARE CURSOR
Next topic: DELETE (positioned)