Start learning free

LESSON 53 OF 173 · BACKEND: SQL

Create a Table in C#

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
)

In C#, you write query as short strings, one per line, joined with +. C# glues them into one string, and that string is the SQL. 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 refuses the query with a syntax error near ) and the program stops before printing anything: a table needs at least one column, and every comma must be followed by another column. Keep each comma inside the quotes, and put a + after every string but the last one.


STEP 1 OF 4
    Ctrl + Enter to check

    Sql Create Table in C#

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