Lesson 16 Intermediate Level
16 of 30 lessons

Creating Tables

Learn how to create tables with CREATE TABLE statement and define columns and constraints. Table creation is fundamental for database design.

Introduction

The CREATE TABLE statement is used to define a new table in Oracle SQL. You specify the table name, column names, data types, and optional constraints such as primary keys.

Syntax

CREATE TABLE table_name (
  column1 datatype constraint,
  column2 datatype constraint,
  ...
);

Example: Creating an Employees Table

CREATE TABLE employees (
  employee_id NUMBER PRIMARY KEY,
  first_name VARCHAR2(50),
  last_name VARCHAR2(50),
  hire_date DATE,
  salary NUMBER(8,2),
  department_id NUMBER
);

Common Data Types

  • VARCHAR2(n): Variable-length string
  • NUMBER(p,s): Numeric with precision and scale
  • DATE: Date and time
  • CLOB: Character Large Object

Optional Constraints

  • PRIMARY KEY: Uniquely identifies each record
  • NOT NULL: Ensures a column must have a value
  • UNIQUE: All values must be different
  • FOREIGN KEY: Links to another table’s column

Practice Task