Creating a table with CREATE TABLE #
So far we have only worked with tables that were already there for us (customers, products, orders, order_items). The CREATE TABLE statement is how you build a brand new table from scratch:
CREATE TABLE table_name (
column_1,
column_2,
column_3
);
table_name: The name of the new table.column_1, column_2, column_3: The names of the columns the table will have.
Every row inserted into this table will have these columns, in this order. In the next lessons you will learn how to give each column a data type and extra rules (like requiring a value, or being unique).
Exercise #
Write a command that creates a new table called reviews with the columns id, product_id, rating, and comment.
Tests #
- A table named
reviewsexists. - The
reviewstable has the columnsid,product_id,rating, andcomment.
Solution
CREATE TABLE reviews (
id,
product_id,
rating,
comment
);
Basic Data Retrieval
Filtering Data
Aggregating Data
Joining Tables
Creating and Managing Tables
Transactions and Data Integrity