This Oracle tutorial explains how to use the Oracle CREATE TABLE statement with syntax, examples, and practice exercises.
Description
The Oracle CREATE TABLE statement allows you to create and define a table.
Syntax
The syntax for the CREATE TABLE statement in Oracle/PLSQL is:
Parameters or Arguments
table_name
The name of the table that you wish to create.
column1, column2, ... column_n
The columns that you wish to create in the table. Each column must have a datatype. The column should either be defined as "null" or "not null" and if this value is left blank, the database assumes "null" as the default.
Example
Let's look at an Oracle CREATE TABLE example.
This Oracle CREATE TABLE example creates a table called customers which has 3 columns.
The first column is called customer_id which is created as a number datatype (maximum 10 digits in length) and can not contain null values.
The second column is called customer_name which is a varchar2 datatype (50 maximum characters in length) and also can not contain null values.
The third column is called city which is a varchar2 datatype but can contain null values.
Now the only problem with this Oracle CREATE TABLE statement is that you have not defined a primary key for the table. We could modify this CREATE TABLE statement and define the customer_id as the primary key as follows:
Create an Oracle table called suppliers that stores supplier ID, name, and address information.
Solution for Practice Exercise #1:
The Oracle CREATE TABLE statement for the suppliers table is:
Practice Exercise #2:
Create an Oracle table called customers that stores customer ID, name, and address information.
But this time, the customer ID should be the primary key for the table.
Solution for Practice Exercise #2:
The Oracle CREATE TABLE statement for the customers table is:
Practice Exercise #3:
Based on the departments table below, create an Oracle table called employees that stores employee number, employee name, department, and salary information. The primary key for the employees table should be the employee number. Create a foreign key on the employees table that references the departments table based on the department_id field.
Solution for Practice Exercise #3:
The Oracle CREATE TABLE statement for the employees table is: