SQL Commands : CREATE

Create is a DDL command used to create a table or a database. It is used to create tables, databases, views, synonyms, sequences, functions, procedures, packages etc.

Creating a Database :

To create a database, below command is used :
CREATE DATABASE <database_name>;
Where the database_name is name of the database you want to create.

Example :
CREATE DATABASE myDB;
The above command will create a database named myDB.

Note : To select a database in MySQL console, use the following statement :
 use <database_name>;
Example :
 use myDB
Creating a Table :

To create a table, below command is used :
 CREATE TABLE table_name (
   column_name1 datatype,
   column_name2 datatype,
   column_name3 datatype,
   ..............................
   column_nameN datatype,
 PRIMARY KEY( one or more columns )
 );
Where the table_name is name of table, and inside the bracket we are defining the columns along with its data type.

Example :
 CREATE TABLE Employee (
    EmpId INT(5),
    EmpName VARCHAR(15),
    HireDate DATE,
    Salary INT(7),
    PRIMARY KEY(EmpId)
 );
The above statement creates a table named Employee with four columns : EmpId, EmpName, HireDate and Salay, with EmpId as primary key.

We can also check our newly created table by desc command.
 DESC Employee;
Output :



Next Topic :