Difference Between DDL and DML in DBMS

Key Differences between DDL vs DML

  • Data Definition Language (DDL) helps you to define the database structure or schema, while Data Manipulation Language (DML command) allows you to manage the data stored in the database.
  • The DDL command is used to create the database schema, while the DML command is used to populate and manipulate the database
  • Comparing DDL vs DML, DDL statements affect the whole table, whereas DML commands only affect one or more rows.
  • In DDL, SQL Statement can’t be rollbacked, while in DML, SQL Statement can be rollbacked.
  • DDL is a declarative method, while DML is an imperative method.
  • Important DDL commands are: 1) CREATE, 2) ALTER, 3) DROP, 4) TRUNCATE, etc., while important DML commands are: 1) INSERT, 2) UPDATE, 3) DELETE, 4) MERGE, etc.
Difference Between DDL and DML in DBMS
Difference Between DDL and DML in DBMS

Here, I have analyzed the difference between DDL and DML and will comprehensively evaluate their pros and cons.

What is DDL?

Data Definition Language helps you to define the database structure or schema. DDL commands help you to create the structure of the database and the other database objects. Its commands are auto-committed so, the changes are saved in the database permanently. The full form of DDL is Data Definition Language.

DDL Commands

Five types of DDL commands are:

CREATE

CREATE statements is used to define the database structure schema:

Syntax:

CREATE TABLE TABLE_NAME (COLUMN_NAME DATATYPES[,....]); 

For example:

Create database university;
Create table students;
Create view for_students;

DROP

Drops commands remove tables and databases from RDBMS.

Syntax:

DROP TABLE ;  

For example:

Drop object_type object_name;
Drop database university;
Drop table student;

ALTER

Alters command allows you to alter the structure of the database.

Syntax:

To add a new column in the table

ALTER TABLE table_name ADD column_name COLUMN-definition;  

To modify an existing column in the table:

ALTER TABLE MODIFY(COLUMN DEFINITION....); 

For example:

Alter table guru99 add subject varchar;

TRUNCATE

This command is used to delete all the rows from the table and free the space containing the table.

Syntax:

TRUNCATE TABLE table_name;  

Example:

TRUNCATE table students;

DDL Command Example

CREATE

Syntax:

CREATE TABLE tableName
( 
  column_1 datatype [ NULL | NOT NULL ],
  column_2 datatype [ NULL | NOT NULL ],
  ...
);

Here,

  • The parameter tableName denotes the name of the table that you are going to create.
  • The parameters column_1, column_2… denote the columns to be added to the table.
  • A column should be specified as either NULL or NOT NULL. If you don’t specify, SQL Server will take NULL as the default

Example:

CREATE TABLE Students
(
Student_ID Int,
Student_Name Varchar(10)
)

ALTER

Syntax:

Alter TABLE <Table name> ADD Column1 datatype, Column2 datatype;

Example:

ALTER TABLE University.Students_Name ADD Course_Duration VARCHAR(20);

DROP

Syntax:

DROP TABLE <tableName>;

The parameter tableName is the name of the table that is to be deleted.

Example:

DROP TABLE COURSE_NAMES;

Why we Use DDL commands?

Here, I will explain why we prefer the DDL method:

  • Allows you to store shared data
  • Data independence improved integrity
  • Allows multiple users
  • Improved security efficient data access

What is DML?

DML commands it to allow you to manage the data stored in the database, although DML commands are not auto-committed. Moreover, they are not permanent. So, It is possible to roll back the operation. The full form of DML is Data Manipulation Language.

DML Commands

I’d like to highlight some important DML commands:

  • INSERT
  • UPDATE
  • DELETE

INSERT

This is a statement that is a SQL query. This command is used to insert data into the row of a table.

Syntax:

INSERT INTO TABLE_NAME  (col1, col2, col3,.... col N)  
VALUES (value1, value2, value3, .... valueN);  
Or 
INSERT INTO TABLE_NAME    
VALUES (value1, value2, value3, .... valueN);    

For example:

INSERT INTO students (RollNo, FIrstName, LastName) VALUES ('60', 'Tom', 'Erichsen');

UPDATE

This command is used to update or modify the value of a column in the table.

Syntax:

UPDATE table_name SET [column_name1= value1,...column_nameN = valueN] [WHERE CONDITION]   

For example:

UPDATE students    
SET FirstName = 'Jhon', LastName=' Wick' 
WHERE StudID = 3;

DELETE

This command is used to remove one or more rows from a table.

Syntax:

DELETE FROM table_name [WHERE condition];

For example:

DELETE FROM students 
WHERE FirstName = 'Jhon';

DML Command Example

INSERT

In PL/SQL, we can insert the data into any table using the SQL command INSERT INTO. This command will take the table name, table column, and column values as the input and insert the value in the base table.

The INSERT command can also take the values directly from another table using ‘SELECT’ statement rather than giving the values for each column. Through ‘SELECT’ statement, we can insert as many rows as the base table contains.

Syntax:

BEGIN
  INSERT INTO <table_name>(<column1 >,<column2>,...<column_n>)
     VALUES(<valuel><value2>,...:<value_n>);
END;

The above Syntax shows the INSERT INTO command. The table name and values are mandatory fields, whereas column names are not mandatory if the insert statements have values for all the columns of the table.

The keyword ‘VALUES’ is mandatory if the values are given separately, as shown above.

Syntax:

BEGIN
  INSERT INTO <table_name>(<columnl>,<column2>,...,<column_n>)
     SELECT <columnl>,<column2>,.. <column_n> FROM <table_name2>;
END;

The above Syntax shows the INSERT INTO command that takes the values directly from the <table_name2> using the SELECT command.

The keyword ‘VALUES’ should not be present in this case, as the values are not given separately.

DELETE

Below is the Syntax to delete table

Syntax:

DROP TABLE <TableName>;

The parameter TableName is the name of the table that is to be deleted.

Example:

DROP TABLE COURSE_NAMES;

SELECT

To view data in SQL Server, we use the SELECT statement.

Syntax:

SELECT expression
FROM tableName
[WHERE condition];

Example:

SELECT * FROM Course;

Why we Use DML Commands?

Here, drawing from our collective experience, are the benefits of DML:

  • The DML statements allow you to modify the data stored in a database.
  • Users can specify what data is needed.
  • DML offers many different flavors and capabilities between database vendors.
  • It offers an efficient human interaction with the system.

Difference Between DDL and DML in DBMS

Let me explain the main difference between DDL and DML commands in DBMS:

Key Differences between DDL vs DML
DDL vs DML
Comparison Basis DDL DML
Basic Data Definition Language (DDL) helps you to define the database structure or schema. Data Manipulation Language (DML command) allows you to manage the data stored in the database.
Use DDL command is used to create the database schema. DML command is used to populate and manipulate database
Categorization DDL is not classified further. DML is classified as Procedural and Non and Procedural DMLs.
Command Uses

The commonly used commands under the DDL language are:

  • CREATE
  • ALTER
  • DROP
  • TRUNCATE
  • COMMENT
  • RENAME

The commonly used commands under DML language are:

  • INSERT
  • UPDATE
  • DELETE
  • MERGE
  • CALL
Defines It defines the column of the table. It adds or updates the row of the table
Effect DDL statements affect the whole table. DML effects one or more rows.
Rollback SQL Statement can’t be rollback SQL Statement can be a rollback

How to Choose Between a DDL and a DML

We observe that while DDL is used to define the structure of the database, DML is all about the data within, each serving critical roles.