The CREATE TABLE statement is used to create a new table in a database.

In the previous chapter we have learned how to create a database on the database server. Now it's time to create some tables inside our database that will actually hold the data. A database table simply organizes the information into rows and columns.


CREATE TABLE table_name (
    column1 datatype,
    column2 datatype,
    column3 datatype,

The column parameters specify the names of the columns of the table.

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


The following example creates a table called "Persons" that contains five columns: PersonID, LastName, FirstName, Address, and City:


    PersonID int,
    LastName varchar(255),
    FirstName varchar(255),
    Address varchar(255),
    City varchar(255)

The PersonID column is of type int and will hold an integer.

The LastName, FirstName, Address, and City columns are of type varchar and will hold characters, and the maximum length for these fields is 255

Please check out the reference section SQL DB data types for the detailed information on all the data types available in popular RDBMS like MySQL, SQL Server, etc.

There are a few additional constraints (also called modifiers) that are set for the table columns in the preceding statement. Constraints define rules regarding the values allowed in columns.

  • The NOT NULL constraint ensures that the field cannot accept a NULL value.
  • The PRIMARY KEY constraint marks the corresponding field as the table's primary key.
  • The AUTO_INCREMENT attribute is a MySQL extension to standard SQL, which tells MySQL to automatically assign a value to this field if it is left unspecified, by incrementing the previous value by 1. Only available for numeric fields.
  • The UNIQUE constraint ensures that each row for a column must have a unique value.