SQL Create Table - The Coding Shala

Home >> Learn SQL >> SQL Create Table

SQL CREATE Table Statement

SQL CREATE TABLE statement is used to create new tables in the database. CREATE DATABASE statement is used to create the database. The important thing here is SQL keywords are not case sensitive means create is the same as CREATE. Below is the SQL CREATE TABLE statements and its examples are given.
SQL CREATE Table - The Coding Shala

CREATE DATABASE Syntax

The following is the statement is used to CREATE DATABASE -  
 CREATE DATABASE database_name;  


SQL CREATE DATABASE Example

 CREATE DATABASE mydb;  
  • This query will create a new database in SQL and name the database as mydb.

CREATE TABLE Syntax

The following CREATE TABLE statement is used to create tables in the database - 
 CREATE TABLE table_name  
        (        
         column1 datatype(size),  
         column2 datatype(size),  
         column3 datatype(size),  
         ..  
         ..  
         );  



SQL CREATE TABLE Example

 CREATE TABLE Persons  
 (  
 PersonID int,  
 LastName varchar(255),  
 FirstName varchar(255),  
 Address varchar(255),  
 City varchar(255)  
 );  
  • This query will create a table in the database named Persons.
  • By using the 'DESC' command we can verify if the table is created or not.
Syntax
 DESC table_name;  

Example
 > desc Persons;  
       Field         Type         Null       Key       Default     Extra  
 1     PersonID      int(11)         YES          NULL       
 2     LastName     varchar(255)     YES          NULL       
 3     FirstName     varchar(255)     YES          NULL       
 4     Address         varchar(255)     YES          NULL       
 5     City             varchar(255)     YES          NULL  




Other Posts You May Like
Prev<< SQL Introduction                                                                 NEXT >>SQL DROP Table
Please leave a comment below if you like this post or found some error, it will help me to improve my content.

Comments

Popular Posts from this Blog

Shell Script to find sum, product and average of given numbers - The Coding Shala

Shell Script to Create a Simple Calculator - The Coding Shala

Add two numbers in Scala - The Coding Shala

LeetCode - Number of Good Pairs Solution - The Coding Shala

Richest Customer Wealth LeetCode Solution - The Coding Shala