adding codes

This commit is contained in:
2026-06-22 23:19:56 +03:30
parent 00f990c653
commit cc82879fcc
22 changed files with 796 additions and 155 deletions
+125 -124
View File
@@ -1,141 +1,142 @@
-- Restaurant Database Management System
--
-- Instructions:
-- 1. Create all required tables.
-- 2. Design appropriate PRIMARY KEY and FOREIGN KEY relationships.
-- 3. Add suitable constraints based on the requirements.
-- 4. Insert initial mock data.
-- 5. Insert at least 3 menu items.
-- 6. The script should be executable from start to finish without errors.
-- =========================================================
-- WS 10 - Restaurant Database Management System
-- database.sql
-- =========================================================
-- Drop tables if they already exist (useful when re-running the script)
DROP TABLE IF EXISTS order_details;
DROP TABLE IF EXISTS orders;
DROP TABLE IF EXISTS menu_items;
DROP TABLE IF EXISTS users;
-- =======================================================
-- USER TABLE
-- =======================================================
--
-- Represents customers using the system.
--
-- Required information:
-- - Unique identifier
-- - Username
-- - Password
-- - Email (optional)
--
-- Requirements:
-- - Each user must have a unique identifier.
-- - Usernames must be unique.
-- - Username and password are required.
-- - Passwords should not be stored in plain text.
--
-- CREATE TABLE ...
-- =========================================================
-- 1. USERS (Customer)
-- =========================================================
CREATE TABLE users (
id BIGSERIAL NOT NULL PRIMARY KEY,
username VARCHAR(50) NOT NULL UNIQUE,
password_hash VARCHAR(255) NOT NULL,
email VARCHAR(100)
);
-- =========================================================
-- 2. MENU_ITEMS
-- =========================================================
CREATE TABLE menu_items (
id BIGSERIAL NOT NULL PRIMARY KEY,
name VARCHAR(100) NOT NULL,
description VARCHAR(255),
price NUMERIC(10, 2) NOT NULL CHECK (price > 0),
category VARCHAR(50)
);
-- =========================================================
-- 3. ORDERS
-- =========================================================
CREATE TABLE orders (
id BIGSERIAL NOT NULL PRIMARY KEY,
user_id BIGINT NOT NULL REFERENCES users (id),
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
total_price NUMERIC(10, 2) NOT NULL CHECK (total_price >= 0)
);
-- =======================================================
-- MENU ITEM TABLE
-- =======================================================
--
-- Represents available food and drink items.
--
-- Required information:
-- - Unique identifier
-- - Name
-- - Description (optional)
-- - Price
-- - Category (optional)
--
-- Requirements:
-- - Each menu item must have a unique identifier.
-- - Name is required.
-- - Price must always be positive.
--
-- CREATE TABLE ...
-- =========================================================
-- 4. ORDER_DETAILS (items inside an order)
-- =========================================================
CREATE TABLE order_details (
id BIGSERIAL NOT NULL PRIMARY KEY,
order_id BIGINT NOT NULL REFERENCES orders (id),
menu_item_id BIGINT NOT NULL REFERENCES menu_items (id),
quantity INT NOT NULL CHECK (quantity > 0),
unit_price NUMERIC(10, 2) NOT NULL CHECK (unit_price > 0)
);
-- =========================================================
-- Initial mock data
-- =========================================================
-- Sample users
-- NOTE: these password_hash values are just placeholders.
-- Your Java app must hash real passwords before inserting (e.g. with BCrypt).
INSERT INTO users (username, password_hash, email) VALUES
('john_doe', '$2a$10$placeholderHashValue1234567890', 'john@example.com'),
('jane_smith', '$2a$10$placeholderHashValue1234567891', 'jane@example.com');
-- =======================================================
-- ORDER TABLE
-- =======================================================
--
-- Represents orders placed by customers.
--
-- Required information:
-- - Unique identifier
-- - Reference to customer
-- - Creation date and time
-- - Total price
--
-- Requirements:
-- - Each order must belong to exactly one user.
-- - A user can have multiple orders.
-- - The relationship between User and Order must be implemented.
--
-- Note:
-- Avoid using reserved SQL keywords as table names.
-- Consider using a name such as "orders" or "customer_orders".
--
-- CREATE TABLE ...
-- Sample menu items (at least 3 required)
INSERT INTO menu_items (name, description, price, category) VALUES
('Pizza Margherita', 'Classic pizza with tomato, mozzarella and basil', 10.00, 'Main'),
('Cheeseburger', 'Beef patty with cheddar cheese and house sauce', 8.00, 'Main'),
('Pasta Carbonara', 'Pasta with egg, pancetta and parmesan', 12.00, 'Main'),
('Tiramisu', 'Traditional Italian coffee-flavored dessert', 6.50, 'Dessert'),
('Cola', 'Soft drink, 330ml can', 2.50, 'Drink');-- =========================================================
-- WS 10 - Restaurant Database Management System
-- database.sql
-- =========================================================
-- Drop tables if they already exist (useful when re-running the script)
DROP TABLE IF EXISTS order_details;
DROP TABLE IF EXISTS orders;
DROP TABLE IF EXISTS menu_items;
DROP TABLE IF EXISTS users;
-- =========================================================
-- 1. USERS (Customer)
-- =========================================================
CREATE TABLE users (
id BIGSERIAL NOT NULL PRIMARY KEY,
username VARCHAR(50) NOT NULL UNIQUE,
password_hash VARCHAR(255) NOT NULL,
email VARCHAR(100)
);
-- =======================================================
-- ORDER DETAIL TABLE
-- =======================================================
--
-- Represents items inside an order.
--
-- Required information:
-- - Unique identifier
-- - Reference to an order
-- - Reference to a menu item
-- - Quantity
-- - Item price at purchase time
--
-- Requirements:
-- - Each detail record must belong to one order.
-- - Each detail record must reference one menu item.
-- - Quantity must always be greater than zero.
-- - Store the item's price at the moment of purchase.
--
-- CREATE TABLE ...
-- =========================================================
-- 2. MENU_ITEMS
-- =========================================================
CREATE TABLE menu_items (
id BIGSERIAL NOT NULL PRIMARY KEY,
name VARCHAR(100) NOT NULL,
description VARCHAR(255),
price NUMERIC(10, 2) NOT NULL CHECK (price > 0),
category VARCHAR(50)
);
-- =========================================================
-- 3. ORDERS
-- =========================================================
CREATE TABLE orders (
id BIGSERIAL NOT NULL PRIMARY KEY,
user_id BIGINT NOT NULL REFERENCES users (id),
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
total_price NUMERIC(10, 2) NOT NULL CHECK (total_price >= 0)
);
-- =========================================================
-- 4. ORDER_DETAILS (items inside an order)
-- =========================================================
CREATE TABLE order_details (
id BIGSERIAL NOT NULL PRIMARY KEY,
order_id BIGINT NOT NULL REFERENCES orders (id),
menu_item_id BIGINT NOT NULL REFERENCES menu_items (id),
quantity INT NOT NULL CHECK (quantity > 0),
unit_price NUMERIC(10, 2) NOT NULL CHECK (unit_price > 0)
);
-- =======================================================
-- INITIAL MENU DATA
-- =======================================================
--
-- Insert at least 3 food or drink items.
--
-- Example categories:
-- - Pizza
-- - Burger
-- - Pasta
-- - Drink
--
-- INSERT INTO ...
-- =========================================================
-- Initial mock data
-- =========================================================
-- Sample users
-- NOTE: these password_hash values are just placeholders.
-- Your Java app must hash real passwords before inserting (e.g. with BCrypt).
INSERT INTO users (username, password_hash, email) VALUES
('john_doe', '$2a$10$placeholderHashValue1234567890', 'john@example.com'),
('jane_smith', '$2a$10$placeholderHashValue1234567891', 'jane@example.com');
-- =======================================================
-- OPTIONAL TEST DATA
-- =======================================================
--
-- You may insert sample users and orders for testing.
-- This section is optional.
--
-- INSERT INTO ...
-- =======================================================
-- VERIFICATION QUERIES
-- =======================================================
--
-- Uncomment these queries to verify your database.
--
-- SELECT * FROM ...;
-- SELECT * FROM ...;
-- SELECT * FROM ...;
-- SELECT * FROM ...;
-- Sample menu items (at least 3 required)
INSERT INTO menu_items (name, description, price, category) VALUES
('Pizza Margherita', 'Classic pizza with tomato, mozzarella and basil', 10.00, 'Main'),
('Cheeseburger', 'Beef patty with cheddar cheese and house sauce', 8.00, 'Main'),
('Pasta Carbonara', 'Pasta with egg, pancetta and parmesan', 12.00, 'Main'),
('Tiramisu', 'Traditional Italian coffee-flavored dessert', 6.50, 'Dessert'),
('Cola', 'Soft drink, 330ml can', 2.50, 'Drink');