Sub topic 1
Anatomy of a `CREATE TABLE` Statement
Prerequisites
This chapter assumes that you know basic SELECT, INSERT, UPDATE, and DELETE statements. You should also understand that a table contains rows, a row contains column values, and a key identifies rows or links related tables. The examples target Oracle Database 12c or later.
The statement’s overall structure
CREATE TABLE defines a new table. It specifies the table name, its columns, each column’s data type, optional default values, and rules that protect the data.
A basic statement has this shape:
CREATE TABLE table_name (
column_name data_type [column_options],
column_name data_type [column_options],
[table_constraints]
);The parentheses contain a comma-separated list. Every column definition needs a name and a data type. The final definition does not have a trailing comma before the closing parenthesis.
The table name and column names become database object names. Oracle normally stores unquoted names in uppercase, so projects, PROJECTS, and Projects refer to the same unquoted table name.
Defining columns
Choose a data type that matches the values a column should store. Common Oracle types include:
| Data type | Typical use | Example |
|---|---|---|
| `NUMBER(12,2)` | Numbers with up to 12 digits and 2 decimal places | `1250.50` |
| `VARCHAR2(100)` | Variable-length text | `'Database migration'` |
| `DATE` | Date and time values to the second | `SYSDATE` |
| `TIMESTAMP` | Date and time with fractional seconds | `SYSTIMESTAMP` |
The first number in NUMBER(12,2) is the precision, or maximum number of significant digits. The second is the scale, or number of digits after the decimal point.
VARCHAR2(100) permits up to 100 characters in the database character set. Do not use a large text type merely because it is convenient. A suitable limit documents the expected value and can prevent accidental oversized data.
A default supplies a value when an INSERT statement omits the column. It does not replace an explicitly supplied value.
CREATE TABLE projects (
project_id NUMBER(10),
project_name VARCHAR2(100),
start_date DATE DEFAULT SYSDATE,
budget NUMBER(12,2)
);In this example, Oracle evaluates SYSDATE when a row is inserted without a start_date. The default is an expression, not a value calculated when the table is created.
Adding rules to the definition
A column definition can include rules that apply directly to that column. A table-level constraint appears after the column definitions and can refer to one or more columns.
This example gives the identifier a primary key and prevents two rows from using the same project name:
CREATE TABLE projects (
project_id NUMBER(10) CONSTRAINT projects_pk PRIMARY KEY,
project_name VARCHAR2(100) NOT NULL,
start_date DATE DEFAULT SYSDATE NOT NULL,
budget NUMBER(12,2),
CONSTRAINT projects_name_uk UNIQUE (project_name),
CONSTRAINT projects_budget_ck CHECK (budget >= 0)
);The names after CONSTRAINT are explicit names. Naming rules makes error messages easier to interpret and makes later changes more precise. The primary key and other constraint types have different purposes; later chapters examine each one in detail.
Reading the definition from top to bottom
When reviewing a CREATE TABLE statement, check it in this order:
- Confirm that the table name describes the entity represented by each row.
- Check that every column has an appropriate data type and size.
- Decide which omitted values should receive defaults.
- Require values that the application cannot operate without.
- Add named constraints for uniqueness, valid ranges, and relationships.
- Confirm that referenced tables exist before creating foreign keys.
A table definition is part of the database’s contract. It should reject values that cannot represent valid business data, rather than relying only on application code.
Exercise
What value does Oracle use for START_DATE when an INSERT statement omits that column in the PROJECTS table?
Show answer
Oracle evaluates the DEFAULT SYSDATE expression at insert time and uses the resulting date and time.