As we learned in the previous section, columns not specified
in an INSERT statement are given NULL
values. You can change this assignment by using the DEFAULT keyword.
When creating a table, the keyword DEFAULT and a value can be used
next to each column type. The value will then be used anytime the column value
is not supplied in an INSERT. If no DEFAULT is defined, a
NULL is used for the column. Figure
shows a typical use of default values. The default for the timestamp
column is actually a call to an internal POSTGRESQL variable that returns
the current date and time.
test=> CREATE TABLE account (
test(> name CHAR(20),
test(> balance NUMERIC(16,2) DEFAULT 0,
test(> active CHAR(1) DEFAULT 'Y',
test(> created TIMESTAMP DEFAULT CURRENT_TIMESTAMP
test(> );
CREATE
test=> INSERT INTO account (name)
test-> VALUES ('Federated Builders');
INSERT 19103 1
test=> SELECT * FROM account;
name | balance | active | created
----------------------+---------+--------+------------------------
Federated Builders | 0.00 | Y | 1998-05-30 21:37:48-04
(1 row)