Nishaglobal Education logo

Creating tables and changing data safely

Create a SQL table with column types and constraints, add rows with INSERT, change them with UPDATE, remove them with DELETE, and undo mistakes with Revert Changes or ROLLBACK.

Lesson 12 of 1235 minQuiz: 4 questions

🎯 By the end of this lesson you will be able to:

  • Create a table with types, a primary key and constraints
  • Add, change and remove rows with INSERT, UPDATE and DELETE
  • Change data safely: check with SELECT first and know how to undo
Outline
  1. What you'll learn and why it matters
  2. Creating a table
  3. Adding rows with INSERT
  4. Changing rows with UPDATE
  5. Removing rows with DELETE
  6. How to undo
  7. Practice
  8. Common mistakes
  9. Recap
  10. Quiz
  11. Mark lesson complete

What you'll learn and why it matters

So far you have only read data. Apps also need to write it: a new customer signs up, a price changes, a test record is removed. These commands are powerful, and a careless one can change thousands of rows in a second. This lesson teaches the commands and the habits that keep you safe.

Creating a table

Our shop wants to collect product reviews. CREATE TABLE defines the new table's columns, their types and their rules (constraints):

SQL
CREATE TABLE reviews (
  review_id   INTEGER PRIMARY KEY,
  product_id  INTEGER NOT NULL REFERENCES products(product_id),
  rating      INTEGER NOT NULL CHECK (rating BETWEEN 1 AND 5),
  comment     TEXT
);
PartMeaning
INTEGER, TEXT, REALColumn types: whole numbers, text, decimals
PRIMARY KEYUnique id. In SQLite an INTEGER PRIMARY KEY is filled in automatically if you leave it out.
NOT NULLThis column must always have a value
REFERENCES products(product_id)A foreign key: this column points to a product
CHECK (rating BETWEEN 1 AND 5)Rejects any row whose rating is outside 1 to 5

Constraints protect your data: the database itself refuses bad rows, however they arrive.

Adding rows with INSERT

Name the columns, then give the values in the same order. You can add one row or several at once:

SQL
CREATE TABLE reviews (
  review_id   INTEGER PRIMARY KEY,
  product_id  INTEGER NOT NULL REFERENCES products(product_id),
  rating      INTEGER NOT NULL CHECK (rating BETWEEN 1 AND 5),
  comment     TEXT
);

INSERT INTO reviews (product_id, rating, comment)
VALUES (3, 5, 'Clear and friendly');

INSERT INTO reviews (product_id, rating, comment) VALUES
  (3, 4, 'Good examples'),
  (5, 3, NULL);

SELECT * FROM reviews;

Result: 3 rows

Query result
review_idproduct_idratingcomment
135Clear and friendly
234Good examples
353NULL

We left out review_id, so SQLite numbered the rows 1, 2 and 3. Try inserting a rating of 9: the CHECK constraint stops it with CHECK constraint failed.

Changing rows with UPDATE

UPDATE sets new values for the rows that match its WHERE. Always run a SELECT with the same WHERE first to see exactly which rows will change:

SQL
SELECT product_id, name, price FROM products WHERE product_id = 2;

UPDATE products
SET price = 99
WHERE product_id = 2;

SELECT product_id, name, price FROM products WHERE product_id = 2;

Result: 1 row

Query result
product_idnameprice
2Gel Pens (pack of 5)99

The first SELECT shows the old price (90), the UPDATE changes it, and the last SELECT confirms the new price. You can update many rows at once and use the old value in the calculation:

SQL
UPDATE products
SET price = ROUND(price * 0.9)
WHERE category = 'Books';

SELECT name, price FROM products WHERE category = 'Books';

Result: 2 rows

Query result
nameprice
Python Basics Book405
SQL Pocket Guide315

Removing rows with DELETE

SQL
DELETE FROM orders
WHERE status = 'cancelled';

SELECT status, COUNT(*) AS orders FROM orders GROUP BY status;

Result: 2 rows

Query result
statusorders
delivered10
shipped3

How to undo

In DB Browser for SQLite, changes are held as pending until you click Write Changes. If you make a mistake, click Revert Changes to throw away everything since you last wrote changes. This is your safety net while learning, so only click Write Changes when you have checked the result.

