Creating a Relational Database
In the previous sessions, we've explored how to represent an ER model in the form of tables in a relational database.
Now, let's create tables to store the data in the database by defining all the columns and relationships between the tables.
Consider the e-commerce scenario. The tables, columns and the relations between them are s follows.
Primary Key
Following syntax creates a table with
c1
as the primary key.Syntax
SQL
Foreign Key
In case of foreign key, we just create a foreign key constraint.
Syntax
SQL
Understanding
SQL
Above part of the foreign key constraint ensure that foreign key can only contain values that are in the referenced primary key.
SQL
Ensure that if a row in
table1
is deleted, then all its related rows intable2
will also be deleted.Note
To enable foreign key constraints in SQLite, use
PRAGMA foreign_keys = ON;
By default it is enabled in our platform!Creating Tables in Relational Database
Customer Table
SQL
Product Table
SQL
Address Table
SQL
Cart Table
SQL
Cart Product Table (Junction Table)
SQ