Lesson 20: Inserting New Data (INSERT)
The INSERT statement is part of the DML group and is used to add new rows (records) to a table.
Basic INSERT Syntax (Specifying Columns)
This is the safest and most recommended method, as it explicitly states which values correspond to which columns.
sql INSERT INTO [table_name] ([column1], [column2], [column3], ...) VALUES ([value1], [value2], [value3], ...);
Example: Adding a New Customer
Assume CustomerID is an auto-incrementing PK (meaning we don't need to specify its value).
sql INSERT INTO Customers (CustomerName, Email, City) VALUES ('Jane Smith', 'jane.s@example.com', 'London');
Shorthand INSERT (Not Recommended for Production)
If you provide values for every single column in the table (in the exact order they were defined), you can omit the column list. Avoid this if the table structure might change.
sql -- Assuming the table has 3 columns: ID, Name, City INSERT INTO Cities VALUES (10, 'Paris', 'France');
Inserting Multiple Rows
Many modern SQL implementations allow inserting multiple rows in a single statement (improving efficiency).
sql INSERT INTO Products (Name, Price, Category) VALUES ('Monitor', 250.00, 'Electronics'), ('Keyboard', 75.00, 'Peripherals'), ('Mouse', 30.00, 'Peripherals');