527 lines
13 KiB
PL/PgSQL
527 lines
13 KiB
PL/PgSQL
-- Downloaded from: https://github.com/Yesk0/KBTU_Database_24-25/blob/ad089a5cc4f6302d82136d7722a929548a3c0e76/Employee.sql
|
|
--
|
|
-- PostgreSQL database dump
|
|
--
|
|
|
|
-- Dumped from database version 16.4
|
|
-- Dumped by pg_dump version 16.4
|
|
|
|
SET statement_timeout = 0;
|
|
SET lock_timeout = 0;
|
|
SET idle_in_transaction_session_timeout = 0;
|
|
SET client_encoding = 'UTF8';
|
|
SET standard_conforming_strings = on;
|
|
SELECT pg_catalog.set_config('search_path', '', false);
|
|
SET check_function_bodies = false;
|
|
SET xmloption = content;
|
|
SET client_min_messages = warning;
|
|
SET row_security = off;
|
|
|
|
--
|
|
-- Name: SCHEMA public; Type: COMMENT; Schema: -; Owner: pg_database_owner
|
|
--
|
|
|
|
COMMENT ON SCHEMA public IS 'Standard public schema';
|
|
|
|
|
|
--
|
|
-- Name: adminpack; Type: EXTENSION; Schema: -; Owner: -
|
|
--
|
|
|
|
CREATE EXTENSION IF NOT EXISTS adminpack WITH SCHEMA pg_catalog;
|
|
|
|
|
|
--
|
|
-- Name: EXTENSION adminpack; Type: COMMENT; Schema: -; Owner:
|
|
--
|
|
|
|
COMMENT ON EXTENSION adminpack IS 'administrative functions for PostgreSQL';
|
|
|
|
|
|
--
|
|
-- Name: mpaa_rating; Type: TYPE; Schema: public; Owner: postgres
|
|
--
|
|
|
|
CREATE TYPE public.mpaa_rating AS ENUM (
|
|
'G',
|
|
'PG',
|
|
'PG-13',
|
|
'R',
|
|
'NC-17'
|
|
);
|
|
|
|
|
|
ALTER TYPE public.mpaa_rating OWNER TO postgres;
|
|
|
|
--
|
|
-- Name: year; Type: DOMAIN; Schema: public; Owner: postgres
|
|
--
|
|
|
|
CREATE DOMAIN public.year AS integer
|
|
CONSTRAINT year_check CHECK (((VALUE >= 1901) AND (VALUE <= 2155)));
|
|
|
|
|
|
ALTER DOMAIN public.year OWNER TO postgres;
|
|
|
|
--
|
|
-- Name: _group_concat(text, text); Type: FUNCTION; Schema: public; Owner: postgres
|
|
--
|
|
|
|
CREATE FUNCTION public._group_concat(text, text) RETURNS text
|
|
LANGUAGE sql IMMUTABLE
|
|
AS $_$
|
|
SELECT CASE
|
|
WHEN $2 IS NULL THEN $1
|
|
WHEN $1 IS NULL THEN $2
|
|
ELSE $1 || ', ' || $2
|
|
END
|
|
$_$;
|
|
|
|
|
|
ALTER FUNCTION public._group_concat(text, text) OWNER TO postgres;
|
|
|
|
--
|
|
-- Name: film_in_stock(integer, integer); Type: FUNCTION; Schema: public; Owner: postgres
|
|
--
|
|
|
|
CREATE FUNCTION public.film_in_stock(p_film_id integer, p_store_id integer, OUT p_film_count integer) RETURNS SETOF integer
|
|
LANGUAGE sql
|
|
AS $_$
|
|
SELECT inventory_id
|
|
FROM inventory
|
|
WHERE film_id = $1
|
|
AND store_id = $2
|
|
AND inventory_in_stock(inventory_id);
|
|
$_$;
|
|
|
|
|
|
ALTER FUNCTION public.film_in_stock(p_film_id integer, p_store_id integer, OUT p_film_count integer) OWNER TO postgres;
|
|
|
|
--
|
|
-- Name: film_not_in_stock(integer, integer); Type: FUNCTION; Schema: public; Owner: postgres
|
|
--
|
|
|
|
CREATE FUNCTION public.film_not_in_stock(p_film_id integer, p_store_id integer, OUT p_film_count integer) RETURNS SETOF integer
|
|
LANGUAGE sql
|
|
AS $_$
|
|
SELECT inventory_id
|
|
FROM inventory
|
|
WHERE film_id = $1
|
|
AND store_id = $2
|
|
AND NOT inventory_in_stock(inventory_id);
|
|
$_$;
|
|
|
|
|
|
ALTER FUNCTION public.film_not_in_stock(p_film_id integer, p_store_id integer, OUT p_film_count integer) OWNER TO postgres;
|
|
|
|
--
|
|
-- Name: get_customer_balance(integer, timestamp without time zone); Type: FUNCTION; Schema: public; Owner: postgres
|
|
--
|
|
|
|
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)),0) 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
|
|
$$;
|
|
|
|
|
|
ALTER FUNCTION public.get_customer_balance(p_customer_id integer, p_effective_date timestamp without time zone) OWNER TO postgres;
|
|
|
|
--
|
|
-- Name: inventory_held_by_customer(integer); Type: FUNCTION; Schema: public; Owner: postgres
|
|
--
|
|
|
|
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 $$;
|
|
|
|
|
|
ALTER FUNCTION public.inventory_held_by_customer(p_inventory_id integer) OWNER TO postgres;
|
|
|
|
--
|
|
-- Name: inventory_in_stock(integer); Type: FUNCTION; Schema: public; Owner: postgres
|
|
--
|
|
|
|
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 $$;
|
|
|
|
|
|
ALTER FUNCTION public.inventory_in_stock(p_inventory_id integer) OWNER TO postgres;
|
|
|
|
--
|
|
-- Name: last_day(timestamp without time zone); Type: FUNCTION; Schema: public; Owner: postgres
|
|
--
|
|
|
|
CREATE FUNCTION public.last_day(timestamp without time zone) RETURNS date
|
|
LANGUAGE sql IMMUTABLE STRICT
|
|
AS $_$
|
|
SELECT CASE
|
|
WHEN EXTRACT(MONTH FROM $1) = 12 THEN
|
|
(((EXTRACT(YEAR FROM $1) + 1) operator(pg_catalog.||) '-01-01')::date - INTERVAL '1 day')::date
|
|
ELSE
|
|
((EXTRACT(YEAR FROM $1) operator(pg_catalog.||) '-' operator(pg_catalog.||) (EXTRACT(MONTH FROM $1) + 1) operator(pg_catalog.||) '-01')::date - INTERVAL '1 day')::date
|
|
END
|
|
$_$;
|
|
|
|
|
|
ALTER FUNCTION public.last_day(timestamp without time zone) OWNER TO postgres;
|
|
|
|
--
|
|
-- Name: last_updated(); Type: FUNCTION; Schema: public; Owner: postgres
|
|
--
|
|
|
|
CREATE FUNCTION public.last_updated() RETURNS trigger
|
|
LANGUAGE plpgsql
|
|
AS $$
|
|
BEGIN
|
|
NEW.last_update = CURRENT_TIMESTAMP;
|
|
RETURN NEW;
|
|
END $$;
|
|
|
|
|
|
ALTER FUNCTION public.last_updated() OWNER TO postgres;
|
|
|
|
--
|
|
-- Name: group_concat(text); Type: AGGREGATE; Schema: public; Owner: postgres
|
|
--
|
|
|
|
CREATE AGGREGATE public.group_concat(text) (
|
|
SFUNC = public._group_concat,
|
|
STYPE = text
|
|
);
|
|
|
|
|
|
ALTER AGGREGATE public.group_concat(text) OWNER TO postgres;
|
|
|
|
--
|
|
-- Name: actor_actor_id_seq; Type: SEQUENCE; Schema: public; Owner: postgres
|
|
--
|
|
|
|
CREATE SEQUENCE public.actor_actor_id_seq
|
|
START WITH 1
|
|
INCREMENT BY 1
|
|
NO MINVALUE
|
|
NO MAXVALUE
|
|
CACHE 1;
|
|
|
|
|
|
ALTER SEQUENCE public.actor_actor_id_seq OWNER TO postgres;
|
|
|
|
--
|
|
-- Name: address_address_id_seq; Type: SEQUENCE; Schema: public; Owner: postgres
|
|
--
|
|
|
|
CREATE SEQUENCE public.address_address_id_seq
|
|
START WITH 1
|
|
INCREMENT BY 1
|
|
NO MINVALUE
|
|
NO MAXVALUE
|
|
CACHE 1;
|
|
|
|
|
|
ALTER SEQUENCE public.address_address_id_seq OWNER TO postgres;
|
|
|
|
--
|
|
-- Name: category_category_id_seq; Type: SEQUENCE; Schema: public; Owner: postgres
|
|
--
|
|
|
|
CREATE SEQUENCE public.category_category_id_seq
|
|
START WITH 1
|
|
INCREMENT BY 1
|
|
NO MINVALUE
|
|
NO MAXVALUE
|
|
CACHE 1;
|
|
|
|
|
|
ALTER SEQUENCE public.category_category_id_seq OWNER TO postgres;
|
|
|
|
--
|
|
-- Name: city_city_id_seq; Type: SEQUENCE; Schema: public; Owner: postgres
|
|
--
|
|
|
|
CREATE SEQUENCE public.city_city_id_seq
|
|
START WITH 1
|
|
INCREMENT BY 1
|
|
NO MINVALUE
|
|
NO MAXVALUE
|
|
CACHE 1;
|
|
|
|
|
|
ALTER SEQUENCE public.city_city_id_seq OWNER TO postgres;
|
|
|
|
--
|
|
-- Name: country_country_id_seq; Type: SEQUENCE; Schema: public; Owner: postgres
|
|
--
|
|
|
|
CREATE SEQUENCE public.country_country_id_seq
|
|
START WITH 1
|
|
INCREMENT BY 1
|
|
NO MINVALUE
|
|
NO MAXVALUE
|
|
CACHE 1;
|
|
|
|
|
|
ALTER SEQUENCE public.country_country_id_seq OWNER TO postgres;
|
|
|
|
--
|
|
-- Name: customer_customer_id_seq; Type: SEQUENCE; Schema: public; Owner: postgres
|
|
--
|
|
|
|
CREATE SEQUENCE public.customer_customer_id_seq
|
|
START WITH 1
|
|
INCREMENT BY 1
|
|
NO MINVALUE
|
|
NO MAXVALUE
|
|
CACHE 1;
|
|
|
|
|
|
ALTER SEQUENCE public.customer_customer_id_seq OWNER TO postgres;
|
|
|
|
SET default_tablespace = '';
|
|
|
|
SET default_table_access_method = heap;
|
|
|
|
--
|
|
-- Name: employee; Type: TABLE; Schema: public; Owner: postgres
|
|
--
|
|
|
|
CREATE TABLE public.employee (
|
|
eid character varying(100),
|
|
name character varying(100),
|
|
job_title character varying(100),
|
|
department character varying(100),
|
|
business_unit character varying(100),
|
|
gender character varying(10),
|
|
ethnicity character varying(100),
|
|
age integer,
|
|
annual_salary numeric(15,2),
|
|
bonus_percentage numeric(15,2),
|
|
country character varying(100),
|
|
city character varying(100),
|
|
id integer NOT NULL,
|
|
department_id integer,
|
|
departmnt character varying(100)
|
|
);
|
|
|
|
|
|
ALTER TABLE public.employee OWNER TO postgres;
|
|
|
|
--
|
|
-- Name: employee_id_seq; Type: SEQUENCE; Schema: public; Owner: postgres
|
|
--
|
|
|
|
CREATE SEQUENCE public.employee_id_seq
|
|
AS integer
|
|
START WITH 1
|
|
INCREMENT BY 1
|
|
NO MINVALUE
|
|
NO MAXVALUE
|
|
CACHE 1;
|
|
|
|
|
|
ALTER SEQUENCE public.employee_id_seq OWNER TO postgres;
|
|
|
|
--
|
|
-- Name: employee_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: postgres
|
|
--
|
|
|
|
ALTER SEQUENCE public.employee_id_seq OWNED BY public.employee.id;
|
|
|
|
|
|
--
|
|
-- Name: film_film_id_seq; Type: SEQUENCE; Schema: public; Owner: postgres
|
|
--
|
|
|
|
CREATE SEQUENCE public.film_film_id_seq
|
|
START WITH 1
|
|
INCREMENT BY 1
|
|
NO MINVALUE
|
|
NO MAXVALUE
|
|
CACHE 1;
|
|
|
|
|
|
ALTER SEQUENCE public.film_film_id_seq OWNER TO postgres;
|
|
|
|
--
|
|
-- Name: inventory_inventory_id_seq; Type: SEQUENCE; Schema: public; Owner: postgres
|
|
--
|
|
|
|
CREATE SEQUENCE public.inventory_inventory_id_seq
|
|
START WITH 1
|
|
INCREMENT BY 1
|
|
NO MINVALUE
|
|
NO MAXVALUE
|
|
CACHE 1;
|
|
|
|
|
|
ALTER SEQUENCE public.inventory_inventory_id_seq OWNER TO postgres;
|
|
|
|
--
|
|
-- Name: language_language_id_seq; Type: SEQUENCE; Schema: public; Owner: postgres
|
|
--
|
|
|
|
CREATE SEQUENCE public.language_language_id_seq
|
|
START WITH 1
|
|
INCREMENT BY 1
|
|
NO MINVALUE
|
|
NO MAXVALUE
|
|
CACHE 1;
|
|
|
|
|
|
ALTER SEQUENCE public.language_language_id_seq OWNER TO postgres;
|
|
|
|
--
|
|
-- Name: payment_payment_id_seq; Type: SEQUENCE; Schema: public; Owner: postgres
|
|
--
|
|
|
|
CREATE SEQUENCE public.payment_payment_id_seq
|
|
START WITH 1
|
|
INCREMENT BY 1
|
|
NO MINVALUE
|
|
NO MAXVALUE
|
|
CACHE 1;
|
|
|
|
|
|
ALTER SEQUENCE public.payment_payment_id_seq OWNER TO postgres;
|
|
|
|
--
|
|
-- Name: rental_rental_id_seq; Type: SEQUENCE; Schema: public; Owner: postgres
|
|
--
|
|
|
|
CREATE SEQUENCE public.rental_rental_id_seq
|
|
START WITH 1
|
|
INCREMENT BY 1
|
|
NO MINVALUE
|
|
NO MAXVALUE
|
|
CACHE 1;
|
|
|
|
|
|
ALTER SEQUENCE public.rental_rental_id_seq OWNER TO postgres;
|
|
|
|
--
|
|
-- Name: staff_staff_id_seq; Type: SEQUENCE; Schema: public; Owner: postgres
|
|
--
|
|
|
|
CREATE SEQUENCE public.staff_staff_id_seq
|
|
START WITH 1
|
|
INCREMENT BY 1
|
|
NO MINVALUE
|
|
NO MAXVALUE
|
|
CACHE 1;
|
|
|
|
|
|
ALTER SEQUENCE public.staff_staff_id_seq OWNER TO postgres;
|
|
|
|
--
|
|
-- Name: store_store_id_seq; Type: SEQUENCE; Schema: public; Owner: postgres
|
|
--
|
|
|
|
CREATE SEQUENCE public.store_store_id_seq
|
|
START WITH 1
|
|
INCREMENT BY 1
|
|
NO MINVALUE
|
|
NO MAXVALUE
|
|
CACHE 1;
|
|
|
|
|
|
ALTER SEQUENCE public.store_store_id_seq OWNER TO postgres;
|
|
|
|
--
|
|
-- Name: employee id; Type: DEFAULT; Schema: public; Owner: postgres
|
|
--
|
|
|
|
ALTER TABLE ONLY public.employee ALTER COLUMN id SET DEFAULT nextval('public.employee_id_seq'::regclass);
|
|
|
|
|
|
--
|
|
-- Name: employee employee_pkey; Type: CONSTRAINT; Schema: public; Owner: postgres
|
|
--
|
|
|
|
ALTER TABLE ONLY public.employee
|
|
ADD CONSTRAINT employee_pkey PRIMARY KEY (id);
|
|
|
|
|
|
--
|
|
-- Name: SCHEMA public; Type: ACL; Schema: -; Owner: pg_database_owner
|
|
--
|
|
|
|
REVOKE USAGE ON SCHEMA public FROM PUBLIC;
|
|
GRANT ALL ON SCHEMA public TO postgres;
|
|
GRANT ALL ON SCHEMA public TO PUBLIC;
|
|
|
|
|
|
--
|
|
-- PostgreSQL database dump complete
|
|
--
|
|
|