DML (Data Manipulation Language) commands are used to manipulate data in a database. Here are four common DML commands with examples of their use:
-
SELECT: The SELECT command is used to retrieve data from one or more tables. The syntax for a simple SELECT statement is:
SELECT column1, column2, ... FROM table_name WHERE condition;
For example, to retrieve the names and ages of all students with a GPA greater than 3.0 from the "Students" table, we might use the following command:
SELECT Name, Age FROM Students WHERE GPA > 3.0;
This command selects the "Name" and "Age" columns from the "Students" table where the "GPA" is greater than 3.0.
-
INSERT: The INSERT command is used to add new rows to a table. The syntax for inserting data into a table is:
INSERT INTO table_name (column1, column2, ...) VALUES (value1, value2, ...);
For example, to add a new student with the name "John Doe", age 21, and a GPA of 3.5 to the "Students" table, we might use the following command:
INSERT INTO Students (Name, Age, GPA) VALUES ('John Doe', 21, 3.5);
This command adds a new row to the "Students" table with the specified values.
-
UPDATE: The UPDATE command is used to modify existing rows in a table. The syntax for updating data in a table is:
UPDATE table_name SET column1 = value1, column2 = value2, ... WHERE condition;
For example, to update the GPA of the student named "John Doe" to 3.7 in the "Students" table, we might use the following command:
UPDATE Students SET GPA = 3.7 WHERE Name = 'John Doe';
This command updates the "GPA" column to 3.7 for the row where the "Name" is "John Doe".
-
DELETE: The DELETE command is used to remove rows from a table. The syntax for deleting data from a table is:
DELETE FROM table_name WHERE condition;
For example, to delete all students with a GPA less than 2.0 from the "Students" table, we might use the following command:
DELETE FROM Students WHERE GPA < 2.0;