Sub topic 1
Creating a table with `CREATE TABLE`
CREATE TABLE defines a new table in a database. A table has columns, and each column describes the kind of value it stores. A table definition can also include rules that protect the data. This chapter focuses on the table structure and column definitions. Later chapters examine each constraint in detail.
Prerequisites
You should know that a table contains rows and columns. You should also be familiar with basic SQL statements such as SELECT and INSERT, and with common data types such as integers, text, and timestamps.
The basic structure
A CREATE TABLE statement names the table and lists its columns inside parentheses:
CREATE TABLE table_name (
column_name data_type,
another_column data_type
);Each column definition has at least a name and a data type. Separate column definitions with commas. The semicolon ends the statement.
For example:
CREATE TABLE customers (
customer_id INTEGER,
email VARCHAR(255),
full_name VARCHAR(100),
created_at TIMESTAMP
);This creates a table named customers with four columns. INTEGER stores whole numbers. VARCHAR(255) stores text up to 255 characters. TIMESTAMP stores a date and time.
Choosing column types
Choose a type that matches the values the column should contain. The type helps the database reject values that do not fit and can improve storage and query behavior.
| Data type | Typical values | Example use |
|---|---|---|
| `INTEGER` | Whole numbers | Quantity or identifier |
| `NUMERIC(10, 2)` | Exact decimal values | Money or measurements |
| `VARCHAR(100)` | Limited-length text | A person's name |
| `TEXT` | Longer text | A description |
| `DATE` | Calendar dates | Birth date |
| `TIMESTAMP` | Date and time | Creation time |
Use NUMERIC for values that require exact decimal precision, such as prices. Floating-point types can introduce rounding differences.
Adding rules to column definitions
A column definition can include rules immediately after its type. For example, NOT NULL requires a value, DEFAULT supplies a value when an insert omits the column, and PRIMARY KEY identifies each row uniquely.
CREATE TABLE customers (
customer_id INTEGER GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
email VARCHAR(255) NOT NULL UNIQUE,
full_name VARCHAR(100) NOT NULL,
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
);The identity column generates a new key value for each inserted row. The database checks the other rules whenever data is inserted or updated. Constraint details and database-specific identity syntax vary, so test the statement with your chosen database system.
Reading the definition as a design
Before writing SQL, ask what one row represents. In the example, one row represents one customer. Every column describes that customer. This prevents unrelated facts from being mixed into one table.
Exercise
Write a CREATE TABLE statement for a books table with an integer bookid, a required title limited to 200 characters, and an optional publicationdate.
Show answer
CREATE TABLE books ( bookid INTEGER, title VARCHAR(200) NOT NULL, publicationdate DATE );