Introduction
In this tutorial, we will focus on the basic SQL operations: Insert, Update, Delete, and Alter.
Insert Statement
The Insert statement is used to add new records to a table. It follows the syntax:
INSERT INTO table_name (column1, column2, ...) VALUES (value1, value2, ...);
For example, to insert a new record into the “Customers” table:
INSERT INTO Customers (CustomerID, CustomerName, City) VALUES ('001', 'John Doe', 'New York');
Update Statement
The Update statement is used to modify existing records in a table. It follows the syntax:
UPDATE table_name SET column1 = value1, column2 = value2, ... WHERE condition;
For example, to update the “City” of a customer with a specific “CustomerID” in the “Customers” table:
UPDATE Customers SET City = 'Los Angeles' WHERE CustomerID = '001';
Delete Statement
The Delete statement is used to remove records from a table. It follows the syntax:
DELETE FROM table_name WHERE condition;
For example, to delete a customer with a specific “CustomerID” from the “Customers” table:
DELETE FROM Customers WHERE CustomerID = '001';
Alter Statement
The Alter statement is used to modify the structure of a table. It can add, modify, or delete columns in an existing table. It follows the syntax:
ALTER TABLE table_name ADD column_name datatype; ALTER TABLE table_name MODIFY column_name datatype; ALTER TABLE table_name DROP COLUMN column_name;
For example, to add a new column “Email” to the “Customers” table:
ALTER TABLE Customers ADD Email VARCHAR(50);
Conclusion
In this tutorial, we covered the basic SQL operations: Insert, Update, Delete, and Alter. These operations are fundamental for managing and manipulating data in a relational database. By understanding and practicing these statements, you will have a solid foundation for working with SQL.
Remember, SQL is a powerful language with many more features and advanced operations. This tutorial serves as a starting point to help you get familiar with the basics.