ProgrammingBackend Developer

Explain the difference between DDL and DML commands in SQL, provide examples, and discuss how this impacts access rights and transactions in practice.

Pass interviews with Hintsage AI assistant

Answer

DDL (Data Definition Language) is a language for defining the structure of a database (creating, modifying, deleting tables, schemas, indexes, etc.):

  • Examples of commands: CREATE TABLE, ALTER TABLE, DROP TABLE.
  • DDL commands are usually automatically committed in the database (autocommit), meaning undoing changes is difficult.

DML (Data Manipulation Language) is a language for manipulating data that works with the contents of tables (adding, modifying, deleting, retrieving rows):

  • Examples of commands: INSERT, UPDATE, DELETE, SELECT.
  • DML commands can usually be wrapped in a transaction and can be rolled back until committed.

For instance, different users may have different rights solely for DML or solely for DDL:

  • Normal applications are often restricted from DDL to prevent accidental changes to the database structure;
  • DML is permitted for working with data.

Example:

-- DDL: create a table CREATE TABLE users ( id INT PRIMARY KEY, name VARCHAR(100) ); -- DML: insert data INSERT INTO users (id, name) VALUES (1, 'Ivan');

Trick Question

"Is it possible to roll back a DDL command (like DROP TABLE), if it was executed within an explicit transaction?"

Most DBMS (like PostgreSQL, Oracle) support transactions for DDL, but many do not allow rolling back DDL commands, even if they are explicitly wrapped in a transaction. In MySQL, DDL commands are always immediately committed.

Example:
BEGIN; DROP TABLE employees; ROLLBACK;

In most cases, the table will be permanently deleted despite the rollback.

Examples of real mistakes due to lack of knowledge in the subject


Story

Loss of a table in the development environment after accidentally executing DROP TABLE — the command is irreversible, despite the rollback of the transaction.


Story

The organization granted administrative rights to a project that, due to a bug, made mass changes to the database schema through ALTER, resulting in prolonged service downtime and loss of orders.


Story

By denying DDL rights to newcomers, the team forgot that procedures for deploying migrations require DDL access — the release failed after several hours of investigation and emergency role elevation.