SQL CREATE TABLE Statement

CREATE TABLE statement

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

SQL CREATE TABLE syntax

CREATE TABLE Table name
(
Column name 1 Data type,
Column name 2 Data type,
Column name 3 Data type,
....
)

The data type (data_type) specifies the type of data that a column can hold. The following table includes the most commonly used data types in SQL:

Data Type Data Type
  • Description
  • integer(size)
  • int(size)
  • smallint(size)
tinyint(size)
  • Can only contain integers. Specify the maximum number of digits within parentheses.
  • decimal(size,d)

numeric(size,d)

Can hold decimal numbers.

"size" specifies the maximum number of digits. "d" specifies the maximum number of digits to the right of the decimal point.

char(size)

Specify the length of a string within parentheses.

varchar(size)

Can hold variable-length strings (can contain letters, numbers, and special characters).

Specify the maximum length of a string within parentheses.

date(yyyymmdd) Can hold dates.

SQL CREATE TABLE Example

This example demonstrates how to create a table named "Person".

This table contains 5 columns, the column names are: "Id_P", "LastName", "FirstName", "Address", and "City":

CREATE TABLE Persons
(
Id_P int,
LastName varchar(255),
FirstName varchar(255),
Address varchar(255),
City varchar(255)
)

The data type of the Id_P column is int, which contains integers. The data types of the other 4 columns are varchar, with a maximum length of 255 characters.

An empty "Persons" table is similar to this:

Id_P LastName FirstName Address City
         

Data can be written into an empty table using the INSERT INTO statement.