CREATE DATABASE "2025_5bhitn_konstantin_hintermayer_smarter_rentDB";
GO
USE "2025_5bhitn_konstantin_hintermayer_smarter_rentDB";
GO

DROP TABLE IF EXISTS Rental;

DROP TABLE IF EXISTS Customer;

DROP TABLE IF EXISTS [Location];

DROP TABLE IF EXISTS Vehicle;

DROP TABLE IF EXISTS VehicleCategory;

CREATE TABLE Customer (
    id BIGINT NOT NULL IDENTITY (1, 1),
    [name] VARCHAR(50) NOT NULL,
    Adress VARCHAR(100) NOT NULL,
    driverlicenseid VARCHAR(100) NOT NULL,
    PRIMARY KEY (id)
);

CREATE TABLE Location (
    id BIGINT NOT NULL IDENTITY (1, 1),
    [name] VARCHAR(250) NOT NULL,
    [address] VARCHAR(250) NOT NULL,
    PRIMARY KEY (id)
);

CREATE TABLE Rental (
    id BIGINT NOT NULL IDENTITY (1, 1),
    rentalStart DATETIME NOT NULL,
    rentalEnd DATETIME NOT NULL,
    actualEnd DATETIME NULL,
    customerID BIGINT NOT NULL,
    vehicleID BIGINT NOT NULL,
    PRIMARY KEY (id)
);

CREATE TABLE Vehicle (
    id BIGINT NOT NULL IDENTITY (1, 1),
    brand VARCHAR(50) NOT NULL,
    model VARCHAR(50) NOT NULL,
    licensePlate VARCHAR(8) NOT NULL,
    buyDate DATE NOT NULL,
    locationID BIGINT NOT NULL,
    vehicleCategoryID BIGINT NOT NULL,
    PRIMARY KEY (id)
);

CREATE TABLE VehicleCategory (
    id BIGINT NOT NULL IDENTITY (1, 1),
    [description] VARCHAR(250) NOT NULL,
    pricePerDay DECIMAL NOT NULL,
    PRIMARY KEY (id)
);

ALTER TABLE Rental
ADD CONSTRAINT FK_Customer_TO_Rental FOREIGN KEY (customerID) REFERENCES Customer (id);

ALTER TABLE Rental
ADD CONSTRAINT FK_Vehicle_TO_Rental FOREIGN KEY (vehicleID) REFERENCES Vehicle (id);

ALTER TABLE Vehicle
ADD CONSTRAINT FK_Location_TO_Vehicle FOREIGN KEY (locationID) REFERENCES Location (id);

ALTER TABLE Vehicle
ADD CONSTRAINT FK_VehicleCategory_TO_Vehicle FOREIGN KEY (vehicleCategoryID) REFERENCES VehicleCategory (id);