Mastering SQL Data Modification: INSERT, UPDATE, DELETE, and Transactions | Electro4u

06 Jun 2023 Balmiki Mandal 0 SQL

Modifying Data in SQL

SQL provides three main statements for modifying data in a database:

  • INSERT - Creates a new row in a table.
  • UPDATE - Modifies the values in an existing row in a table.
  • DELETE - Deletes an existing row from a table.

INSERT Statement

The INSERT statement is used to create a new row in a table. The syntax for the INSERT statement is as follows:

SQL
INSERT INTO table_name (column_name1, column_name2, ...) VALUES (value1, value2, ...);

Example: INSERT statement creates a new row in the customers table:

SQL
INSERT INTO customers (name, email, phone) VALUES ('John Doe', '[email protected]', '+15555555555');

UPDATE Statement

The UPDATE statement is used to modify the values in an existing row in a table. The syntax for the UPDATE statement is as follows:

SQL
UPDATE table_name SET column_name1 = value1, column_name2 = value2, ... WHERE condition;

The WHERE clause is optional, but it is recommended to use it to ensure that you are only updating the rows that you want to update. For example, the following UPDATE statement updates the email address of the customer with the ID of 1:

SQL
UPDATE customers SET email = '[email protected]' WHERE id = 1;

DELETE Statement

The DELETE statement is used to delete an existing row from a table. The syntax for the DELETE statement is as follows:

SQL
DELETE FROM table_name WHERE condition;

The WHERE clause is optional, but it is recommended to use it to ensure that you are only deleting the rows that you want to delete. For example, the following DELETE statement deletes the customer with the ID of 1:

SQL
DELETE FROM customers WHERE id = 1;

Transaction Management using COMMIT and ROLLBACK

A transaction in SQL is a group of one or more statements that are executed together as a single unit. Transactions are used to ensure that data is always in a consistent state.

To start a transaction, you use the BEGIN TRANSACTION statement. To commit a transaction, you use the COMMIT statement. To rollback a transaction, you use the ROLLBACK statement.

If a statement in a transaction fails, the entire transaction is rolled back. This ensures that the data in the database is not corrupted.

Example: how to use a transaction to update the customer's email address and phone number:

SQL
BEGIN TRANSACTION;

UPDATE customers SET email = '[email protected]', phone = '+15555555555' WHERE id = 1;

COMMIT;

If the UPDATE statement fails, the entire transaction will be rolled back and the customer's email address and phone number will not be changed.

 

Transactions are an important part of SQL programming. By using transactions, you can ensure that your data is always in a consistent state.

Top Search:


Enroll Now:


 

[ Course in production] "Start Supercharging Your Productivity!"

Contact Us:


 

  • For any inquiries, please email us at [[email protected]].
  • Follow us on insta  [ electro4u_offical_ ] for updates and tips.

 

Note: If you encounter any issues or specific errors when running this program, please let me know and I'll be happy to help debug them!

BY: Balmiki Mandal

Related Blogs

Post Comments.

Login to Post a Comment

No comments yet, Be the first to comment.