Using SQL to Update Data in a Database
Inserting New Records with INSERT INTO
The INSERT INTO statement adds new records to a table.
ExampleAdding a new customer to a database:
INSERT INTO Customer (CustomerID, CustomerEmail, CustomerAddress) VALUES (1, 'john.doe@example.com', '123 Main St');
TipWhen inserting data, ensure that all required fields (e.g., those marked as NOT NULL) are provided to avoid errors.
Modifying Data with UPDATE SET
The UPDATE statement modifies existing records in a table.
ExampleChanging the email address of a customer:
UPDATE Customer SET CustomerEmail = 'jane.doe@example.com' WHERE CustomerID = 1;
Note- Always use a WHERE clause to specify which records to update.
- Omitting it will update all records in the table.
Removing Data with DELETE
The DELETE statement removes records from a table.
ExampleDeleting a customer record:
DELETE FROM Customer WHERE CustomerID = 1;
NoteBe cautious when using DELETE without a WHERE clause, as it will remove all records from the table.