SQL INSERT Statement
The INSERT statement is used to add new rows of data into a table. It specifies the table name and the values to be inserted into its columns. The INSERT statement can add a single row at a time, ensuring accurate data entry into the database.
You can use the INSERT INTO statement in two ways:
1. Specify the column names explicitly.
2. Omit the column names (only works if values match all table columns in sequence).
Example
In this example, a single row is inserted into the Orders table.
Code Example
-- Insert a single row into the Orders table
INSERT INTO Orders (Product, Category, Price)
VALUES ('Smartwatch', 'Gadgets', 250.00);
-- View the updated table
SELECT * FROM Orders;
Output
Product | Category | Price |
---|---|---|
Laptop | Electronics | 1200.00 |
Phone | Electronics | 800.00 |
Tablet | Gadgets | 600.00 |
Smartwatch | Gadgets | 250.00 |
Explanation
- The INSERT INTO Orders specifies the table where data is being inserted.
- The columns (Product, Category, Price) match the data provided in the VALUES clause.
- After executing the query, the table now contains the new row for the product "Smartwatch".
Using the SELECT statement, you can view the updated table and verify the insertion.