Learn how to use the SQL CREATE TABLE Statement with the explained examples given in this tutorial. To create a table using the SQL CREATE TABLE statement, you have to specify the table contents. These contents are the table rows and the columns.

By using the statement, you can specify the column names with the data types of the respected columns. After creating a table, you can start inserting rows of the table.

A row is a piece of data for each column. Let’s suppose you have created a table with the name Employee that contains the columns Name, Designation, Email, Address, and Mobile. You can put data for these column names and can put as many rows as you want.

How to Use SQL CREATE TABLE Statement

With the SQL CREATE statement, we can specify the column headers and the data types. Let’s see how to create a table using the statement with the syntax given below.

Syntax

Parameter of the Syntax

Sr.No. Parameter Name Description
1 tablename Specify the name of the table you want to create.
2 column1, column2…columnN Enter the column name you want to create for the table. You can create N number of column for the table.
3 data_type You must specify the data type for each column name. Specify after you enter the column name. Also put the size for the data of each column as the argument of each data types.
4 NULL | NOT NULL You should also define the column whether it is NULL or NOT NULL. If you do not specify this specify and left blank, the database automatically assign NULL as the default value

Examples to Use SQL CREATE TABLE Statement

Create a table for the employee of a company that contains the EmplyeeID, Name, Email, Mobile and the address. Specify the data type and the size of each column as given in the example below.

Explanation of Example1

  • The above example creates a table using SQL that contains 5 columns.
  • The first column is the ‘EmployeeID‘ which is created as integer data type and cannot be null. In addition to this is also a primary specified at the bottom of the example.
  • The other columns are not specified as NULL or NOT NULL. So, by default, they are treated as the null value. Each one except the integer contains the size of the column data as the argument of the data type.
The example creates a table for the customer of a company that contains the CustomerID, Name, Address, City, and Mobile. You can specify the data type and the size of each column of the table as given in the example below.

You must also learn.

Reference

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.