dblabs2/scripts/001.sql
2024-02-02 07:27:13 +01:00

67 lines
1.9 KiB
SQL

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