Warming up the neural circuits...
stands for Create, Read, Update, Delete. These are the four basic operations of all software. In this module, we focus on the first two:
Rendering diagram…
The INSERT command adds new rows to a table. You should always specify the columns you are filling to keep your code stable.
INSERT INTO users (username, email, age)
VALUES ('alice_dev', 'alice@example.com', 25);Instead of running 100 commands, you can run one. This is much faster in production environments.
INSERT INTO users (username, email, age)
VALUES
('bob_codes', 'bob@example.com', 30),
('charlie_writes', 'charlie@example.com', 22),
('diana_dev', 'diana@example.com', 28);RETURNING ClauseIn modern app development, you often need the id of the row you just created. Postgres makes this easy with RETURNING.
INSERT INTO users (username, email)
VALUES ('new_user', 'new@example.com')
RETURNING id, created_at;SELECT is the most common command you will ever run. It tells the database: "Give me this specific data from this table."
While common in learning, SELECT * (Select All) is usually avoided in production because it can be slow if you have 100 columns.
SELECT * FROM users;Only request the data you actually need.
SELECT username, email FROM users;AS)You can rename the output columns for your reports or for your application's .
SELECT username AS user_handle, email AS contact_info
FROM users;You can do more than just "read" data; you can transform it as it's being retrieved.
-- Assuming a products table with price in cents
SELECT name, price / 100 AS price_in_dollars
FROM products;-- Combine first and last names (standard SQL uses ||)
SELECT first_name || ' ' || last_name AS full_name
FROM users;INSERT INTO users VALUES (...) is dangerous because if you add a new column to the table later, this command will break. Always list your columns.SELECT * can significantly slow down your application and consume unnecessary memory.Using the courses table you created in Module 3:
('SQL Basics', 'John Doe', 2999, true).RETURNING id to see the generated IDs of your new courses.SELECT query that returns the course title and the price divided by 100, renamed as price_in_rupees.Great Job! You've mastered half of CRUD. In the next module, we'll cover the "danger zone": Updating and Deleting data safely.