SQL CREATE TABLE Statement (original) (raw)

Skip to content

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

Introduction to SQL CREATE TABLE statement #

In relational databases, a table is a structured set of data organized into rows and columns:

To create a new table, you use the CREATE TABLE statement. Here’s the basic syntax of the CREATE TABLE statement

CREATE TABLE table_name ( column1 datatype constraint, column2 datatype constraint, ... );Code language: SQL (Structured Query Language) (sql)

In this syntax:

The following example uses the CREATE TABLE statement to create a new table called courses that stores the course name, description, and duration:

CREATE TABLE courses ( name VARCHAR(255) NOT NULL, description TEXT, duration DEC(4, 2) NOT NULL );Code language: SQL (Structured Query Language) (sql)

Try it

The courses table has three columns:

IF NOT EXISTS option #

The database system will issue an error if you attempt to create a table that already exists. To avoid the error, you can use the IF EXISTS option in the CREATE TABLE statement:

CREATE TABLE IF NOT EXIST table_name ( column1 datatype constraint, column2 datatype constraint, ... );Code language: SQL (Structured Query Language) (sql)

The CREATE TABLE statement with the IF NOT EXISTS option creates a table only when the table does not exist.

If the table already exists, the database system may issue a warning or notice and won’t do anything else.

Summary #

Databases #

Quiz #

Was this tutorial helpful ?