×

DDL Commands in SQL

DDL stands for Data Definition Language.

Data Definition Language is a bunch of SQL commands used to create, modify and delete Database schema but not the records or data. Data Definition Language includes Structured Query Language Commands used to define the Database structure. The Data Definition Language commands in Structured Query Language define the schema and modify the Database's schema and its objects as well.

List of Data Definition Language commands in Structured Query Language are:

  1. CREATE Command.
  2. ALTER Command.
  3. TRUNCATE Command.
  4. DROP Command.

Let’s understand each Data Definition Language commands.

1. CREATE Command

In SQL, CREATE command in Data Definition Language is used to create Database and Database objects like to create Table, Index, Views, Triggers, Stored Procedure, and other Database Objects.

Syntax of CREATE Command in Structured Query Language:

CREATE OBJECT OBJECT_NAME; 

Here Object can be Table, Index, View, Trigger, or any other Database Object.

Examples of CREATE Command

Example 1: This example helps us how to create a database using the CREATE Command of the Data Definition Language in SQL:

Syntax to Create a DB (Database):

CREATE DATABASE DATABASE_NAME; 

Now, we will create one Database, and the Database name is School in the SQL Database. To Create a School Database, we will write the following Data Definition Language Command:

CREATE DATABASE SCHOOL;

We defiined the above query to create a database, but how did we know it created a database. To crosscheck whether the Database is created, we will use the SHOW keyword, which will display the entire Database present in the system.

The output of the above query is as follows:

SHOW Databases;
Database
Company
demo
e
information_schema
Mysql
performance_schema
Phpmyadmin
ranking
School
Student

Output 

DDL Commands in SQL

Now, we will create a table inside this School Database. For this, we will use the following command:

USE SCHOOL;

Now, whatever Object we create, modify or delete will be inside this school Database.

Example 2:  This example helps us how to create a table using the CREATE Command of the Data Definition Language in SQL:

Syntax to create Table:

CREATE TABLE TABLE_NAME (
COLUMN NAME1 DATA_TYPE (COLUMN SIZE),
COLUMN NAME2 DATA_TYPE (COLUMN SIZE),
COLUMN NAME N DATA_TYPE (COLUMN SIZE)
);

Now, we will create one Table, and the Table name is Stud in the SQL Database. To Create Stud Table with some columns, we will write the following Data Definition Language Command:

CREATE TABLE STUD (
STUD_ID INT,
STUD_NAME VARCHAR (30),
STUD_AGE INT,
STUD_CITY VARCHAR (20),
STUD_MARKS INT
);

To crosscheck whether Table is created or not, use DESC table - name

DESC STUD;

2. ALTER Command

Another Data Definition Language command is Alter Command in the Structured Query Language. Alter command is used to add the column, delete the column, or modify a column in an existing table. Alter command is also used to add or drop constraints in an existing table.

Examples of Alter Command:

Example 1: This example helps us with how to add a column to the existing Table:

Syntax to add column in the existing Table:

ALTER TABLE TABE_NAME ADD COLUMN_NAME DATA_TYPE (COLUMN SIZE);

Now we will add the last name column to the existing stud table. To add a column in an existing table, we will write the following Data Definition Language Command:

ALTER TABLE STUD ADD STUD_LAST_NAME VARCHAR(20);

Use the DESC STUD command to check whether the query is executed properly or not

FieldTypeNullKeyDefaultExtra
STUD_IDint(11)YES NULL 
STUD_NAMEvarchar(30)YES NULL 
STUD_AGEint(11)YES NULL 
STUD_CITYvarchar(20)YES NULL 
STUD_MARKSint(11)YES NULL 
STUD_LAST_NAMEvarchar(20)YES NULL 

Output

DDL Commands in SQL

Example 2: These examples help how to drop the existing field from the Table.

Syntax to drop a column from the Table:

ALTER TABLE TABLE_NAME DROP COLUMN_NAME;

Now we will drop the last name column from the existing stud table. To drop a column from the existing Table, we will write the following Data Definition Language Command:

ALTER TABLE STUD DROP STUD_LAST_NAME;

Use the DESC STUD command to check whether the query is executed properly or not

FieldTypeNullKeyDefaultExtra
STUD_IDint(11)YES NULL 
STUD_NAMEvarchar(30)YES NULL 
STUD_AGEint(11)YES NULL 
STUD_CITYvarchar(20)YES NULL 
STUD_MARKSint(11)YES NULL 
DDL Commands in SQL

3. TRUNCATE Command

The TRUNCATE command is used to delete or remove all the records from the table. These commands only remove data, but the table's structure still exists. And these commands can be rollback after executing the command.

Syntax of TRUNCATE Command:

TRUNCATE TABLE TABLE_NAME;

Example: We will remove all the records from the Stud table, and there is some dummy or temporary data inserted into the stud table.

Table Name: Stud.

STUD_IDSTUD_NAMESTUD_AGESTUD_CITYSTUD_MARKS
1Manan16Dhule85
2Monika15Dhule80
3Chirag16Chalisgaon95

