PostgreSQLIntermediate
TechShop — সম্পূর্ণ schema (শিক্ষার project)
আমাদের পুরো ডিজাইন এক নজরে — users ⟫ orders ⟫ items।
4টি অংশবাংলা ভাষায়
CREATE TABLE categories (
id bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
name text NOT NULL UNIQUE
);
CREATE TABLE users (
id bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
email text NOT NULL UNIQUE,
name text NOT NULL,
role text NOT NULL DEFAULT 'customer'
);
CREATE TABLE products (
id bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
category_id bigint REFERENCES categories(id),
name text NOT NULL,
price numeric(10,2) NOT NULL,
qty integer NOT NULL DEFAULT 0,
spec jsonb
);
CREATE TABLE orders (
id bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
user_id bigint NOT NULL REFERENCES users(id),
total numeric(10,2) NOT NULL DEFAULT 0,
status text NOT NULL DEFAULT 'pending',
created_at timestamptz DEFAULT now()
);
CREATE TABLE order_items (
order_id bigint NOT NULL REFERENCES orders(id),
product_id bigint NOT NULL REFERENCES products(id),
qty integer NOT NULL,
price numeric(10,2) NOT NULL,
PRIMARY KEY (order_id, product_id)
);এই schema-তে order_items bridge table — order-এর সঙ্গে product-এর many-to-many। orders.user_id — user-এর সাথে one-to-many।
- প্রতিটি নন-empty relational column-এ FK + NOT NULL
- money হিসাব
numeric priceorder_items-এ copy — product-এর দাম বদলালেও ইতিহাস ঠিক থাকে
index এখনই
যেকোনো dropdown/FK-তে index: users(id) auto, orders(user_id) — যেটা JOIN-এ ভারী, এখনই CREATE INDEX দিন।
শেষ প্যারায় নিজে practice করুন — কোড কপি করে নিজের এডিটরে চালিয়ে দেখুন।