SQL Create Table

SQL table is a place where the SQL database stores the data. It has columns and rows. We can use CREATE TABLE statement to create a table in a database.

SQL CREATE TABLE Syntax

CREATE TABLE table_name
(
    column1 data_type [null/not null],
    column2 data_type [null/not null],
    column3 data_type [null/not null],
    ....
)

data_type is required.
null/not null is optional, if it is not specified, the default value is null.

SQL CREATE TABLE Example

We want to create a table Users which will store the user’s information: user’s ID, first name, last name, E-mail address, phone number, birthday.

We can use the following script to create a table “Users” in database:

CREATE TABLE Users
(
    UserID int NOT NULL,
    FirstName varchar(100) NOT NULL,
    LastName varchar(100) NOT NULL,
    Email varchar(200),
    Phone varchar(50),
    Birthday Date    
)

The script will create an empty table which has 6 columns: UserID, FirstName, LastName, Email, Phone and Birthday.
The UserID, FirstName, LastName will not accept NULL value, when we insert or update a row in database, we must set the values for them. Email, Phone and Birthday can accept NULL value.

The new created empty table will look like:

UserID FirstName LastName Email Phone Birthday

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>