DDL and DML

DDL and DML - Definition

Databases are essential for storing and managing data in modern applications. But behind every well-organized database is a system that defines its structure and handles its data, this is where DDL and DML come in. Some industry analysts prefer the term Database Management System (DBMS) over "database" because, technically, a database is just the data store. While a DBMS in fact combines the data store with the computational logic that defines, manages, and manipulates that data.

Most DBMSs operate using two core components: Data Definition Language (DDL) and Data Manipulation Language (DML). Together, DDL and DML handle the computation within a DBMS, while the database itself stores the data. In a Relational Database Management System (RDBMS), these operations are carried out using SQL. 

DDL commands such as CREATE, ALTER, and DROP define and modify database structures, while DML commands like SELECT, INSERT, UPDATE, and DELETE manage the data within those structures.

Understanding the role of DDL and DML in database operations is essential for working with both transactional and analytical database systems. Read on to explore their roles and applications.

What Is DDL (Data Definition Language)?

Data Definition Language (DDL) consists of SQL commands that establish a database's framework. It is responsible for setting up structures such as tables and schemas, modifying their design, and removing them when they are no longer needed.

When setting up a database, DDL commands define tables, indexes, and relationships. These commands modify structures and remove database objects. Common examples include:

  • CREATE: Defines new database objects like tables, indexes, or entire databases.

    • CREATE DATABASE my_database: Creates a new database.

    • CREATE TABLE users (id INT PRIMARY KEY, name VARCHAR(50), email VARCHAR(100)): Defines a table with columns and data types.

    • CREATE INDEX idx_email ON users(email): Generates an index for faster lookups.
  • ALTER: Modifies existing database structures without affecting stored data.

    • ALTER TABLE users ADD COLUMN phone VARCHAR(20): Adds a new column to the users table.

    • ALTER TABLE users MODIFY COLUMN name VARCHAR(100): Changes a column's data type.

  • DROP: Deletes database objects permanently.

    • DROP TABLE users: Removes the users table and its data.

    • DROP INDEX idx_email ON users: Deletes an index. (SQL Server syntax: DROP INDEX users.idx_email;)

  • TRUNCATE: Clears all records from a table without removing its structure.

    • TRUNCATE TABLE users: Empties the users table while keeping its schema intact.

DDL Commands in SQL

Here are DDL statements following MySQL and PostgreSQL standards:

CREATE TABLE users (
  id INT PRIMARY KEY, 
  name VARCHAR(50),
  email VARCHAR(100)
);
ALTER TABLE users ADD COLUMN phone VARCHAR(20);
DROP INDEX idx_email ON users; -- MySQL
-- DROP INDEX idx_email; -- PostgreSQL
TRUNCATE TABLE users;

For SQL Server, syntax varies slightly:

CREATE TABLE users (
  id INT PRIMARY KEY, 
  name VARCHAR(50),
  email VARCHAR(100)
);
ALTER TABLE users ADD phone VARCHAR(20); -- No "COLUMN" keyword in SQL Server
DROP INDEX users.idx_email; -- SQL Server requires table name prefix

In summary, DDL provides the foundation for database organization by defining structures, enforcing constraints, and allowing modifications as data needs evolve. By creating and managing tables, indexes, and relationships, it ensures databases remain structured, scalable, and efficient. 

What Is DML (Data Manipulation Language)?

Data Manipulation Language (DML) is a component of SQL used to insert, retrieve, modify, and delete records within a database structure. Unlike DDL, which defines database objects like tables and indexes, DML manages data within those structures.

The most frequent examples of commands include:

  • INSERT: Adds new rows to a table.

Example: The following statement inserts a new customer record into the customers table:

INSERT INTO customers (first_name, last_name, email)  
VALUES ('Brian,' 'David,' 'briandavid@domain.com');

UPDATE: Modifies existing data within a table.

Example: Updating a customer's email address based on their id:

UPDATE customers  
SET email = 'briandavid@domain.com'  
WHERE id = 123;


DELETE: Removes specific rows from a table.

Example: This statement deletes all customer records where the id is greater than 200:

DELETE FROM customers  
WHERE id > 200;

SELECT: Retrieves data from one or more tables.

Example: This query selects all records from the customers table:

SELECT * FROM customers;


The above statements access and modify data across tables in an SQL database.

DDL vs. DML: Key Differences

Knowing the difference between DDL and DML allows you to write well-structured queries, build more scalable schemas, and troubleshoot issues faster. Below is a side-by-side comparison of the two:

