Sub topic 2
Choosing data types and column defaults
This chapter covers how to pick a data type for each column in a CREATE TABLE statement and how to give columns default values. It assumes you can write basic SELECT, INSERT, UPDATE and DELETE statements and can run SQL against an Oracle Database 12c or later schema. Constraints and identity columns get their own chapters. Here they appear only where a default interacts with them.
Why the data type matters
A data type decides what values a column accepts, how much space they use, how they sort and compare, and which implicit conversions Oracle performs. A wrong choice rarely fails at creation time. It shows up later as rejected inserts, silent rounding, slow queries, or date arithmetic that is off by hours.
The usual approach is to start from what the value means, then pick the narrowest type that holds every legitimate value.
Character types
VARCHAR2(n) is the standard type for variable-length text. Oracle stores only the characters you supply and does not pad them.
The length n is measured in bytes or characters depending on the declaration:
VARCHAR2(100 BYTE)limits the value to 100 bytes.VARCHAR2(100 CHAR)limits the value to 100 characters, whatever their byte size.VARCHAR2(100)uses the session or database settingNLS_LENGTH_SEMANTICS, which isBYTEunless changed.
In a multibyte character set such as AL32UTF8, a name with accented letters can be longer in bytes than in characters. Writing CHAR explicitly avoids ORA-12899: value too large for column surprises. The maximum size is 4000 bytes, or 32767 bytes if the database runs with MAX_STRING_SIZE=EXTENDED.
CHAR(n) pads values with blanks to length n. Use it only for values that are truly fixed-width. Padding changes comparison behavior when you mix CHAR and VARCHAR2, so most designs use VARCHAR2 throughout.
CLOB holds text beyond the VARCHAR2 limit. Large objects are slower to query and cannot be used in many places, such as GROUP BY and ordinary indexes. Choose them only when the text really can be that long.
Oracle treats an empty string in a VARCHAR2 column as NULL. Inserting '' stores NULL, so a NOT NULL column rejects it.
Numeric types
NUMBER(p,s) is Oracle's exact decimal type. The precision p is the total number of significant digits (up to 38). The scale s is the number of digits to the right of the decimal point.
| Declaration | What it stores |
|---|---|
| `NUMBER(10)` | Integers up to 10 digits |
| `NUMBER(10,2)` | Up to 8 digits before the decimal point and 2 after |
| `NUMBER(5,-2)` | Values rounded to the nearest hundred |
| `NUMBER` | Any value, up to 38 significant digits, no fixed scale |
Oracle handles too many digits and too many decimals differently. Extra decimals are rounded silently. Extra integer digits raise an error.
CREATE TABLE number_demo (
amount NUMBER(5,2)
);
INSERT INTO number_demo (amount) VALUES (123.456); -- stored as 123.46
INSERT INTO number_demo (amount) VALUES (1000); -- ORA-01438: value larger than specified precision
SELECT amount FROM number_demo;Money values belong in NUMBER(p,2) or a scale that matches your currency. BINARY_FLOAT and BINARY_DOUBLE are IEEE binary floating-point types. They are fast and compact, but they cannot represent values like 0.1 exactly. Use them for measurements and scientific data, not for prices.
Oracle has no BOOLEAN column type in SQL before Oracle Database 23ai. On earlier versions, use NUMBER(1) or CHAR(1) with a CHECK constraint that restricts the allowed values. The next chapters on constraints show how.
Date and time types
| Type | Holds | Time zone |
|---|---|---|
| `DATE` | Date plus time to the second | None |
| `TIMESTAMP(p)` | Date plus fractional seconds (default precision 6) | None |
| `TIMESTAMP WITH TIME ZONE` | Timestamp plus the zone it was recorded in | Stored with the value |
| `TIMESTAMP WITH LOCAL TIME ZONE` | Timestamp normalized to the database time zone | Converted to the session zone on display |
| `INTERVAL DAY TO SECOND` | A duration in days, hours, minutes, seconds | Not applicable |
Despite its name, DATE always contains a time portion. A value inserted as DATE '2026-03-01' has a time of 00:00:00. A filter such as WHERE hire_date = DATE '2026-03-01' misses rows hired that day at 09:30. Compare a range instead, or truncate with TRUNC(hire_date).
Choose DATE for business dates where seconds are enough and zones are irrelevant. Choose a TIMESTAMP variant when you need sub-second precision or when users in several time zones read the same data.
Column defaults
A DEFAULT clause gives a column a value to use when an INSERT does not supply one. It goes after the data type and before any inline constraint.
CREATE TABLE product_demo (
sku VARCHAR2(20 CHAR) NOT NULL,
product_name VARCHAR2(100 CHAR) NOT NULL,
list_price NUMBER(10,2) DEFAULT 0 NOT NULL,
is_active NUMBER(1) DEFAULT 1 NOT NULL,
created_on DATE DEFAULT SYSDATE NOT NULL,
created_at TIMESTAMP WITH TIME ZONE DEFAULT SYSTIMESTAMP NOT NULL,
created_by VARCHAR2(128) DEFAULT USER NOT NULL,
notes CLOB
);
INSERT INTO product_demo (sku, product_name)
VALUES ('A-1', 'Widget');
SELECT sku, list_price, is_active, created_on, created_by
FROM product_demo;The insert names only two columns. Oracle fills in the other six NOT NULL columns from their defaults. notes has no default, so it becomes NULL.
A default can be:
- A literal such as
0or'ACTIVE'. - A date literal such as
DATE '2026-01-01'. - A function such as
SYSDATE,SYSTIMESTAMP,USERorSYS_GUID(). - An expression built from literals and functions, such as
SYSDATE + 30. - From Oracle 12c, a sequence reference such as
my_seq.NEXTVAL.
A default cannot refer to another column in the same row. It also cannot use a subquery or a non-deterministic function that Oracle does not allow, such as ROWNUM. Use DATE '...' or TO_DATE with an explicit format rather than a bare string, so the default does not depend on NLS_DATE_FORMAT.
When a default applies
The default is evaluated each time a row is inserted, not once when the table is created. DEFAULT SYSDATE therefore records the time of each insert.
A default applies in three cases:
- The column is left out of the
INSERTcolumn list. - The
INSERTsupplies the keywordDEFAULTas the value. - A statement such as
UPDATE ... SET column = DEFAULTresets the column.
A default does not apply when you supply NULL explicitly. This is a common source of errors:
INSERT INTO product_demo (sku, product_name, list_price)
VALUES ('A-2', 'Gadget', NULL);
-- ORA-01400: cannot insert NULL into ("PRODUCT_DEMO"."LIST_PRICE")
INSERT INTO product_demo (sku, product_name, list_price)
VALUES ('A-3', 'Gizmo', DEFAULT);
-- succeeds: list_price = 0Also, a default is not applied on a plain UPDATE. Changing a row never refills a column unless you set it to DEFAULT.
DEFAULT ON NULL
DEFAULT ON NULL (Oracle 12c and later) applies the default when the column is omitted and also when the statement supplies NULL. The column is implicitly NOT NULL.
CREATE TABLE task_demo (
task_id NUMBER(10) NOT NULL,
status VARCHAR2(10 CHAR) DEFAULT ON NULL 'OPEN'
);
INSERT INTO task_demo (task_id, status) VALUES (1, NULL); -- status = 'OPEN'
INSERT INTO task_demo (task_id) VALUES (2); -- status = 'OPEN'This is useful when an application layer or an ORM sends NULL for every field it did not populate. It also applies on UPDATE. Setting the column to NULL stores the default instead of failing or clearing the value. Keep that in mind if you rely on NULL to mean "unknown".
Changing a default later
Altering a default affects only future inserts. Existing rows keep the values they already have.
ALTER TABLE product_demo MODIFY (list_price DEFAULT 9.99);
ALTER TABLE product_demo MODIFY (list_price DEFAULT NULL); -- removes the defaultAdding a column with a default to a populated table gives every existing row the default value. In current Oracle releases, this is stored as dictionary metadata instead of rewriting every row, so it is fast even on large tables.
You can view defaults in USER_TAB_COLUMNS. The DATA_DEFAULT column is a LONG, so it displays well in a query tool but is awkward to filter on.
SELECT column_name, data_type, data_length, data_precision, data_scale,
nullable, data_default
FROM user_tab_columns
WHERE table_name = 'PRODUCT_DEMO'
ORDER BY column_id;Common mistakes
- Using
VARCHAR2(n)withoutCHARfor names in a multibyte database, then hittingORA-12899on long accented values. - Storing dates as
VARCHAR2text. That removes validation, breaks sorting and makes range queries slow. - Declaring
NUMBERwith no precision for keys and amounts. It works, but it documents nothing and accepts any scale. - Comparing a
DATEcolumn to a date literal and forgetting the time portion. - Expecting
DEFAULTto replace an explicitNULL. - Writing
DEFAULT 'ACTIVE'with a column too short to hold it, which fails at creation time or on insert.
Exercise
A table has the column status VARCHAR2(10) DEFAULT 'NEW' NOT NULL. What happens for each of these statements? (a) INSERT INTO t (id) VALUES (1); (b) INSERT INTO t (id, status) VALUES (2, NULL); (c) INSERT INTO t (id, status) VALUES (3, DEFAULT); And what single change to the column definition would make statement (b) store 'NEW'?
Show answer
(a) succeeds and stores 'NEW', because the column is omitted. (b) fails with ORA-01400, because an explicit NULL does not trigger the default and the column is NOT NULL. (c) succeeds and stores 'NEW', because the DEFAULT keyword requests the default. Changing the clause to DEFAULT ON NULL 'NEW' makes (b) store 'NEW'.
Exercise
A column is declared NUMBER(6,2). What happens when you insert 1234.567, and what happens when you insert 12345.6?
Show answer
1234.567 is rounded to 1234.57 and stored, because excess decimals are rounded to the scale. 12345.6 fails with ORA-01438, because it needs 5 digits before the decimal point and NUMBER(6,2) allows only 4.