SQL Tutorial



DML IN SQL


DML in SQL

Data Manipulation Language (DML) is a subset of SQL used to manage data within schema objects. It involves operations like inserting, updating, and deleting data in tables. The most commonly used DML commands are INSERT, UPDATE, and DELETE.

πŸ“š What is DML?

DML allows users to interact with the data stored in a database. The main DML operations are:

  • INSERT: Adds new rows of data to a table.
  • UPDATE: Modifies existing data in a table.
  • DELETE: Removes data from a table.

πŸ“ INSERT Statement

The INSERT statement is used to add new rows of data into a table. Here's the basic syntax:

INSERT INTO table_name (column1, column2, column3)
VALUES (value1, value2, value3);
  

Example: Insert a new student record into the Students table:

INSERT INTO Students (StudentID, Name, Grade)
VALUES (1, 'John Doe', 'A');
  

πŸ“ UPDATE Statement

The UPDATE statement is used to modify existing data in a table. Here's the basic syntax:

UPDATE table_name
SET column1 = value1, column2 = value2
WHERE condition;
  

Example: Update the grade of student with StudentID = 1 in the Students table:

UPDATE Students
SET Grade = 'B'
WHERE StudentID = 1;
  

πŸ“ DELETE Statement

The DELETE statement is used to remove one or more rows from a table. Here's the basic syntax:

DELETE FROM table_name WHERE condition;
  

Example: Delete a student record from the Students table where StudentID = 1:

DELETE FROM Students WHERE StudentID = 1;
  

πŸ’‘ Important Notes

  • INSERT: You can insert multiple records at once using multiple VALUES clauses.
  • UPDATE: Always use a WHERE clause to specify which rows to update. Without it, all rows in the table will be updated.
  • DELETE: Be careful when using DELETE. If you omit the WHERE clause, all rows in the table will be deleted.

πŸš€ Interactive Example

Let's assume a Products table with columns ProductID, ProductName, and Price:

CREATE TABLE Products (
  ProductID INT,
  ProductName VARCHAR(100),
  Price DECIMAL(10, 2)
);

INSERT INTO Products (ProductID, ProductName, Price)
VALUES (1, 'Laptop', 800.00), (2, 'Smartphone', 500.00);
    

Now, let's update the price of the product with ProductID = 1:

UPDATE Products
SET Price = 750.00
WHERE ProductID = 1;
    

Finally, let's delete the product with ProductID = 2:

DELETE FROM Products WHERE ProductID = 2;
    

Conclusion

DML statements are essential for interacting with data in a database. By mastering the INSERT, UPDATE, and DELETE commands, you can effectively manage and manipulate your database’s data.

Quick Tip:

Before using the DELETE statement, always double-check your WHERE clause to avoid accidental data loss.


🌟 Enjoyed Learning with Us?

Help others discover Technorank Learning by sharing your honest experience.
Your support inspires us to keep building!

Leave a Google Review