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.
DML allows users to interact with the data stored in a database. The main DML operations are:
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');
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;
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;
VALUES
clauses.WHERE
clause to specify which rows to update. Without it, all rows in the table will be updated.DELETE
. If you omit the WHERE
clause, all rows in the table will be deleted.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;
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.
Help others discover Technorank Learning by sharing your honest experience.
Your support inspires us to keep building!