MySQL Data Manipulation
Data Manipulation in MySQL:
Data manipulation operations allows you to add, modify, and remove data from tables efficiently in your MySQL database.
Inserting Data:
- Use the INSERT INTO statement to add new records (rows) into a table.
INSERT INTO table_name (column1, column2) VALUES (value1, value2);
Updating Data:
- Use the UPDATE statement to modify existing records in a table.
UPDATE table_name SET column1 = value1 WHERE condition;
Deleting Data:
- Use the DELETE FROM statement to remove records from a table.
UPDATE table_name SET column1 = value1 WHERE condition;
Bulk Insert:
- You can use the INSERT INTO statement with multiple value sets to insert multiple rows at once.
INSERT INTO table_name (column1, column2) VALUES (value1, value2), (value3, value4), ...;
Bulk Update:
- You can use the UPDATE statement with conditions to update multiple records at once.
UPDATE table_name SET column1 = value1 WHERE condition;
Bulk Delete:
- DELETE FROM command with predefined conditions enables you to eliminate several records simultaneously.
DELETE FROM table_name WHERE condition;
Using Transactions for Bulk Operations:
- Transactions focus to safeguard an environment of manipulative data stream to either all succeed or all fail.
- The transitions can be controlled with the help of the BEGIN, COMMIT, and ROLLBACK statements.
Example of Transaction:
BEGIN;
INSERT INTO table_name (column1, column2) VALUES (value1, value2);
UPDATE table_name SET column1 = value1 WHERE condition;
DELETE FROM table_name WHERE condition;
COMMIT;