Data Manipulation Language (DML)
DML Commands:
- These Commands are used to manage and manipulate data within existing database tables.
- DML commands are essential for everyday database operations, such as adding new customer records, updating product prices, or deleting obsolete data.
- They are typically used within transactions to ensure data consistency and integrity, allowing multiple operations to be committed or rolled back as a single unit.
The following are the DML Commands:
- INSERT
- UPDATE
- DELETE
- SELECT
INSERT:
The INSERT INTO Statement is used to insert New Records in a Table.
Syntax:
INSERT INTO table_name (column1, column2, …)
VALUES (value1, value2, …);
Example:
INSERT the data only specified Columns:
If you want add data only specific columns you can do it.
Here is the Example:
INSERT into Employee(FirstName,LastName,City)
Values(‘santhosh’,’kumar’,’vijyawada’);
INSERT Multiple Rows at a time:
We can also insert multiple rows in a single statement.
Example:
INSERT into Employee(FirstName,LastName,City)
Values(‘santhosh’,’kumar’,’vijyawada’),
(‘Ganesh’,’kumar’,’guntur’),
(‘prasad’,’kumar’,’vijayawada’);
NOTE:
We have to separate each set of values with comma.
UPDATE Command:
This Command is used to modify the existing Records in a Table.
Syntax:
UPDATE table_name
SET column1 = value1, column2 = value2, …
WHERE condition;
Example:
The following update statement is updating with new Email
UPDATE Employee
SET Email = ‘new_email@example.com’
WHERE EmpID = 101;
UPDATE Multiple Columns:
Example:
UPDATE Employee
SET Salary = 23000,
Bonus = salary + 1000;
WHERE EmpID = 102;
Key Points:
- Always specify the table to update using UPDATE table_name.
- Use the SET clause to indicate which columns and values to modify.
- Use the WHERE clause to target specific rows for updating (if needed).
DELETE Command:
This command is used to delete exciting Records or data from a Table.
Syntax:
DELETE FROM table_name
WHERE condition;
Example:
If you want to delete a specific record from a table
Delete from Employee
Where FirstName = ‘Ganesh’;
NOTE: if you don’t specify ‘Where’ Condition then all the records will be delete.
SELECT Command:
This command is used to retrieve data from the Database.
Syntax:
SELECT column1, column2, …
FROM table_name;
Example:
Retrieve all the Records from a table:
Select * from Employee;
To retrieve specific columns from a Table:
SELECT FirstName, LastName, Email FROM Employee;
Filter Results with the Condition:
SELECT * FROM Employee WHERE Salary > 25000;