Year End Sale: Get Upto 40% OFF on Live Training! Offer Ending in
D
H
M
S
Get Now
Data Definition Language (DDL) in DBMS: Structuring Your Database

Data Definition Language (DDL) in DBMS: Structuring Your Database

17 Oct 2024
Beginner
982 Views
21 min read
Learn with an interactive course and practical hands-on labs

SQL Server Foundation Course: Best SQL Courses Online

Data Definition Language (DDL) in DBMS

Data Definition Language (DDL) in DBMS is a component of SQL (Structured Query Language). DDL in DBMS is utilized in managing database systems (DBMS) to define and oversee all the elements within a database. DDL commands are employed to create, alter, and remove database configurations without affecting the data. Through the application of DDL commands, database frameworks can be Upheld, guaranteeing the arrangement and format of information stored in the database.

In this DBMS Tutorialwe will learn deep down what is the Data Definition Language(DDL), what are DDL commands, the types of DDL commands, create commands, alter commands, update commands, truncate commands, comment commands, and many more.

What is Data Definition Language in DBMS?

In an easy way, Data Definition Language (DDL) is a bunch of SQL commands used to explain and customize the structure of a database management system, such as tables, indexes, and schemas. It provides several commands to the user by which you can operate the database, such as creating commands, altering commands, and updating commands. Let us learn about Data Definition Language Commands first.

What are Data Definition Language(DDL) Commands in DBMS?

The structure of database objects is defined and managed using DDL (Data Definition Language) commands. This is a succinct explanation:

  • CREATE: Generates new indexes and tables in a database.
  • ALTER: Changes an object's existing structure.
  • DROP: Permanently deletes database objects.
  • TRUNCATE: A table's structure is preserved when all of its rows are removed.
  • RENAME: Changes the name of tables or columns in a database.

You have four types of DDL commands that are:

  1. Create Command
  2. Alter COmmand
  3. Drop Command
  4. Truncate Command
  5. Comment Command
  6. Rename Command

If you are using the DDL commands, you should remember that DDL commands don't change or experiment with the data that is stored in the database.

What are Data Definition Language(DDL) Commands in DBMS

1. CREATE Command

You can use the Create Command in the SQL to create the table, create the index, and create the view. These SQL commands result in the newly formatted table.

Create Command Syntax

You can use this syntax to make a table in the database:

CREATE TABLE table_name
(
column_1 datatype,
column_2 datatype,
column_3 datatype,
....
);

Example

By following the above syntax, we are creating a table of Students.

CREATE TABLE Students(
    stud_id INT PRIMARY KEY,
    first_name VARCHAR(50),
    last_name VARCHAR(50),
    admission_date DATE
);

After running the above command, it generates the table of Students:

Students Table:

stud_idfirst_namelast_nameadmission_date

2. ALTER Command

The following database objects can be reorganized by adding, removing, or changing table columns using the Alter command.

Alter Command Syntax

You can use Alter Command syntax to alter a table in the database:

ALTER TABLE table_name
ADD column_name datatype;

Example

Here, we will add a new column in the above-made 'Students' table by using the Alter syntax.

ALTER TABLE students ADD COLUMN email VARCHAR(100);

After running the above command, it adds a new column in the table of Students:

Student Table:

stud_idfirst_namelast_nameadmisssion_dateemail

3. DROP Command

You can use the DROP command to permanently delete existing database objects.

DROP Command Syntax

You can use the drop Command syntax to delete an object in the database:

DROP TABLE table_name;

Example

Here, you can delete the above Student table by using the above Drop Commands.

DROP TABLE students;

After using the DROP command, the above Students table will be deleted.

4. TRUNCATE Command

You can use the TRUNCATE command to delete all the existing rows in a table without deleting the table itself. It resets any auto-incremented columns.

TRUNCATE Command Syntax

You can use the TRUNCATE Command syntax to delete all the rows in the database:

TRUNCATE TABLE table_name;

Example

Suppose we have the Students table, and we want to delete all the rows from the table.

Students Table:

stud_idfirst_namelast_nameadmission_date
001AmanMishra16-06-2017
002AnkitaDubey25-06-2017
003ShaileshOjha27-06-2017
TRUNCATE TABLE Students;

After performing the above query, all the rows of the Students table will be deleted.

Students Table:

stud_idfirst_namelast_name

5. RENAME Command

You can use the RENAME command to change the name of the table.

Rename Commands Syntax

Here is the syntax for changing the name of the table by using RENAME commands:

Rename <OLD_TABLENAME> to <NEW_TABLENAME>; 

Example

You can use Rename commands to change the 'Students' table to the 'Student_details' table.

RENAME Students to Student_details;

After running the above query, the renamed table is Student_details.

Student_details

stud_idfirst_namelast_nameadmission_date

6. COMMENT Command

We use the COMMENT commands to write the comments inside the SQL queries. These comments, which are meant for documentation purposes only, can help developers and database administrators understand the function or application of particular database objects.

There are two ways to write comments on SQL queries that are:

  1. Single-Line Comments
  2. Multi-Line Comments

1. Single-Line Comments

We are using the (--) operator to add single-line comments in the SQL query.

Example

2. Multi-Line Comments

We can use “/ ** /” to write the multi-line comments in the SQL query.

Example

Best Practices of using Data Definition Language(DDL)

Here, we will discuss the best practice of using DDL commands

1. Creating Database Objects

We can use the Data Definition Language for creating different types of database objects:

1. Creating Databases: Creating a new database involves defining the database name and establishing its initial structure.

CREATE DATABASE company_db;

2. Creating Tables: Creating tables involves defining the table's columns, data types, and constraints.

