In database management, working with data quickly and correctly is very important. This is where Data Manipulation Language (DML) helps. DML is a key part of SQL and lets users get, add, change, or remove data in a database. Learning DML helps keep the data correct and useful. In this article, we will explain what DML is, look at its types, and show simple examples. We will also talk about primary keys and the different types of SQL. That will generally help you understand more about how databases work.

Data Manipulation Language Definition

DML is a part of SQL that helps you work with the data in a database. It generally lets you get data, add new data, change existing data, or delete data from tables. The main Data Manipulation Language commands are SELECT, INSERT, UPDATE, and DELETE. These commands help keep the data correct and useful. If you want to learn DML practically, you can explore our Python Training Course, where SQL and database operations are taught using real-world examples.

Importance of DML

Data Manipulation Language is important for a few key reasons:

  • Managing Data: It helps users keep their data organized and current, making sure that the information in the database is relevant and useful.
  • User-Friendly Interaction: DML commands allow people to work with the database easily. Even if they don’t know all the technical details about how it’s structured.
  • Maintaining Data Quality: By using DML commands, users can ensure that the data remains accurate and consistent. This is crucial for reliable information.

Types of Data Manipulation Language

DML comes in two main types: procedural and non-procedural DML.

1. Procedural DML

This type requires you to clearly outline a series of steps or actions that need to be taken to work with the data. It’s similar to giving someone a detailed recipe, it tells you exactly what to do and in what order. This method can be very powerful because it allows for more complex tasks. But it can also be more complicated because you need to think through each step carefully.

2. Non-Procedural DML

In contrast, non-procedural Data Manipulation Language lets you express what you want to do with the data without having to explain how to do it. It’s like telling someone, “I want a pizza,” without needing to describe how to make it. This approach is generally easier to use and is often found in tools like SQL. It enables you to compose straightforward requests for information or updates, making it more accessible to everyday users.

DML Commands in Sql With Examples

In SQL, DML commands are used to perform various operations on the data stored in a database. The primary DML commands include:

1. SELECT

The SELECT statement is generally used to retrieve data from one or more tables. It allows users to specify which columns to retrieve and can include conditions to filter the results.

Example:

SELECT first_name, last_name FROM employees WHERE department = 'Sales';

2. INSERT

The INSERT Data Manipulation Language statement is used to add new records to a table. Users can specify the values for each column in the new record.

Example:

INSERT INTO employees (first_name, last_name, department) VALUES ('John', 'Doe', 'Sales');

3. UPDATE

The UPDATE data manipulation in SQL is used to modify existing records in a table. Users can specify which records to update and the new values for the specified columns.

Example:

UPDATE employees SET department = 'Marketing' WHERE last_name = 'Doe';

4. DELETE

The DELETE statement is used to remove records from a table. Users can specify which records to delete based on certain conditions.

Example:

DELETE FROM employees WHERE last_name = 'Doe';

5. MERGE

The MERGE Data Manipulation Language statement is used to perform an "upsert" operation, which means it can insert new records or update existing ones based on certain conditions.

Example:

MERGE INTO employees AS target

USING (SELECT 'John' AS first_name, 'Doe' AS last_name, 'Sales' AS department) AS source

ON target.first_name = source.first_name AND target.last_name = source.last_name

WHEN MATCHED THEN

UPDATE SET target.department = source.department

WHEN NOT MATCHED THEN

INSERT (first_name, last_name, department) VALUES (source.first_name, source.last_name, source.department);

Also Read: DML and DDL in SQL – Explain SQL Commands in Detail

Example of DML in Action

Let’s consider a simple example involving a database for a bookstore. The database has a table named books with the following columns: id, title, author, and price.

Inserting Data:

INSERT INTO books (title, author, price) VALUES ('The Great Gatsby', 'F. Scott Fitzgerald', 10.99);

Retrieving Data:

SELECT * FROM books WHERE price < 15.00;

Updating Data:

UPDATE books SET price = 12.99 WHERE title = 'The Great Gatsby';

Deleting Data:

DELETE FROM books WHERE title = 'The Great Gatsby';

Understanding Primary Keys in Databases

As we are looking at Data Manipulation Language, now we will see what a primary key in SQL is. So, a primary key is like a unique ID card for each item in a database table. Just as everyone has a distinct identification number, every record in a table needs a primary key to stand out. This is important because it helps keep the data organized and ensures that we can easily find and manage each item without confusion.

What Makes a Primary Key Special?

  • Uniqueness: Every primary key must be different from all the others in the table. No two items can share the same primary key.
  • Non-nullability: A primary key can't be empty. Each record needs to have a valid ID so that it can be identified.
  • Immutability: Once we set a primary key, it shouldn't change often. We want to keep the IDs stable to avoid confusion.

Example of a Primary Key

Suppose we have a table that lists books. We can use the “id” column as the primary key. Each book will have its unique ID number, making it easy to sort and find any book in the table.

CREATE TABLE books ( 2 id INT PRIMARY KEY, 3 title VARCHAR(100), 4 author VARCHAR(100), 5 price DECIMAL(10, 2) 6);

Conclusion

Data Manipulation Language is an important part of SQL that helps users work with data in a database. By learning commands like SELECT, INSERT, UPDATE, DELETE, and MERGE, users can keep their data correct and organized. Understanding primary keys also helps keep data unique and error-free. So, knowing the different types of SQL, including DML, is useful for working with databases and making sure the information is right.

Frequently Asked Questions (FAQs)
Q. What is the primary key in SQL?

Ans. A primary key in SQL is used to identify each row in a table. It must be unique and cannot be empty, which helps keep the data correct and easy to find.

Q. What are the 5 types of SQL?

Ans. The five types of SQL are DDL, DML, DCL, TCL, and DQL. They are used to create tables, change data, control user access, manage changes, and get data from the database.