Skip to content

Instantly share code, notes, and snippets.

@yuzefovich
Created November 19, 2023 22:28
Show Gist options
  • Save yuzefovich/35c7e5b6f2dd326c80b1e5210b8a62c0 to your computer and use it in GitHub Desktop.
Save yuzefovich/35c7e5b6f2dd326c80b1e5210b8a62c0 to your computer and use it in GitHub Desktop.
SELECT pg_catalog.setval('public.actor_actor_id_seq', 200, true);
SELECT pg_catalog.setval('public.address_address_id_seq', 605, true);
SELECT pg_catalog.setval('public.category_category_id_seq', 16, true);
SELECT pg_catalog.setval('public.city_city_id_seq', 600, true);
SELECT pg_catalog.setval('public.country_country_id_seq', 109, true);
SELECT pg_catalog.setval('public.customer_customer_id_seq', 599, true);
SELECT pg_catalog.setval('public.film_film_id_seq', 1000, true);
SELECT pg_catalog.setval('public.inventory_inventory_id_seq', 4581, true);
SELECT pg_catalog.setval('public.language_language_id_seq', 6, true);
SELECT pg_catalog.setval('public.payment_payment_id_seq', 32098, true);
SELECT pg_catalog.setval('public.rental_rental_id_seq', 16049, true);
SELECT pg_catalog.setval('public.staff_staff_id_seq', 2, true);
SELECT pg_catalog.setval('public.store_store_id_seq', 2, true);
ALTER TABLE ONLY public.actor
ADD CONSTRAINT actor_pkey PRIMARY KEY (actor_id);
ALTER TABLE ONLY public.address
ADD CONSTRAINT address_pkey PRIMARY KEY (address_id);
ALTER TABLE ONLY public.category
ADD CONSTRAINT category_pkey PRIMARY KEY (category_id);
ALTER TABLE ONLY public.city
ADD CONSTRAINT city_pkey PRIMARY KEY (city_id);
ALTER TABLE ONLY public.country
ADD CONSTRAINT country_pkey PRIMARY KEY (country_id);
ALTER TABLE ONLY public.customer
ADD CONSTRAINT customer_pkey PRIMARY KEY (customer_id);
ALTER TABLE ONLY public.film_actor
ADD CONSTRAINT film_actor_pkey PRIMARY KEY (actor_id, film_id);
ALTER TABLE ONLY public.film_category
ADD CONSTRAINT film_category_pkey PRIMARY KEY (film_id, category_id);
ALTER TABLE ONLY public.film
ADD CONSTRAINT film_pkey PRIMARY KEY (film_id);
ALTER TABLE ONLY public.inventory
ADD CONSTRAINT inventory_pkey PRIMARY KEY (inventory_id);
ALTER TABLE ONLY public.language
ADD CONSTRAINT language_pkey PRIMARY KEY (language_id);
ALTER TABLE ONLY public.payment
ADD CONSTRAINT payment_pkey PRIMARY KEY (payment_id);
ALTER TABLE ONLY public.rental
ADD CONSTRAINT rental_pkey PRIMARY KEY (rental_id);
ALTER TABLE ONLY public.staff
ADD CONSTRAINT staff_pkey PRIMARY KEY (staff_id);
ALTER TABLE ONLY public.store
ADD CONSTRAINT store_pkey PRIMARY KEY (store_id);
CREATE INDEX film_fulltext_idx ON public.film USING gist (fulltext);
CREATE INDEX idx_actor_last_name ON public.actor USING btree (last_name);
CREATE INDEX idx_fk_address_id ON public.customer USING btree (address_id);
CREATE INDEX idx_fk_city_id ON public.address USING btree (city_id);
CREATE INDEX idx_fk_country_id ON public.city USING btree (country_id);
CREATE INDEX idx_fk_customer_id ON public.payment USING btree (customer_id);
CREATE INDEX idx_fk_film_id ON public.film_actor USING btree (film_id);
CREATE INDEX idx_fk_inventory_id ON public.rental USING btree (inventory_id);
CREATE INDEX idx_fk_language_id ON public.film USING btree (language_id);
CREATE INDEX idx_fk_rental_id ON public.payment USING btree (rental_id);
CREATE INDEX idx_fk_staff_id ON public.payment USING btree (staff_id);
CREATE INDEX idx_fk_store_id ON public.customer USING btree (store_id);
CREATE INDEX idx_last_name ON public.customer USING btree (last_name);
CREATE INDEX idx_store_id_film_id ON public.inventory USING btree (store_id, film_id);
CREATE INDEX idx_title ON public.film USING btree (title);
CREATE UNIQUE INDEX idx_unq_manager_staff_id ON public.store USING btree (manager_staff_id);
CREATE UNIQUE INDEX idx_unq_rental_rental_date_inventory_id_customer_id ON public.rental USING btree (rental_date, inventory_id, customer_id);
ALTER TABLE ONLY public.customer
ADD CONSTRAINT customer_address_id_fkey FOREIGN KEY (address_id) REFERENCES public.address(address_id) ON UPDATE CASCADE ON DELETE RESTRICT;
ALTER TABLE ONLY public.film_actor
ADD CONSTRAINT film_actor_actor_id_fkey FOREIGN KEY (actor_id) REFERENCES public.actor(actor_id) ON UPDATE CASCADE ON DELETE RESTRICT;
ALTER TABLE ONLY public.film_actor
ADD CONSTRAINT film_actor_film_id_fkey FOREIGN KEY (film_id) REFERENCES public.film(film_id) ON UPDATE CASCADE ON DELETE RESTRICT;
ALTER TABLE ONLY public.film_category
ADD CONSTRAINT film_category_category_id_fkey FOREIGN KEY (category_id) REFERENCES public.category(category_id) ON UPDATE CASCADE ON DELETE RESTRICT;
ALTER TABLE ONLY public.film_category
ADD CONSTRAINT film_category_film_id_fkey FOREIGN KEY (film_id) REFERENCES public.film(film_id) ON UPDATE CASCADE ON DELETE RESTRICT;
ALTER TABLE ONLY public.film
ADD CONSTRAINT film_language_id_fkey FOREIGN KEY (language_id) REFERENCES public.language(language_id) ON UPDATE CASCADE ON DELETE RESTRICT;
ALTER TABLE ONLY public.address
ADD CONSTRAINT fk_address_city FOREIGN KEY (city_id) REFERENCES public.city(city_id);
ALTER TABLE ONLY public.city
ADD CONSTRAINT fk_city FOREIGN KEY (country_id) REFERENCES public.country(country_id);
ALTER TABLE ONLY public.inventory
ADD CONSTRAINT inventory_film_id_fkey FOREIGN KEY (film_id) REFERENCES public.film(film_id) ON UPDATE CASCADE ON DELETE RESTRICT;
ALTER TABLE ONLY public.payment
ADD CONSTRAINT payment_customer_id_fkey FOREIGN KEY (customer_id) REFERENCES public.customer(customer_id) ON UPDATE CASCADE ON DELETE RESTRICT;
ALTER TABLE ONLY public.payment
ADD CONSTRAINT payment_staff_id_fkey FOREIGN KEY (staff_id) REFERENCES public.staff(staff_id) ON UPDATE CASCADE ON DELETE RESTRICT;
ALTER TABLE ONLY public.rental
ADD CONSTRAINT rental_customer_id_fkey FOREIGN KEY (customer_id) REFERENCES public.customer(customer_id) ON UPDATE CASCADE ON DELETE RESTRICT;
ALTER TABLE ONLY public.rental
ADD CONSTRAINT rental_inventory_id_fkey FOREIGN KEY (inventory_id) REFERENCES public.inventory(inventory_id) ON UPDATE CASCADE ON DELETE RESTRICT;
ALTER TABLE ONLY public.rental
ADD CONSTRAINT rental_staff_id_key FOREIGN KEY (staff_id) REFERENCES public.staff(staff_id);
ALTER TABLE ONLY public.staff
ADD CONSTRAINT staff_address_id_fkey FOREIGN KEY (address_id) REFERENCES public.address(address_id) ON UPDATE CASCADE ON DELETE RESTRICT;
ALTER TABLE ONLY public.store
ADD CONSTRAINT store_address_id_fkey FOREIGN KEY (address_id) REFERENCES public.address(address_id) ON UPDATE CASCADE ON DELETE RESTRICT;
ALTER TABLE ONLY public.store
ADD CONSTRAINT store_manager_staff_id_fkey FOREIGN KEY (manager_staff_id) REFERENCES public.staff(staff_id) ON UPDATE CASCADE ON DELETE RESTRICT;
CREATE FUNCTION public.get_customer_balance(p_customer_id integer, p_effective_date timestamp without time zone) RETURNS numeric
LANGUAGE plpgsql
AS $$
--#OK, WE NEED TO CALCULATE THE CURRENT BALANCE GIVEN A CUSTOMER_ID AND A DATE
--#THAT WE WANT THE BALANCE TO BE EFFECTIVE FOR. THE BALANCE IS:
--# 1) RENTAL FEES FOR ALL PREVIOUS RENTALS
--# 2) ONE DOLLAR FOR EVERY DAY THE PREVIOUS RENTALS ARE OVERDUE
--# 3) IF A FILM IS MORE THAN RENTAL_DURATION * 2 OVERDUE, CHARGE THE REPLACEMENT_COST
--# 4) SUBTRACT ALL PAYMENTS MADE BEFORE THE DATE SPECIFIED
DECLARE
v_rentfees DECIMAL(5,2); --#FEES PAID TO RENT THE VIDEOS INITIALLY
v_overfees INTEGER; --#LATE FEES FOR PRIOR RENTALS
v_payments DECIMAL(5,2); --#SUM OF PAYMENTS MADE PREVIOUSLY
BEGIN
SELECT COALESCE(SUM(film.rental_rate),0) INTO v_rentfees
FROM film, inventory, rental
WHERE film.film_id = inventory.film_id
AND inventory.inventory_id = rental.inventory_id
AND rental.rental_date <= p_effective_date
AND rental.customer_id = p_customer_id;
SELECT COALESCE(SUM(IF((rental.return_date - rental.rental_date) > (film.rental_duration * '1 day'::interval),
((rental.return_date - rental.rental_date) - (film.rental_duration * '1 day'::interval)),0::INTERVAL)),0::INTERVAL) INTO v_overfees
FROM rental, inventory, film
WHERE film.film_id = inventory.film_id
AND inventory.inventory_id = rental.inventory_id
AND rental.rental_date <= p_effective_date
AND rental.customer_id = p_customer_id;
SELECT COALESCE(SUM(payment.amount),0) INTO v_payments
FROM payment
WHERE payment.payment_date <= p_effective_date
AND payment.customer_id = p_customer_id;
RETURN v_rentfees + v_overfees - v_payments;
END
$$;
CREATE FUNCTION public.inventory_held_by_customer(p_inventory_id integer) RETURNS integer
LANGUAGE plpgsql
AS $$
DECLARE
v_customer_id INTEGER;
BEGIN
SELECT customer_id INTO v_customer_id
FROM rental
WHERE return_date IS NULL
AND inventory_id = p_inventory_id;
RETURN v_customer_id;
END $$;
CREATE FUNCTION public.inventory_in_stock(p_inventory_id integer) RETURNS boolean
LANGUAGE plpgsql
AS $$
DECLARE
v_rentals INTEGER;
v_out INTEGER;
BEGIN
-- AN ITEM IS IN-STOCK IF THERE ARE EITHER NO ROWS IN THE rental TABLE
-- FOR THE ITEM OR ALL ROWS HAVE return_date POPULATED
SELECT count(*) INTO v_rentals
FROM rental
WHERE inventory_id = p_inventory_id;
IF v_rentals = 0 THEN
RETURN TRUE;
END IF;
SELECT COUNT(rental_id) INTO v_out
FROM inventory LEFT JOIN rental USING(inventory_id)
WHERE inventory.inventory_id = p_inventory_id
AND rental.return_date IS NULL;
IF v_out > 0 THEN
RETURN FALSE;
ELSE
RETURN TRUE;
END IF;
END $$;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment