DDL (for MyShop)
-
Create Schema:
CREATE SCHEMA myshop;CREATE SCHEMA: Creates a new schema in the database.- Creates a schema named
myshop.
-
Create Products Table:
CREATE TABLE myshop.products (
"product_id" BIGINT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
"name" varchar(50),
"quantity" integer DEFAULT 0
);CREATE TABLE: Creates a new table.BIGINT GENERATED BY DEFAULT AS IDENTITY: Creates an auto-incrementing column for unique identifiers.PRIMARY KEY: Defines the primary key for the table.DEFAULT: Sets a default value for the column.- Creates a
productstable withproduct_id,name, andquantity.
-
Create Categories Table:
CREATE TABLE myshop.categories (
"category_id" BIGINT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
"name" varchar(50)
);- Creates a
categoriestable withcategory_idandname.
- Creates a
-
Create Clients Table:
CREATE TABLE myshop.clients (
"client_id" BIGINT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
"name" varchar(50)
);- Creates a
clientstable withclient_idandname.
- Creates a
-
Create Orders Table:
CREATE TABLE myshop.orders (
"order_id" bigint PRIMARY KEY,
"client_id" bigint,
"order_date" date DEFAULT (now())
);now(): A function that returns the current date and time.- Creates an
orderstable withorder_id,client_id, andorder_date.
-
Create Product_Category Table:
CREATE TABLE myshop.product_category (
"product_id" bigint,
"category_id" bigint,
PRIMARY KEY ("product_id", "category_id")
);PRIMARY KEY ("product_id", "category_id"): Defines a composite primary key.- Creates a
product_categorytable to associate products with categories usingproduct_idandcategory_id.
-
Create Ordered_Products Table:
CREATE TABLE myshop.ordered_products (
"product_id" bigint,
"order_id" bigint,
"quantity" integer,
PRIMARY KEY ("product_id", "order_id")
);- Creates an
ordered_productstable to track ordered products usingproduct_id,order_id, andquantity.
- Creates an
-
Add Foreign Key to Orders Table:
ALTER TABLE myshop.orders ADD FOREIGN KEY ("client_id") REFERENCES myshop.clients ("client_id");ALTER TABLE: Modifies an existing table.ADD FOREIGN KEY: Adds a foreign key constraint to a column.- Adds a foreign key constraint on
client_idin theorderstable referencingclient_idin theclientstable.
-
Add Foreign Key to Product_Category Table (Category ID):
ALTER TABLE myshop.product_category ADD FOREIGN KEY ("category_id") REFERENCES myshop.categories ("category_id");- Adds a foreign key constraint on
category_idin theproduct_categorytable referencingcategory_idin thecategoriestable.
- Adds a foreign key constraint on
-
Add Foreign Key to Product_Category Table (Product ID):
ALTER TABLE myshop.product_category ADD FOREIGN KEY ("product_id") REFERENCES myshop.products ("product_id");- Adds a foreign key constraint on
product_idin theproduct_categorytable referencingproduct_idin theproductstable.
- Adds a foreign key constraint on
-
Add Foreign Key to Ordered_Products Table (Order ID):
ALTER TABLE myshop.ordered_products ADD FOREIGN KEY ("order_id") REFERENCES myshop.orders ("order_id");- Adds a foreign key constraint on
order_idin theordered_productstable referencingorder_idin theorderstable.
- Adds a foreign key constraint on
-
Add Foreign Key to Ordered_Products Table (Product ID):
ALTER TABLE myshop.ordered_products ADD FOREIGN KEY ("product_id") REFERENCES myshop.products ("product_id");- Adds a foreign key constraint on
product_idin theordered_productstable referencingproduct_idin theproductstable.
- Adds a foreign key constraint on