Oracle CREATE TABLE Statement

Summary: In this tutorial, you will learn how to use the Oracle CREATE TABLE statement to create a new table in the Oracle database.

Introduction to Oracle CREATE TABLE statement #

In Oracle, a table is a structured collection of related data organized in rows and columns, like a spreadsheet.

  • Each row represents a record in the table.
  • Each column represents an attribute of the data.

So far, you’ve learned how to work with various tables in the Oracle sample database, such as orders and order_items.

To create a new table in Oracle Database, you use the CREATE TABLE statement.

Here’s the basic syntax of the CREATE TABLE statement:

CREATE TABLE table_name (
    column_1 datatype [constraint],
    column_2 datatype [constraint],
    ...
    table_constraint
 );Code language: SQL (Structured Query Language) (sql)

In this syntax:

  • First, specify the name of the table you want to create after the CREATE TABLE keyword.
  • Second, provide one or more columns. Each column includes the name, data type, and constraint.
  • Third, define table constraints like primary key, foreign key, check, and unique.

A column may have one of the following data types:

  • NUMBER – The column may store numbers like integers and decimals.
  • VARCHAR2 – The column can store text of variable length.
  • DATE – The column may store date and time. Note that in Oracle Database, the DATE data type includes both date and time.
  • And many others.

Additionally, a column may have rules for validating data values, such as:

  • NOT NULL – The column cannot have NULL.
  • UNIQUE – The column cannot have duplicate data.
  • DEFAULT – The column may accept a default value if you don’t explicitly specify it. For example, created_at column defaults to the time when you insert a row into the table.
  • CHECK – The data in the column must satisfy a Boolean condition.

Typically, a table has a primary key that consists of one or more columns. These columns are called primary key columns.

The primary key uniquely identifies each row within the same table. To define a column as a primary key, you use the PRIMARY KEY constraint:

column_name NUMBER PRIMARY KEYCode language: SQL (Structured Query Language) (sql)

A table has one and only one primary key. You’ll learn about the primary key in the following tutorial.

Unlike primary keys, a table can have multiple foreign keys, which we will cover in the foreign key tutorial.

Note that you must have the CREATE TABLE system privilege to create a new table in your schema and CREATE ANY TABLE system privilege to create a new table in another user’s schema. Additionally, the owner of the new table must have the quota for the tablespace that contains the new table or UNLIMITED TABLESPACE system privilege.

Oracle CREATE TABLE statement example #

First, connect to the Oracle Database server using SQL*Plus or SQL Developer.

Second, execute the following statement to create a new table:

CREATE TABLE invoices (
   invoice_no NUMBER PRIMARY KEY,
   invoice_date DATE NOT NULL,
   customer_id NUMBER NOT NULL,
   note VARCHAR2(255)
);Code language: SQL (Structured Query Language) (sql)

This CREATE TABLE statement creates a new table called invoices with four columns:

  • invoice_no column has the type NUMBER, meaning that it can store only numbers and cannot store values of other types. The invoice_no column is the primary key denoted by the PRIMARY KEYconstraint. It means the invoice_no column will not have duplicate values.
  • invoice_date column has the type DATE so it can store date and time. Additionally, the NOT NULL constraint ensures the column does not have NULL.
  • customer_id column has the type NUMBER so it can store numbers only. It also has a NOT NULL constraint. The customer_id should reference the customer_id in the customers table via a foreign key.
  • note column has the VARCHAR2 type that can store a text string with a maximum length of 255. Unlike other columns in the invoices table, the note column does have NOT NULL constraint so it can store NULL.

IF NOT EXISTS option #

You’ll get an error if you attempt to create a new table with the existing name.

For example, if you attempt to create the invoices table again, Oracle Database will issue the following error:

ORA-00955: name is already used by an existing objectCode language: SQL (Structured Query Language) (sql)

To avoid the error, starting in Oracle 23, you can use the IF NOT EXISTS clause for the CREATE TABLE statement.

CREATE TABLE IF NOT EXISTS table_name (
    column_1 datatype [constraint],
    column_2 datatype [constraint],
    ...
    table_constraint
 );Code language: SQL (Structured Query Language) (sql)

The IF NOT EXISTS option creates a new table only if it does not exist; otherwise, it will not do anything.

For example, you can execute the CREATE TABLE statement that creates the invoices table as many times as you want , with the IF NOT EXISTS option:

CREATE TABLE IF NOT EXISTS invoices (
   invoice_no NUMBER PRIMARY KEY,
   invoice_date DATE NOT NULL,
   customer_id NUMBER NOT NULL,
   note VARCHAR2(255)
);Code language: SQL (Structured Query Language) (sql)

Summary #

  • Use the CREATE TABLE statement to create a new table.
  • Use the IF NOT EXISTS clause to create a new table only if it does not exist.

Quiz #

Was this tutorial helpful?