SQL Create Table – Creating a Table in SQL

If you want to store your data in a database then you will have to create SQL Table with a unique name. t has a simple structure that contains rows & columns. The name of Each column must be unique with a data type.

In this tutorial, You will get complete information with syntax & examples. After reading this tutorial, you will easily create different types of Tables. So, This tutorial will be very useful to use its concept in the project.

Read also –

SQL Interview Question with Answers for 2020

Insert Data into Database Using MySQL & PHP

How to Create a Table in SQL

Before Creating a SQL table, You must do the following required configuration.

  • Install SQL Server
  • Create a Database

If you are working with PHP, You can configure the above requirements in PHPMyAdmin.

Now, You can create a SQL table using the following Points –

1. Guideline for Creating Table

You must follow the given guidelines below –

  • Table Name must start with an uppercase or lowercase alphabet.
  • Table Name does not accept blank space, Single or Double Quotes.
  • Reserve words of RDBMS/DBMS does not declare as table Name
  • The table must have a unique column name with proper size & datatype.
  • Table Name & column name with more than one word should not be separated by white spaces or hyphen. But It should be separated by an underscore.

2. CREATE TABLE Statement

Create Table statement has the following syntaxes. These syntaxes contain four columns. But you can declare more columns according to your requirement.

Syntax – 1

This syntax has table columns without constraints –

CREATE TABLE table_name
(
column_name1 data_type(size),
column_name2 data_type(size),
column_name2 data_type(size),
column_name2 data_type(size),
);

Syntax – 2

This syntax has table columns with constraints –

CREATE TABLE table_name
(
column_name1 data_type(size)[constraints],
column_name2 data_type(size)[constraints],
column_name2 data_type(size)[constraints],
column_name2 data_type(size)[constraints],
);

 

3. Example of Creating Table.

Suppose that you have to create a table users with the column as first_name, last_name, email, mobile_number & about. you can create it using the following query. Here two examples are available, you can use one of them.

Example – 1

This example has table columns without constraints –

CREATE TABLE users 
( 
 first_name varchar(50), 
 last_name varchar(50), 
 email varchar(50), 
 mobile_number int(20), 
 about text(500) 
);

 

Example – 2

This example has table columns with constraints –

CREATE TABLE users
(
 id int(10) PRIMARY KEY NOT NULL AUTO_INCREMENT,
 first_name varchar(50) DEFAULT NULL,
 last_name varchar(50) DEFAULT NULL,
 email varchar(50) DEFAULT NULL,
 mobile_number int(20) DEFAULT NULL,
 about text(500) DEFAULT NULL
);

 

4. Creating a Table with Command

Run the command with the following points¬† ¬†–

For New Table –

  • First of all, create a new database.
  • Use the created database.
  • Create a new table.
  • Show the created table.
mysql> CREATE DATABASE my_db;
mysql> USE my_db;
mysql> CREATE TABLE users
     (
      first_name varchar(50),
      last_name varchar(50),
      email varchar(50),
      mobile_number int(20),
      about text(500)
     );
mysql> DESC users

For Existing Table –

If you have already created a database & table then you should run only the following command

mysql>USE my_db; 
mysql> DESC users

 

Create a Table with Another Table

You can also create a new table from the existing table. It has the same guideline as you have read in the previous step. But you will have to create it using the following query –

CREATE TABLE employees AS
SELECT first_name, last_name, email
FROM users;

 

Tutorial Summary

I have given complete information about the SQL CREATE Table statement with an example. Now you are able to use this information for creating different types of tables like student table, employee table, login table, registration, table, products table so on.

If you have any questions related to SQL, Kindly ask me through the comment box. Even you can suggest sharing another web development tutorial.

Thanks for giving time to this tutorial. Keep visiting my blog to learn more & become an expert in the coding field.

Categories SQL

Leave a Comment