diff --git a/.gitignore b/.gitignore index 524299b..b27712d 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1 @@ -lab.db +db.sqlite \ No newline at end of file diff --git a/001.sql b/001.sql deleted file mode 100644 index e69de29..0000000 diff --git a/002.sql b/002.sql deleted file mode 100644 index e69de29..0000000 diff --git a/makefile b/makefile index 89f536a..08f0763 100644 --- a/makefile +++ b/makefile @@ -1,14 +1,20 @@ -DB_NAME := lab.db -SQL_FILES := 001.sql 002.sql +DB_FILE := db.sqlite +SCRIPTS_DIR := scripts +SQL_SCRIPTS := $(wildcard $(SCRIPTS_DIR)/*.sql) .PHONY: all clean -all: $(SQL_FILES:.sql=.db) +all: $(DB_FILE) -%.db: %.sql - sqlite3 $(DB_NAME) < $< - @echo ============================== +$(DB_FILE): $(SQL_SCRIPTS) + @echo "Creating database: $@" + @for script in $^; do \ + echo "Running script: $$script"; \ + sqlite3 $@ < $$script; \ + done + @echo "Database creation completed." clean: - rm -f $(DB_NAME) - + @echo "Cleaning up" + @rm -f $(DB_FILE) + @echo "Cleanup completed." diff --git a/scripts/001.sql b/scripts/001.sql new file mode 100644 index 0000000..02d9a6b --- /dev/null +++ b/scripts/001.sql @@ -0,0 +1,67 @@ +PRAGMA foreign_keys = OFF; + +-- Drop the tables if they exist +DROP TABLE IF EXISTS Reservations; +DROP TABLE IF EXISTS Shows; +DROP TABLE IF EXISTS Movies; +DROP TABLE IF EXISTS Theaters; +DROP TABLE IF EXISTS Users; + +PRAGMA foreign_keys = ON; + +-- Create the Users table +CREATE TABLE + IF NOT EXISTS Users ( + username VARCHAR(50) PRIMARY KEY, + name VARCHAR(100) NOT NULL, + address VARCHAR(200), + telephone VARCHAR(20) NOT NULL + ); + +-- Create the Theaters table +CREATE TABLE + IF NOT EXISTS Theaters ( + theater_id INTEGER PRIMARY KEY, + name VARCHAR(100) NOT NULL, + seats INTEGER NOT NULL + ); + +-- Create the Movies table +CREATE TABLE + IF NOT EXISTS Movies ( + movie_id INTEGER PRIMARY KEY, + name VARCHAR(100) NOT NULL + ); + +-- Create the Shows table +CREATE TABLE + IF NOT EXISTS Shows ( + show_id INTEGER PRIMARY KEY, + movie_id INTEGER NOT NULL, + theater_id INTEGER NOT NULL, + show_date DATE NOT NULL, + FOREIGN KEY (movie_id) REFERENCES Movies (movie_id), + FOREIGN KEY (theater_id) REFERENCES Theaters (theater_id) + ); + +-- Create the Reservations table +CREATE TABLE + IF NOT EXISTS Reservations ( + reservation_id INTEGER PRIMARY KEY, + username VARCHAR(50) NOT NULL, + show_id INTEGER NOT NULL, + reservation_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP, + FOREIGN KEY (username) REFERENCES Users (username), + FOREIGN KEY (show_id) REFERENCES Shows (show_id) + ); + +-- Index for Users table +CREATE INDEX IF NOT EXISTS idx_users_username ON Users (username); + +-- Index for Shows table +CREATE INDEX IF NOT EXISTS idx_shows_movie_id ON Shows (movie_id); +CREATE INDEX IF NOT EXISTS idx_shows_theater_id ON Shows (theater_id); + +-- Index for Reservations table +CREATE INDEX IF NOT EXISTS idx_reservations_username ON Reservations (username); +CREATE INDEX IF NOT EXISTS idx_reservations_show_id ON Reservations (show_id); diff --git a/scripts/002.sql b/scripts/002.sql new file mode 100644 index 0000000..a9a70fc --- /dev/null +++ b/scripts/002.sql @@ -0,0 +1,44 @@ +-- Insert sample data into the Users table +INSERT +OR IGNORE INTO Users (username, name, address, telephone) +VALUES + ('john_doe', 'John Doe', '123 Main St', '555-1234'), + ( + 'jane_smith', + 'Jane Smith', + '456 Elm St', + '555-5678' + ); + +-- Insert sample data into the Theaters table +INSERT +OR IGNORE INTO Theaters (theater_id, name, seats) +VALUES + (1, 'Theater A', 100), + (2, 'Theater B', 150); + +-- Insert sample data into the Movies table +INSERT +OR IGNORE INTO Movies (movie_id, name) +VALUES + (1, 'Movie X'), + (2, 'Movie Y'); + +-- Insert sample data into the Shows table +INSERT +OR IGNORE INTO Shows (show_id, movie_id, theater_id, show_date) +VALUES + (1, 1, 1, '2022-01-01'), + (2, 2, 2, '2022-01-02'); + +-- Insert sample data into the Reservations table +INSERT +OR IGNORE INTO Reservations ( + reservation_id, + username, + show_id, + reservation_date +) +VALUES + (1, 'john_doe', 1, CURRENT_TIMESTAMP), + (2, 'jane_smith', 2, CURRENT_TIMESTAMP); \ No newline at end of file diff --git a/scripts/003.sql b/scripts/003.sql new file mode 100644 index 0000000..9273982 --- /dev/null +++ b/scripts/003.sql @@ -0,0 +1,14 @@ +-- Select everything from the Users table +SELECT * FROM Users; + +-- Select everything from the Theaters table +SELECT * FROM Theaters; + +-- Select everything from the Movies table +SELECT * FROM Movies; + +-- Select everything from the Shows table +SELECT * FROM Shows; + +-- Select everything from the Reservations table +SELECT * FROM Reservations; \ No newline at end of file