This commit is contained in:
Imbus 2024-02-02 07:27:13 +01:00
parent 0abe31abf0
commit 371eb7e8b3
7 changed files with 140 additions and 9 deletions

2
.gitignore vendored
View file

@ -1 +1 @@
lab.db
db.sqlite

View file

View file

View file

@ -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."

67
scripts/001.sql Normal file
View file

@ -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);

44
scripts/002.sql Normal file
View file

@ -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);

14
scripts/003.sql Normal file
View file

@ -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;