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 stringNUMBER(p,s): Numeric with precision and scaleDATE: Date and timeCLOB: Character Large Object
Optional Constraints
PRIMARY KEY: Uniquely identifies each recordNOT NULL: Ensures a column must have a valueUNIQUE: All values must be differentFOREIGN KEY: Links to another table’s column
Practice Task
- Create a table named
departmentswith columns:dept_id,dept_name, andlocation.