How to Create a Table in SQL? | Simple Example

In this article, we will see how to create a table in SQL with examples.

Before we discuss on that let us see the importance of table in SQL.

What is a table and Why we need to create a table in SQL?

A table is a physical storage area to retrieve the rows and columns from the Database.

It is used to store transactional records in the database which contains collection of rows and columns.

The database tables have dependencies with other tables in the database which are called relationships. The table dependencies are correlated using primary key and foreign key relationships.

The primary key and foreign key relationships of tables can be specified using cardinality.

Cardinality specifies how many times the value of the primary key of an independent table is used as a foreign key in dependent table.

Well, there are 3 steps that you need to follow while creating a table in SQL.

  • Name the Table
  • Define the columns
  • Mention data types of the columns.

I have not specified any constraints in the create table statement because it is not necessary for table creation.

The above are the absolute things that you have to do but if you also want to apply some constraints in the table you can also do that too.

There are lots of things that you can do to create a statement. It is one of the most powerful statements in SQL.

Obviously right? The data is going to be store in the tables. You know creating tables is the sense of data management system.

Here is the syntax of the create table statement.

Create Table Syntax

Table creation in SQL table syntax

In this example, I have mentioned the primary key constraint at the last step or last line in the create table statement.

Now I am going to show you how to create a table using SQL editor.

I am going to use the test database for our current scenario.

I have given as use test in the SQL editor.

Now I am going to create a table in SQL called employee. Here I am going to specify 4 columns.

  1. ID
  2. Name
  3. DOB
  4. Email

All SQL keywords preferably use upper case because when you share your code with others it becomes easier to follow other people.

Table creation sample in SQL

After you complete the query press control enter combination.

Now I get the message in the output portation of the SQL editor window that the create table employee statement has been processed correctly and no rows have been affected.

Table Successfully Created message

Now you can go back and expand the database and table in the structure pane. There you could see the newly created table and its columns.

Check table description in SQL

Otherway you can check it using desc command. We have desc command in My SQL and Oracle. In SQL server I just you to use some alternative way.

Once you execute this you will get all the table columns and its datatypes and you also see the columns can have datatypes or not and you have any constrained applied for that column or not.

Here you can see the Id values integer data type and it cannot hold null values since it is a primary key and it does not have any default value because we have not specified it while creating a table.

Check table columns and syntax in SQL Table creation

Things to keep in mind

  • In a table, if you have one column or two-columns or n number of columns I can have only one primary key.
  • To terminate the SQL statement you should put the semicolon.
  • The number should be int data type and string should be varchar. Also, you must have to give a space between the column name and data type while creating a table in SQL.

I think this article table creation in sql is useful to you. In case you need more information you can also check this article SQL Create Table.

Related Queries

Here we have discussed the topics related to create table my sql, create table in sql with primary key, sql create table foreign key, sql create table from select, create table oracle.

Do you have any queries related to this article? or Would you like to share something about Create Table in SQL?

Please leave your comments in the comment section.

Leave a comment