By the end of this tutorial, you’ll know how to write correct INSERT, UPDATE, and DELETE statements — and just as importantly, you’ll have the safety habits that prevent one of the most common, costly mistakes anyone writing SQL can make: changing or deleting far more data than you intended.
Early in my career, I ran an UPDATE statement meant to change one customer’s status, forgot the WHERE clause, and updated every single customer in the table to that same status instead. A backup built for exactly this kind of situation let me catch the error within about thirty seconds, but those thirty seconds still rank among the most uncomfortable moments of my career.
INSERT, UPDATE, and DELETE differ from everything covered in earlier tutorials in one fundamental way: SELECT only reads and displays data without changing anything, while these three commands modify your real data, permanently, the moment they run successfully. This tutorial walks through correct syntax for each — but more importantly, it covers the safety habits that keep a mistake like mine from happening to you.
INSERT: Adding New Rows
INSERT adds a brand-new row into a table. Its basic shape: start with INSERT INTO, name your table, list the specific columns you’re providing values for in parentheses, then write VALUES followed by the corresponding values in parentheses — in the same order as the column list.
Say you’re adding a new customer to a customers table with columns for name, email, and signup_date. That statement would look like: INSERT INTO customers, opening parenthesis, name, email, signup_date, closing parenthesis, VALUES, opening parenthesis, the actual name value, the actual email value, the actual date value, closing parenthesis.
One detail worth knowing: any column you leave out of your INSERT statement will get one of three outcomes — a predefined default value, if the column has one configured; NULL, if there’s no default but the column allows NULL; or an outright error, if the column is required and has neither a default nor NULL allowed. Knowing your table’s structure — which columns are required, which carry defaults — before you write the INSERT saves you from unexpected errors and from NULL values quietly showing up where you didn’t intend them.
Inserting multiple rows at once: instead of writing a separate INSERT statement for every row, most database systems let you list several parenthesized value sets, separated by commas, inside a single INSERT statement. It’s both shorter to write and typically faster for the database to process than running many individual statements back to back.
UPDATE: Changing Existing Data
UPDATE changes values in rows that already exist. Its basic shape: UPDATE, your table name, SET, the column you want to change along with its new value, then WHERE, followed by a condition that pins down exactly which rows should be affected.
For instance, updating one customer’s email address by their customer ID would read: UPDATE customers, SET email equals the new email value, WHERE customer ID equals that specific customer’s ID.
This is the exact command behind the mistake I mentioned at the start. The WHERE clause isn’t optional in any technical sense — SQL will run an UPDATE with no WHERE clause without complaint, and it will apply your change to every row in the table,