Skip to content

Creating Tables & Managing Data

1 min read3 lessons in topic

Learning objective

By the end of this lesson you will be able to create tables and add, update and delete rows with SQL.

Beyond reading data, SQL lets you build tables and keep them up to date.

Creating a table

CREATE TABLE courses (
  id INT PRIMARY KEY,
  title VARCHAR(100),
  credits INT
);

Inserting rows

INSERT INTO courses (id, title, credits)
VALUES (1, 'Database Systems', 3),
       (2, 'Web Development', 3);

Updating rows

UPDATE courses
SET credits = 4
WHERE id = 1;

UPDATE changes existing rows; the WHERE clause limits which ones.

Deleting rows

DELETE FROM courses WHERE id = 2;
  • CREATE TABLE — define a new table
  • INSERT INTO — add rows
  • UPDATE … SET — change rows
  • DELETE FROM — remove rows
  • Always use WHERE with UPDATE/DELETE unless you really mean every row

Practice exercise

Create a table for books (id, title, author, year) and insert three books of your choice.

Check your understanding

1. Which command adds a new row to a table?

2. What happens if you run DELETE FROM courses without WHERE?

Self-graded — answer then check below.