Sub topic 2
Choosing Data Types and Column Defaults
This chapter assumes you know basic SELECT, INSERT, UPDATE, and DELETE statements. You should also understand that a table contains rows and columns, and that a column can store only values compatible with its declared type.
Choose a type from the data’s meaning
A column’s data type controls which values Oracle accepts and how it stores them. Choose the narrowest type that represents the data without losing useful information.
| Data type | Suitable values | Example |
|---|---|---|
| `NUMBER(p,s)` | Numbers with optional precision and scale | `NUMBER(10,2)` for money |
| `VARCHAR2(n CHAR)` | Variable-length text | `VARCHAR2(100 CHAR)` |
| `CHAR(n CHAR)` | Fixed-length text | `CHAR(2 CHAR)` for a code |
| `DATE` | Date and time to whole seconds | `DATE` |
| `TIMESTAMP` | Date and time with fractional seconds | `TIMESTAMP` |
| `CLOB` | Large text | `CLOB` |
NUMBER(10,2) allows up to 10 total digits, with 2 digits after the decimal point. It can store values such as 12345678.90.
Use VARCHAR2 for most text. The CHAR length syntax shown above counts characters instead of bytes, which is usually clearer when a database may store multibyte characters. Use DATE when seconds are enough. Use TIMESTAMP when fractional seconds matter.
Do not use a text column for dates or numbers merely because incoming data arrives as text. Native types support comparisons, sorting, arithmetic, and date functions without repeated conversion.
Define useful defaults
A column default supplies a value when an INSERT statement omits that column. The default does not normally replace an explicitly supplied NULL.
The following table uses literal and calculated defaults:
CREATE TABLE task_notes (
note_id NUMBER(10),
title VARCHAR2(100 CHAR),
priority NUMBER(2) DEFAULT 3,
status VARCHAR2(12 CHAR) DEFAULT 'OPEN',
created_at TIMESTAMP DEFAULT SYSTIMESTAMP,
review_date DATE DEFAULT SYSDATE + 7,
details CLOB
);When an insert omits priority, Oracle stores 3. When it omits created_at, Oracle evaluates SYSTIMESTAMP for that row. A date expression such as SYSDATE + 7 means seven days after the current database date and time.
INSERT INTO task_notes (note_id, title, details)
VALUES (1, 'Check invoice', 'Confirm the supplier total');
INSERT INTO task_notes (note_id, title, status)
VALUES (2, 'Archive report', NULL);
SELECT note_id, title, priority, status, created_at, review_date
FROM task_notes
ORDER BY note_id;The first row receives the defaults for priority, status, created_at, and review_date. The second row explicitly supplies NULL for status, so its status is NULL, not 'OPEN'.
On Oracle Database 12c and later, DEFAULT ON NULL also applies the default when an insert explicitly supplies NULL:
CREATE TABLE service_requests (
request_id NUMBER(10),
status VARCHAR2(12 CHAR) DEFAULT ON NULL 'NEW',
opened_at TIMESTAMP DEFAULT ON NULL SYSTIMESTAMP
);Use this form when the application must never turn an omitted value or an explicit NULL into a missing status or timestamp. If the column must always contain a value, combine this design with a NOT NULL constraint.
How defaults are applied
Exercise
What value does status receive in an insert that omits the column, and what value does it receive when the insert supplies NULL for a column defined as DEFAULT 'OPEN'?
Show answer
An omitted column receives 'OPEN'. An explicitly supplied NULL remains NULL because an ordinary DEFAULT does not replace explicit NULL.