Inserting, updating, and deleting data
In SQL, you can insert, update, and delete data stored in relational databases. Here are examples of how to perform each of these operations in SQL:
- Inserting data: The
INSERT INTO
statement is used to insert data into a table.
INSERT INTO customers (first_name, last_name, email)
VALUES ('John', 'Doe', 'johndoe@example.com');
In this example, the INSERT INTO
statement is used to insert a new row into the customers
table. The columns first_name
, last_name
, and email
are specified, and the values 'John'
, 'Doe'
, and 'johndoe@example.com'
are assigned to these columns.
- Updating data: The
UPDATE
statement is used to modify existing data in a table.
UPDATE customers
SET email = 'johndoe123@example.com'
WHERE first_name = 'John' AND last_name = 'Doe';
In this example, the UPDATE
statement is used to modify the email address of a customer with the first name 'John'
and last name 'Doe'
in the customers
table. The SET
clause is used to specify the new email address, and the WHERE
clause is used to identify the specific row to be updated.
- Deleting data: The
DELETE FROM
statement is used to remove data from a table.
DELETE FROM customers
WHERE first_name = 'John' AND last_name = 'Doe';
In this example, the DELETE FROM
statement is used to remove the row for the customer with the first name 'John'
and last name 'Doe'
from the customers
table. The WHERE
clause is used to identify the specific row to be deleted.
It is important to use these operations carefully, as they can result in permanent data loss. It is always a good practice to back up your data before performing any update or delete operations.
Leave a Comment