SQL Keyword - CREATE

CREATE Keyword

The create keyword is used to create tables, databases, views, procedures, indexes, functions, etc.

CREATE DATABASE

The Create Database keyword is used to create a new database in SQL.

Syntax

CREATE DATABASE <DATABASE_NAME>;

Example

CREATE DATABASE MyDB;

CREATE TABLE

The Create Table Keyword is used to create a new table in a database.

Syntax

CREATE TABLE <TABLE_NAME>
(
  <COLUMN_1> <DATATYPE>,
  <COLUMN_2> <DATATYPE>,
  .....
  <COLUMN_N> <DATATYPE>,
)

Example

CREATE TABLE Employee
(
  Emp_Id Integer,
  Emp_Name Varchar(20),
  Emp_Gender Varchar(5),
  Emp_Salary Integer
)

CREATE VIEW

The Create View keyword is used to create a new view.

A View is a virtual table based on the result of a select query.

Syntax

CREATE VIEW <VIEW_NAME> AS
<SELECT_QUERY>

Example

CREATE VIEW Emp_Male AS
SELECT * FROM Employee WHERE Emp_Gender = 'Male';

CREATE INDEX

The Create INDEX keyword is used to create an Index in a Table.

An index contains keys built from one or more columns in the table or view.

Syntax

CREATE INDEX <INDEX_NAME> ON <TABLE_NAME> (COLUMNS);

Example 1

CREATE INDEX idx_name ON Employee (Emp_Name);

Example 2

CREATE INDEX idx_name_Gender ON Employee (Emp_Name,Emp_Gender);

CREATE PROCEDURE

The Create Procedure is used to create a stored procedure in a database.

Stored Procedures are created to perform one or more DML operations on a database.

It is nothing but a group of SQL statements that accept some input in the form of parameters and perform some task that may or may not return a value.

Syntax

CREATE PROCEDURE <PROCEDURE_NAME>
AS
BEGIN
  <STATEMENTS>
END
GO;

Example

CREATE PROCEDURE SelectMaleEmp
AS
BEGIN
   SELECT * FROM Employee Where Emp_Gender = 'Male'
END
GO;

CREATE FUNCTION

The Create Function is used to create user-defined functions in a database.

A User Defined Function is a programming construct that accepts parameters, does actions, and returns the result of that action.

Syntax

CREATE FUNCTION  <FUNCTION_NAMR> (<PARAMETER>)  
RETURNS  <RETURN_TYPE>  
AS  
BEGIN  
    <STATEMENTS>
    RETURN <VALUE>
END 

Example

CREATE FUNCTION GetEMP(@Emp_Id Integer)  
 RETURNS VARCHAR(50)  
 AS  
BEGIN  
    RETURN (SELECT Emp_Name FROM Employee WHERE Emp_Id = @Emp_Id)  
END

Summary

The create keyword is used to create table, database, view, procedure, index, function, etc.

Next Recommended Reading Top Keyword in SQL