In the sqlite3 command line and most other tools, wrap risky changes in a transaction: BEGIN starts it, and ROLLBACK undoes everything since BEGIN (or COMMIT keeps it):

In the sqlite3 command-line program
sqlite> BEGIN;
sqlite> UPDATE products SET price = 0;
sqlite> SELECT name, price FROM products LIMIT 3;
Notebook A5|0.0
Gel Pens (pack of 5)|0.0
Python Basics Book|0.0
sqlite> ROLLBACK;
sqlite> SELECT name, price FROM products LIMIT 3;
Notebook A5|120.0
Gel Pens (pack of 5)|90.0
Python Basics Book|450.0

A safe routine for any change, in any tool:

  1. Write a SELECT with the WHERE you plan to use. Check the rows and the count.
  2. Change SELECT ... to UPDATE ... SET ... or DELETE, keeping the same WHERE.
  3. Run it, then run a SELECT to check the result.
  4. Only then save: Write Changes in DB Browser, or COMMIT in a transaction.

Practice

Guided exercise

A new customer

Priya Das from Kolkata, India (priya@example.com) joined on 2025-10-20. Add her as customer 11 and check she is there.

  1. Write INSERT INTO customers (customer_id, name, city, country, email, joined_on).
  2. Add VALUES (...) with text in single quotes, in the same order.
  3. Check with a SELECT of customers with an id over 9.
Show the solution
Solution
INSERT INTO customers (customer_id, name, city, country, email, joined_on)
VALUES (11, 'Priya Das', 'Kolkata', 'India', 'priya@example.com', '2025-10-20');

SELECT customer_id, name, country FROM customers WHERE customer_id > 9;

Result: 2 rows

Query result
customer_idnamecountry
10Kofi MensahGhana
11Priya DasIndia

How to get there: Listing the column names makes the INSERT clear and keeps working even if columns are added to the table later.

Your turn

Fix a missing email

Meera Nair (customer 3) has sent her email: meera@example.com. Update her record safely and confirm the change.

Show the solution
Solution
UPDATE customers
SET email = 'meera@example.com'
WHERE customer_id = 3;

SELECT name, email FROM customers WHERE customer_id = 3;

Result: 1 row

Query result
nameemail
Meera Nairmeera@example.com

How to get there: Filtering by the primary key guarantees exactly one row changes. Filtering by name = 'Meera Nair' would also update any other customer with the same name.

Common mistakes

UPDATE or DELETE without WHERE

You see: Every row changed; DB Browser shows a large number of rows affected

Fix: Click Revert Changes (or ROLLBACK), then add the WHERE.

Values in a different order from the columns

You see: Data in the wrong columns, or datatype mismatch

Fix: List the columns explicitly and match the values to them.

Inserting a duplicate primary key

You see: UNIQUE constraint failed: customers.customer_id

Fix: Use a new id, or leave an INTEGER PRIMARY KEY out so SQLite picks one.

Forgetting Write Changes in DB Browser

You see: Changes are lost when you close the database

Fix: Click Write Changes once you have checked the result.

Recap

  • CREATE TABLE defines columns, types and constraints such as PRIMARY KEY, NOT NULL and CHECK.
  • INSERT INTO table (columns) VALUES (...) adds rows; UPDATE ... SET ... WHERE changes them; DELETE FROM ... WHERE removes them.
  • Without WHERE, UPDATE and DELETE affect every row. Check with SELECT first.
  • Undo with Revert Changes in DB Browser, or BEGIN ... ROLLBACK in other tools.

Key terms

Constraint
A rule the database enforces on a column, such as NOT NULL or CHECK.
Transaction
A group of changes that are kept together (COMMIT) or undone together (ROLLBACK).

Check your understanding

Answer all 4 questions, then check your answers to see an explanation for each one.

  1. 1. What does `DELETE FROM orders;` do?

  2. 2. What is the safest first step before an UPDATE?

  3. 3. A `CHECK (rating BETWEEN 1 AND 5)` constraint exists. What happens when you insert rating 9?

  4. 4. You ran an UPDATE in DB Browser and it changed the wrong rows. You have not clicked Write Changes. What should you do?

Answer all 4 questions to check.

Read more

Mark this lesson complete

Finished this lesson?

When you have worked through the examples and exercises, mark it complete to save your progress.