Published on

Power Up with the INSERT Statement! 📥

Authors
  • avatar
    Name
    Edward Villarin
    Twitter

Power Up with the INSERT Statement! 📥

The INSERT statement is the superhero of SQL when it comes to adding new rows to a database table! 🦸‍♂️ It’s how you pump fresh data into your tables, whether you’re adding a complete row or just filling specific columns. There are two main flavors of INSERT:

  • INSERT INTO table_name: Adds a full row, expecting values for all columns in the table’s defined order. 📋
  • INSERT INTO table_name (column1, column2, ...): Lets you specify which columns to fill, giving you flexibility to skip optional fields. 🎯

With INSERT, you’re building your database one row at a time, making it essential for data creation and updates! 🌟

Sample Code: INSERT in Action! 💻

Here’s a sample SQL query using a customers table (columns: customer_id, name, email, signup_date) to showcase the INSERT statement’s power:

-- Create a sample customers table
CREATE TABLE customers (
    customer_id INT,
    name VARCHAR(50),
    email VARCHAR(100),
    signup_date DATE
);

-- INSERT: Full row insertion
INSERT INTO customers
VALUES (1, 'Emma Watson', 'emma@example.com', '2025-01-10');

-- INSERT: Specific columns
INSERT INTO customers (customer_id, name, email)
VALUES (2, 'Liam Chen', 'liam@example.com');

-- INSERT: Multiple rows at once
INSERT INTO customers (customer_id, name, email, signup_date)
VALUES 
    (3, 'Sofia Alvarez', 'sofia@example.com', '2025-02-15'),
    (4, 'Noah Kim', 'noah@example.com', '2025-03-01');

-- Verify the inserted data
SELECT * FROM customers;