Start learning free

LESSON 53 OF 188 · BACKEND: SQL

Create a Table in Python

Practice

So far your queries have read and changed tables that already existed. CREATE TABLE makes a new one. You give it a name and a list of columns, and it starts with no rows: those come later, with INSERT.

Each column is a name followed by its type, the kind of value it holds: INTEGER for whole numbers, TEXT for words. The columns sit inside parentheses with commas between them, so there is no comma after the last one. A table of discount coupons, for example:

CREATE TABLE coupons (
    code TEXT,
    percent INTEGER
)

The shop wants to keep its customers' reviews: which product, how many stars, and what the customer wrote. Once your table exists, the check lists its columns with their types, then adds two reviews and reads them back:

name | type
id | INTEGER
product_id | INTEGER
stars | INTEGER
text | TEXT
(4 rows)
id | product_id | stars | text
1 | 1 | 5 | Great mug
2 | 3 | 4 | Nice light
(2 rows)

Your task: write a query that creates the table reviews with four columns, in this order: id, product_id and stars, all INTEGER, then text, a TEXT column.


Assignment
Follow the Coding Tutorial to create the reviews table, one column per line.


Hint
Until the last column is in, SQLite stops with near ")": syntax error: a table needs at least one column, and every comma must be followed by another column. Keep each comma inside the quotes: a comma after a closing quote makes Python build a tuple instead of one string.


STEP 1 OF 4
    Ctrl + Enter to check

    Sql Create Table in Python

    Keep going with Python1,600+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free