We will remove all the records of the Stud table using the below query:

TRUNCATE TABLE STUD;  

This query will delete the records; the structure remains to exist.

After executing the above query, execute a SELECT query to check whether data is deleted or not.

SELECT * FROM STUD;

The output shows the empty table.

4. DROP Commands: 

DROP Commands are Data Definition Language Commands used to drop the Database and the Database objects from the Structured Query Language. Using the DROP command, we can easily drop view, Index, and table from the Database.

Example of DROP Commands in Structured Query Language:

Example 1: These examples help how to drop existing Index from the Structured Query Language Database:

Syntax to drop an Index:

DROP INDEX NAME_OF_INDEX;

We want to drop the Stud_Index from the Structured Query Language Database. For this, we will execute the following query:

DROP INDEX STUD_INDEX;

Example 2: These examples help how to drop existing tables from the Structured Query Language Database:

Syntax to drop a table:

DROP TABLE TABLE_NAME

We will drop the Stud table and delete all the table structure records in this example.

DROP TABLE STUD;

Example 3: These examples help how to drop existing Database from the Structured Query Language Database:

Syntax to drop a Database:

DROP TABLE DATABASE_NAME; 

In this example, we will drop School Database.

DROP DATABASE SCHOOL;

Related Topics

SQL Except

In SQL, we probably use the JOIN clause to receive the combined result from one or more than one table. But sometimes, we want a result that contains data from...

4 minutes read.

SQL WHERE Multiple Conditions

In this topic, we will learn how to add multiple conditions using the WHERE clause. First, let's understand the concept of WHERE clause. WHERE clause is used to specify a condition while...

5 minutes read.

SQL Between Operator

SQL Between operator is a logical operator in the Structured Query Language. The Between operator is used to retrieve data within the range specified in the condition in the query. The...

7 minutes read.

How to Create Temporary Table in SQL?

How to Create Temporary Table in SQL  Introduction to Temporary Tables Temporary table is a table which is used to store temporary data that can be used further in the same client...

3 minutes read.

Grant Command in SQL

What is DCL (Data Control Language)? Data Control Language (DCL) is a computer programming language with syntax intended to manage access to data kept in databases. It is a part of...

3 minutes read.

DELETE VS DROP in SQL

This page contains all the information about the Delete command and Drop command concept and the difference between the DELETE and DROP commands in SQL. What is the DELETE command in...

3 minutes read.

Types of SQL Commands

The Structured Query Language is used to deal with structured data. The data which are stored in the form of tables are structured data. These SQL commands store records or...

10 minutes read.

SQL IN vs SQL EXISTS

SQL IN vs SQL EXISTS This article discusses in detail about the IN and the EXISTS operators in SQL. It is a common question between developers that what is the difference...

3 minutes read.

SQL FULL JOIN

In this section, we will help you understand the concept of the SQL FULL Join clause with a few examples. The SQL FULL Join query is executed to display the integrated...

3 minutes read.

Save Point in SQL

In SQL, the classification is done into 4 languages. They are Data Definition Language (DDL)Data Manipulation Language (DML)Transaction Control Language (TCL)Data Control Language (DCL) Save Point falls under the Transaction Control Language....

4 minutes read.

SQL SELECT MAX

The SQL Max() function is an aggregate function in SQL. This function returns the values which are greater in the condition. The condition may be a number, or it may...

5 minutes read.

How to use COUNT in SQL?

How to use COUNT in SQL Introduction COUNT( ) is an aggregate function in SQL.This function counts the number of records in a table if the condition is not specified.If the condition...

4 minutes read.

SQL CREATE TABLE

In SQL tutorial, we learned and created different databases. To stores data in databases, we need to create a table. To create the table, we need to use CREATE TABLE...

5 minutes read.

SQL SELECT BETWEEN Operator

This SQL tutorial explains and helps us understand how to use the BETWEEN operator in the SELECT query with examples. The SQL SELECT BETWEEN operator retrieves the data from the table...

2 minutes read.

SQL KEYS

SQL KEYS are single or multiple attributes used to get data from the table according to the requirement or condition. They can also be used to set up relationships amongst...

3 minutes read.

SQL Top Clause

Top Clause: It returns the first ‘n’ number of rows as output. This Top clause is used to retrieve the required number of rows when there are thousands of records stored...

3 minutes read.

SQL DELETE

In this tutorial, you will learn about the SQL DELETE concept by using examples. In Structured Query Language (SQL), we fetch the data from the database table using SELECT Statement and...

3 minutes read.

SQL INSERT INTO SELECT

In this tutorial, we will help you to understand and learn how to copy records from one table and add them to another table in the SQL with the help...

5 minutes read.

SQL NOT Operator

SQL NOT is a Boolean operator used with the WHERE clause. NOT operator shows the records if the expression is false. When we use the NOT operator, we fetch only...

7 minutes read.

SQL Aliases

SQL Aliases: These are used to give a temporary name for a column or table. These are used to make tables or columns more readable. These are used to rename the table...

2 minutes read.