Home › Learn › SQL › Changing data, and the report

CREATE TABLE

Lesson 24 of 26. Define a new table and put a row in it.

CREATE TABLE reviews (id INTEGER PRIMARY KEY, product_id INTEGER NOT NULL, rating INTEGER NOT NULL, comment TEXT); defines a table with its columns and their types. INTEGER PRIMARY KEY makes an auto-numbered identifier; NOT NULL refuses missing values; TEXT, INTEGER, and REAL are the common types. REFERENCES products(id) on a column declares a foreign key.

Statements run one after another, so a CREATE TABLE followed by an INSERT into it works in a single run.

CREATE TABLE tags (id INTEGER PRIMARY KEY, label TEXT NOT NULL);
INSERT INTO tags (label) VALUES ('sale');

Your task

Create a reviews table with columns id (integer primary key), product_id (integer, not null), rating (integer, not null), and comment (text). Then insert one review for product 1 with rating 5 and the comment Great keys.

Hint 1

id INTEGER PRIMARY KEY, product_id INTEGER NOT NULL, rating INTEGER NOT NULL, comment TEXT

Hint 2

VALUES (1, 5, 'Great keys')

Show the solution
CREATE TABLE reviews (
  id INTEGER PRIMARY KEY,
  product_id INTEGER NOT NULL,
  rating INTEGER NOT NULL,
  comment TEXT
);

INSERT INTO reviews (product_id, rating, comment) VALUES (1, 5, 'Great keys');

    Runs SQLite compiled to WebAssembly in this tab; the database is rebuilt before every run.