--
-- PostgreSQL database dump
--

-- Dumped from database version 16.2 (Debian 16.2-1.pgdg120+2)
-- Dumped by pg_dump version 16.2 (Debian 16.2-1.pgdg120+2)

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;

SET default_tablespace = '';

SET default_table_access_method = heap;

--
-- Name: authors; Type: TABLE; Schema: public; Owner: postgres
--

CREATE TABLE public.authors (
    id uuid DEFAULT gen_random_uuid() NOT NULL,
    first_name character varying NOT NULL,
    last_name character varying NOT NULL
);


ALTER TABLE public.authors OWNER TO postgres;

--
-- Name: average_grades; Type: VIEW; Schema: public; Owner: postgres
--

CREATE VIEW public.average_grades AS
SELECT
    NULL::character varying(255) AS title,
    NULL::bigint AS reviews_count,
    NULL::numeric AS avg_grade;


ALTER VIEW public.average_grades OWNER TO postgres;

--
-- Name: VIEW average_grades; Type: COMMENT; Schema: public; Owner: postgres
--

COMMENT ON VIEW public.average_grades IS 'widok zawierający 5-ciu autorów, których średnia
ocen wszystkich książek jest najwyższa.';


--
-- Name: books; Type: TABLE; Schema: public; Owner: postgres
--

CREATE TABLE public.books (
    id uuid DEFAULT gen_random_uuid() NOT NULL,
    author_id uuid NOT NULL,
    title character varying(255) NOT NULL,
    published_year integer NOT NULL,
    isbn character varying(20) NOT NULL
);


ALTER TABLE public.books OWNER TO postgres;

--
-- Name: reviews; Type: TABLE; Schema: public; Owner: postgres
--

CREATE TABLE public.reviews (
    id uuid DEFAULT gen_random_uuid() NOT NULL,
    book_id uuid NOT NULL,
    grade smallint NOT NULL,
    review text NOT NULL
);


ALTER TABLE public.reviews OWNER TO postgres;

--
-- Data for Name: authors; Type: TABLE DATA; Schema: public; Owner: postgres
--

INSERT INTO public.authors (id, first_name, last_name) VALUES ('a842544f-2f2d-4fdb-82c0-90c84902c996', 'Best', 'Seller');
INSERT INTO public.authors (id, first_name, last_name) VALUES ('ea4b242d-4fb9-4b7e-9301-3f5489759eb8', 'Yoko', 'Ono');


--
-- Data for Name: books; Type: TABLE DATA; Schema: public; Owner: postgres
--

INSERT INTO public.books (id, author_id, title, published_year, isbn) VALUES ('13564239-b83c-4896-88cb-f92b312438ab', 'a842544f-2f2d-4fdb-82c0-90c84902c996', 'Rozwiązanie tajemnicy Bażanta', 2003, '15487-634-756-123');
INSERT INTO public.books (id, author_id, title, published_year, isbn) VALUES ('752f58d2-604a-466e-8957-ced8c256a0c6', 'a842544f-2f2d-4fdb-82c0-90c84902c996', 'Tajemnica zaginionego Bażanta', 2001, '12346-543-123-162');
INSERT INTO public.books (id, author_id, title, published_year, isbn) VALUES ('b9fc879e-ba41-4d89-819d-7bb928eab557', 'ea4b242d-4fb9-4b7e-9301-3f5489759eb8', 'Autobiografia Yoko Ono', 1999, '12345-567-789-123');


--
-- Data for Name: reviews; Type: TABLE DATA; Schema: public; Owner: postgres
--

INSERT INTO public.reviews (id, book_id, grade, review) VALUES ('ef2355d8-3277-440b-8344-7fff3ea72a8a', '13564239-b83c-4896-88cb-f92b312438ab', 9, 'Mam nadzieje na kolejną część');
INSERT INTO public.reviews (id, book_id, grade, review) VALUES ('bdd1ecaa-0fa7-4546-b9fd-e1138c5d52a6', '752f58d2-604a-466e-8957-ced8c256a0c6', 4, 'Dobra literatura');
INSERT INTO public.reviews (id, book_id, grade, review) VALUES ('6db5ef53-751b-4034-b61c-8c31b6a1af80', '752f58d2-604a-466e-8957-ced8c256a0c6', 6, 'Niezła ksiązka');
INSERT INTO public.reviews (id, book_id, grade, review) VALUES ('f28488b5-24db-4e64-8455-aa30ea1f1c10', '752f58d2-604a-466e-8957-ced8c256a0c6', 7, 'Spoko');
INSERT INTO public.reviews (id, book_id, grade, review) VALUES ('cb0a3d30-fd8f-45a3-8c9f-f8763fa332eb', 'b9fc879e-ba41-4d89-819d-7bb928eab557', 6, 'Spoko ksiązka');
INSERT INTO public.reviews (id, book_id, grade, review) VALUES ('bccfa802-c020-450e-9b76-376251c15de1', '13564239-b83c-4896-88cb-f92b312438ab', 7, 'OK');
INSERT INTO public.reviews (id, book_id, grade, review) VALUES ('9a23d2d8-988a-49f9-888e-4e68233c456b', '13564239-b83c-4896-88cb-f92b312438ab', 8, 'Bardzo dobra. Lepsza niż poprzednia.');


--
-- Name: authors authors_pkey; Type: CONSTRAINT; Schema: public; Owner: postgres
--

ALTER TABLE ONLY public.authors
    ADD CONSTRAINT authors_pkey PRIMARY KEY (id);


--
-- Name: books books_pkey; Type: CONSTRAINT; Schema: public; Owner: postgres
--

ALTER TABLE ONLY public.books
    ADD CONSTRAINT books_pkey PRIMARY KEY (id);


--
-- Name: reviews reviews_pkey; Type: CONSTRAINT; Schema: public; Owner: postgres
--

ALTER TABLE ONLY public.reviews
    ADD CONSTRAINT reviews_pkey PRIMARY KEY (id);


--
-- Name: average_grades _RETURN; Type: RULE; Schema: public; Owner: postgres
--

CREATE OR REPLACE VIEW public.average_grades AS
 SELECT b.title,
    count(DISTINCT r.id) AS reviews_count,
    avg(r.grade) AS avg_grade
   FROM (public.books b
     JOIN public.reviews r ON ((r.book_id = b.id)))
  GROUP BY b.id
  ORDER BY (avg(r.grade)) DESC
 LIMIT 5;


--
-- Name: reviews reviews_book_id_fkey; Type: FK CONSTRAINT; Schema: public; Owner: postgres
--

ALTER TABLE ONLY public.reviews
    ADD CONSTRAINT reviews_book_id_fkey FOREIGN KEY (book_id) REFERENCES public.books(id);


--
-- PostgreSQL database dump complete
--
