How to use SQL Create Statement to Create Database and Tables

In this post we will look at how to use the SQL create statement to create a database and tables in SQL. Before being able to create a table and add data, we first need to create a database.

SQL CREATE DATABASE Syntax

To create a database in SQL, we need to use the CREATE DATABASE command.

CREATE DATABASE dbname;

For example:

The following SQL statement creates a database called “ProductionDB”

CREATE DATABASE ProductionDB;

SHOW DATABASES

To see the database you just created and also see the list of databases in the system, we use the SHOW DATABASES command:

SHOW DATABASES;

SQL CREATE TABLE Syntax

Tables are the building blocks of relational databases. The database table is where all the data in a database is stored.

To create a table in SQL, we need to use the CREATE TABLE command.

CREATE TABLE table_name (
    column_name1 datatype,
    column_name2 datatype,
    column_name3 datatype,
   ....
);

column_names specify the name of the columns of the table.

The datatype specifies the type of the data the column can hold (e.g. integer, text, date, etc.).

The most common data types used are:

  • CHAR
  • VARCHAR
  • TEXT
  • DATE
  • TIME
  • TIMESTAMP

SQL CREATE TABLE Example

CREATE TABLE employees (
    EmployeeID int,
    FirstName varchar(255),
    LastName varchar(255),
    Department varchar(255),
    JoiningDate DATE
);

The above code creates an empty table called “employees” with five columns.

In the specified columns, EmployeeID can only hold integer values - FirstName, LastName and Department columns can hold up to 255 characters.

JoiningDate column holds Date type.