CREATE TABLE departments (
    department_id INT PRIMARY KEY,
    department_name VARCHAR(100)
);

3. Creating Indexes: Indexes are created to enhance the performance of data retrieval operations.

CREATE INDEX idx_employee_name ON employees (last_name);

2. Modifying Database

We can use the Data Definition Language to modify the database.

1. Objects Altering Tables: Altering tables allows for adding, dropping, or modifying columns and constraints.

ALTER TABLE employees MODIFY COLUMN email VARCHAR(255);

2. Renaming Database: Objects Renaming database objects involves changing the name of existing objects.

ALTER TABLE employees RENAME TO staff;

3. Deleting Database

We can also use the DDL to delete the database.

1. Dropping Tables and Databases: Dropping tables and databases removes the objects and their data permanently.

DROP DATABASE company_db;
DROP TABLE departments;

2. Truncating Tables: Truncating tables removes all data from a table quickly without affecting the table structure.

TRUNCATE TABLE departments;

Constraints in Data Definition Language

There are seven types of SQL Constraints in DDL that are:

1. NOT NULL: Ensures that a column cannot have NULL values.

Example

CREATE TABLE Employees (
    employee_id INT NOT NULL,
    employee_name VARCHAR(100) NOT NULL
);

2. UNIQUE: Guarantees that all values in a column are distinct.

Example

CREATE TABLE Employees (
    employee_id INT UNIQUE,
    employee_email VARCHAR(100) UNIQUE
);

3. PRIMARY KEY: Combines NOT NULL and UNIQUE, ensuring each row is uniquely ident

Example

CREATE TABLE Employees (
    employee_id INT PRIMARY KEY,
    employee_name VARCHAR(100) NOT NULL
);

4. FOREIGN KEY: Connecting a column to the primary key of another table ensures referential integrity.

Example

CREATE TABLE Departments (
    department_id INT PRIMARY KEY,
    department_name VARCHAR(100)
);

CREATE TABLE Employees (
    employee_id INT PRIMARY KEY,
    employee_name VARCHAR(100),
    department_id INT,
    FOREIGN KEY (department_id) REFERENCES Departments(department_id)
);

5. CHECK: Verifies if values in a column satisfy certain requirements.

Example

CREATE TABLE Employees (
    employee_id INT,
    employee_age INT CHECK (employee_age >= 18)
);

6. DEFAULT: When a column has no value entered, a default value is assigned to it.

Example

CREATE TABLE Employees (
    employee_id INT PRIMARY KEY,
    employee_name VARCHAR(100),
    employee_status VARCHAR(20) DEFAULT 'Active'
);

7. AUTO_INCREMENT: Produces distinct values for a column automatically; this column is usually used for primary keys.

Example

CREATE TABLE Employees (
    employee_id INT AUTO_INCREMENT PRIMARY KEY,
    employee_name VARCHAR(100)
);

Difference between DDL and DML

FactorsData Definition Language(DDL)Data Manipulation Language(DML)
PurposeDefines and manages database schema/structure.Manipulates the data within the database.
OperationsCREATE, ALTER, DROP, TRUNCATE, RENAME.SELECT, INSERT, UPDATE, DELETE.
Effect on DataDoes not directly work with the data; it works with the database schema (structure).Directly affects the data stored in the tables (insert, modify, delete data).
Effect on SchemaAlters or defines the schema (table, database, or object structure).Does not alter the database schema; it only works with the data.
Transaction ControlUsually auto-committed (changes are permanent immediately).Changes can be rolled back if they are part of the transaction.
ExamplesCreating a new table, altering the structure of a table.Inserting new records, updating existing records, deleting records.
CommandsCREATE TABLE, ALTER TABLE, DROP DATABASE, etc.INSERT INTO, UPDATE, DELETE FROM, SELECT, etc.
Read More:
Top 50 DBMS Viva Questions and Answers
DBMS Interview Questions For Freshers
Conclusion
In conclusion, we have explored the Data Definition Language(DDL) and its commands. It supports the database engineer by providing SQL queries to create, alter, and truncate the database objects. If you want to become a full-stack developer in the future, Scholarhat provides you with the Full-Stack .NET Developer Certification Training Course that helps you with your career growth.

FAQs

DQL – Data Query Language. 
DML – Data Manipulation Language.
DCL – Data Control Language. TCL – Transaction Control Language.

DML is an abbreviation for Data Manipulation Language. Represents a collection of programming languages explicitly used to make changes to the database, such as: CRUD operations to create, read, update and delete data. Using INSERT, SELECT, UPDATE, and DELETE commands

A Data Definition Language (DDL) refers to a language that is used to modify data and define data structures.
Share Article
About Author
Shailendra Chauhan (Microsoft MVP, Founder & CEO at ScholarHat)

Shailendra Chauhan, Founder and CEO of ScholarHat by DotNetTricks, is a renowned expert in System Design, Software Architecture, Azure Cloud, .NET, Angular, React, Node.js, Microservices, DevOps, and Cross-Platform Mobile App Development. His skill set extends into emerging fields like Data Science, Python, Azure AI/ML, and Generative AI, making him a well-rounded expert who bridges traditional development frameworks with cutting-edge advancements. Recognized as a Microsoft Most Valuable Professional (MVP) for an impressive 9 consecutive years (2016–2024), he has consistently demonstrated excellence in delivering impactful solutions and inspiring learners.

Shailendra’s unique, hands-on training programs and bestselling books have empowered thousands of professionals to excel in their careers and crack tough interviews. A visionary leader, he continues to revolutionize technology education with his innovative approach.
Accept cookies & close this