Aspect DDL DML
Purpose Defines and manages the database structure, schema, and objects. Works with the actual data stored in database tables.
Commands CREATE, ALTER, DROP, TRUNCATE, COMMENT, RENAME INSERT, UPDATE, DELETE, SELECT, MERGE
Impact Modifies the database structure (e.g., adding or removing tables, columns, indexes). Modifies or retrieves data within existing tables without changing the structure.
Examples CREATE TABLE customers (id INT, name VARCHAR(50)); This command sets up the structure for storing customer data but does not insert any data into the table. INSERT INTO customers (id, name) VALUES (1, 'William'); This code inserts a new row into the customers' table with the ID 1 and name 'William.'

How DDL and DML Work Together

DDL establishes the structure and defines tables, constraints, and relationships. Once the schema is in place, DML allows operations on stored data and handles record insertion, retrieval, updates, and deletions.

Efficient database systems balance DDL for stability and DML for flexible data handling.

Practical Examples of DDL and DML Usage

To illustrate the real-world usage of DDL vs DML, consider a simple database example that stores information about devices. The process involves two steps:

  1. Defining the table structure using DDL
  2. Adding and modifying data using DML

Here's how it works:

Step 1: Creating a Table with DDL

The following DDL statement defines the table structure in SQL:

CREATE TABLE devices (
  device_id INT PRIMARY KEY,
  device_name VARCHAR(50),
  category VARCHAR(50),
  price DECIMAL(10,2)
);
  • device_id INT PRIMARY KEY: Ensures unique device identification.
    VARCHAR(50): Stores variable-length text for names and categories.
    DECIMAL(10,2): Stores numeric values with two decimal places.

Step 2: Adding & Modifying Data with DML

Once the structure is defined, DML operations handle the data itself:

Inserting Data (INSERT): Adds a new device record:

INSERT INTO devices
VALUES (1, 'Alpha,' 'Smartphone,' 299.99); 

Updating Data (UPDATE): Adjust the price of an existing device:

UPDATE devices
SET price= 279.99
WHERE device_id=1;

Retrieving Data (SELECT): Retrieves all records:

SELECT * FROM devices;

Common Usage Scenarios

DDL is used during database setup and maintenance, helping DBAs define tables, indexes, and schemas that keep data organized and reliable. In application development, software engineers use DDL to update and refine database models. DML supports daily operations like transactions, analytics, and data pipelines. 

Data engineers rely on DML for building ETL and EL workflows, analysts use it to generate reports with SELECT queries, and developers integrate CRUD operations to manage data within applications.

Importance of DDL and DML in SQL

Without DDL and DML, databases would be unmanageable data dumps, making it impossible to organize and retrieve data. Let's explore why mastering these components is vital for efficient database management and optimized data operations:

  • Ensures Data Integrity: DDL defines an optimized schema that models business concepts and enforces data accuracy through entities like primary keys, foreign keys, and constraints. DML supports standardized CRUD processes, ensuring accuracy as data is updated or modified.
  • Improves Security: DDL also provides object-level security, granting and revoking user permissions on specific database objects. DML verifies access control lists during executions, ensuring users can only query and manipulate authorized data.
  • Simplifies Usage: DDL and SML use simple, clear keywords and SQL syntax, making them easier to work with compared to proprietary coding languages. This streamlines development and analytics tasks for technical SQL users across organizations.
  • Enhances Flexibility: Database schemas and data can be adjusted with incremental DDL and DML changes, avoiding the need for full recreations. As business needs evolve, the database model and contents can be modified iteratively.
  • Provides Insight into Database Usage and Performance: DML supports query analytics, allowing business users to analyze database usage patterns and guide efficiency improvements. DBAs rely on DML procedures to monitor output and identify optimization opportunities.

FAQs

Here are some questions to help you better understand DDL and DML. They clarify some additional concepts, commands, and how they are used in database management:

  • Can DML Commands Modify Database Structures?

No, DML commands cannot alter the structure or schema of a database. DML (Data Manipulation Language) focuses solely on managing data within existing tables or collections. Without DML, databases would be static, with no way to add, change, or remove records. 

Commands like INSERT, UPDATE, and DELETE allow users to manipulate data but cannot modify the database structure itself. For structural changes, such as adding a new column, you need DDL (Data Definition Language) commands like ALTER TABLE.

For example, you would need to use the ALTER TABLE DDL statement to add a new column to an existing table. A DML command like UPDATE or INSERT will not change the table design.

  • Are SELECT Queries Part of DML or DQL?

SELECT queries belong to the Data Query Language (DQL), not DML. The SELECT command only reads data without altering it, while DML commands like INSERT, UPDATE, and DELETE change data.

While SELECT is categorized as DQL, most databases combine DQL and DML in the same structure. Complex SELECT queries often include conditional expressions found in DML. 

  • Is it Necessary to Know DDL and DML for Advanced SQL?

Yes, understanding both DDL and DML is necessary for complex queries, performance tuning, and database administration, as DDL and DML give full control over databases, making them vital for professionals like database developers, architects, and administrators. Junior analysts should also learn these languages to write better queries and advance toward engineering or architect roles.