diff --git a/task-1/README.MD b/task-1/README.MD new file mode 100644 index 0000000..1c1d80f --- /dev/null +++ b/task-1/README.MD @@ -0,0 +1,85 @@ +📘 Task 1 — Library Database (3NF Normalization) +This folder contains all files required to set up and run the database for Task 1, including the schema, seed data, ER diagram, and Docker Compose configuration. + +📌 1. ER Diagram +The ER diagram is included in the repository: library_er_diagram.png +It shows all tables and relationships in the normalized database. + +📌 2. Running PostgreSQL (Docker) +A docker-compose.yml file is included to start PostgreSQL quickly. +Start the database: + +```bash +docker compose up -d +``` + +Check that the container is running: + +```bash +docker ps +``` + +Connection details: +- Host: localhost +- Port: 5432 +- Database: library +- User: postgres +- Password: postgres + +📌 3. Apply the Schema +Run the schema file to create all tables and constraints: + +```bash +psql -h localhost -p 5432 -U postgres -d library -f schema.sql +``` + +📌 4. Insert Seed Data +Load the initial dataset: + +```bash +psql -h localhost -p 5432 -U postgres -d library -f seed.sql +``` + +This inserts: +- members +- cards +- publishers +- authors +- tags +- books +- shelves +- copies +- borrows +Sequences are synchronized using setval. + +📌 5. Tables Included +- members +- cards +- publishers +- authors +- tags +- books +- shelves +- copies +- borrows +- book_authors (N:M) +- book_tags (N:M) + +📌 6. Relationship Types +1:1 — members ↔ cards +1:N — publishers → books +1:N — books → copies +1:N — members → borrows +N:M — books ↔ authors +N:M — books ↔ tags + +📌 7. Constraints +PRIMARY KEY on every table +FOREIGN KEY relationships +UNIQUE constraints (email, ISBN, barcode, tag name, etc.) +CHECK constraints (fine ≥ 0, valid dates, etc.) + +📌 8. Notes +The schema and seed data are ready to be used in Task 2 (Java JDBC). +The ER diagram reflects the final normalized structure. +The database can be recreated at any time using the steps above. \ No newline at end of file diff --git a/task-1/docker-compose.yml b/task-1/docker-compose.yml new file mode 100644 index 0000000..8bdcb95 --- /dev/null +++ b/task-1/docker-compose.yml @@ -0,0 +1,18 @@ +version: '3.9' + +services: + postgres: + image: postgres:16 + container_name: library-db + restart: unless-stopped + environment: + POSTGRES_USER: postgres + POSTGRES_PASSWORD: postgres + POSTGRES_DB: library + ports: + - "5432:5432" + volumes: + - postgres_data:/var/lib/postgresql/data + +volumes: + postgres_data: diff --git a/task-1/library_er_diagram.png b/task-1/library_er_diagram.png new file mode 100644 index 0000000..dd57776 Binary files /dev/null and b/task-1/library_er_diagram.png differ diff --git a/task-1/library_loans.csv b/task-1/library_loans.csv deleted file mode 100644 index 43b8ec8..0000000 --- a/task-1/library_loans.csv +++ /dev/null @@ -1,101 +0,0 @@ -member_email,member_name,member_phone,member_address,card_number,card_issued_on,card_expires_on,book_isbn,book_title,publisher_name,author_names,tag_names,copy_barcode,shelf_code,borrowed_at,due_date,returned_at,fine_eur -omar@example.com,Omar,+31689012345,"Rijnkade 19, Utrecht",C-1008,2024-01-30,2027-01-30,9780596517748,JavaScript: The Good Parts,O'Reilly,Douglas Crockford,javascript; beginners,BC-0011,B-03,2025-12-02 08:08,2025-12-23 08:08,2025-12-13 08:08,0.00 -tom@example.com,Tom,+31645678901,"Biltstraat 88, Utrecht",C-1004,2023-11-20,2026-11-20,9780596517748,JavaScript: The Good Parts,O'Reilly,Douglas Crockford,javascript; beginners,BC-0013,B-03,2026-01-10 08:51,2026-01-31 08:51,2026-01-25 08:51,0.00 -sam@example.com,Sam,+31612345678,"Oudegracht 10, Utrecht",C-1001,2024-01-10,2027-01-10,9780596007126,Head First Design Patterns,O'Reilly,Eric Freeman; Elisabeth Robson,design-patterns; java,BC-0024,A-02,2025-12-21 23:02,2026-01-11 23:02,2026-01-14 23:02,0.75 -alex@example.com,Alex,+31623456789,"Lange Viestraat 5, Utrecht",C-1002,2024-02-15,2027-02-15,9780596517748,JavaScript: The Good Parts,O'Reilly,Douglas Crockford,javascript; beginners,BC-0011,B-03,2026-05-02 02:49,2026-05-23 02:49,,0.00 -alex@example.com,Alex,+31623456789,"Lange Viestraat 5, Utrecht",C-1002,2024-02-15,2027-02-15,9780321125217,Domain-Driven Design,Addison-Wesley,Eric Evans,software-design; architecture,BC-0020,D-01,2026-05-08 22:34,2026-05-29 22:34,,0.00 -priya@example.com,Priya,+31634567890,"Maliebaan 22, Utrecht",C-1003,2024-03-01,2027-03-01,9780134494166,Clean Architecture,Prentice Hall,Robert C. Martin,architecture; best-practices,BC-0030,C-01,2026-02-28 05:49,2026-03-21 05:49,,15.75 -sam@example.com,Sam,+31612345678,"Oudegracht 10, Utrecht",C-1001,2024-01-10,2027-01-10,9780132350884,Clean Code,Prentice Hall,Robert C. Martin,best-practices; software-design,BC-0005,C-01,2026-03-22 05:23,2026-04-12 05:23,2026-04-04 05:23,0.00 -omar@example.com,Omar,+31689012345,"Rijnkade 19, Utrecht",C-1008,2024-01-30,2027-01-30,9781491950357,Learning Python,O'Reilly,Mark Lutz,python; beginners,BC-0015,C-01,2026-03-14 20:16,2026-04-04 20:16,2026-04-06 20:16,0.50 -yuki@example.com,Yuki,+31690123456,"Springweg 2, Utrecht",C-1009,2023-12-08,2026-12-08,9780596009205,Head First Java,O'Reilly,Kathy Sierra; Bert Bates,java; beginners,BC-0003,A-02,2026-02-18 18:15,2026-03-11 18:15,2026-02-23 18:15,0.00 -omar@example.com,Omar,+31689012345,"Rijnkade 19, Utrecht",C-1008,2024-01-30,2027-01-30,9780321774620,Continuous Delivery,Addison-Wesley,Jez Humble; David Farley,devops; best-practices,BC-0036,D-01,2026-02-15 11:43,2026-03-08 11:43,2026-03-02 11:43,0.00 -alex@example.com,Alex,+31623456789,"Lange Viestraat 5, Utrecht",C-1002,2024-02-15,2027-02-15,9780201633610,Design Patterns,Addison-Wesley,Erich Gamma; Richard Helm; Ralph Johnson; John Vlissides,design-patterns; oop,BC-0009,B-01,2026-03-17 17:29,2026-04-07 17:29,2026-03-23 17:29,0.00 -lena@example.com,Lena,+31656789012,"Voorstraat 3, Utrecht",C-1005,2024-04-05,2027-04-05,9780201633610,Design Patterns,Addison-Wesley,Erich Gamma; Richard Helm; Ralph Johnson; John Vlissides,design-patterns; oop,BC-0008,B-01,2026-04-03 11:47,2026-04-24 11:47,2026-04-13 11:47,0.00 -sam@example.com,Sam,+31612345678,"Oudegracht 10, Utrecht",C-1001,2024-01-10,2027-01-10,9780201633610,Design Patterns,Addison-Wesley,Erich Gamma; Richard Helm; Ralph Johnson; John Vlissides,design-patterns; oop,BC-0007,B-01,2026-04-26 09:59,2026-05-17 09:59,2026-05-07 09:59,0.00 -yuki@example.com,Yuki,+31690123456,"Springweg 2, Utrecht",C-1009,2023-12-08,2026-12-08,9781491950357,Learning Python,O'Reilly,Mark Lutz,python; beginners,BC-0014,C-01,2026-04-01 10:42,2026-04-22 10:42,2026-04-05 10:42,0.00 -sam@example.com,Sam,+31612345678,"Oudegracht 10, Utrecht",C-1001,2024-01-10,2027-01-10,9781617294945,Spring in Action,Manning,Craig Walls,java; spring; frameworks,BC-0026,D-01,2026-01-10 13:15,2026-01-31 13:15,,28.00 -omar@example.com,Omar,+31689012345,"Rijnkade 19, Utrecht",C-1008,2024-01-30,2027-01-30,9780132181815,The Clean Coder,Prentice Hall,Robert C. Martin,best-practices; career,BC-0029,D-01,2026-03-16 11:54,2026-04-06 11:54,2026-04-15 11:54,2.25 -chen@example.com,Chen,+31601234567,"Wittevrouwenstraat 31, Utrecht",C-1010,2024-06-01,2027-06-01,9780321774620,Continuous Delivery,Addison-Wesley,Jez Humble; David Farley,devops; best-practices,BC-0037,D-01,2026-03-11 01:13,2026-04-01 01:13,2026-03-27 01:13,0.00 -james@example.com,James,+31667890123,"Catharijnesingel 14, Utrecht",C-1006,2023-09-12,2026-09-12,9781491950357,Learning Python,O'Reilly,Mark Lutz,python; beginners,BC-0016,C-01,2026-03-21 05:22,2026-04-11 05:22,2026-04-02 05:22,0.00 -nina@example.com,Nina,+31678901234,"Nobelstraat 7, Utrecht",C-1007,2024-05-22,2027-05-22,9780137081073,The Pragmatic Programmer,Addison-Wesley,David Thomas; Andrew Hunt,best-practices; career,BC-0019,C-01,2025-12-02 14:01,2025-12-23 14:01,,37.75 -priya@example.com,Priya,+31634567890,"Maliebaan 22, Utrecht",C-1003,2024-03-01,2027-03-01,9781617294945,Spring in Action,Manning,Craig Walls,java; spring; frameworks,BC-0025,D-01,2026-01-15 13:38,2026-02-05 13:38,2026-01-18 13:38,0.00 -tom@example.com,Tom,+31645678901,"Biltstraat 88, Utrecht",C-1004,2023-11-20,2026-11-20,9780137081073,The Pragmatic Programmer,Addison-Wesley,David Thomas; Andrew Hunt,best-practices; career,BC-0018,C-01,2026-02-23 11:30,2026-03-16 11:30,2026-03-05 11:30,0.00 -alex@example.com,Alex,+31623456789,"Lange Viestraat 5, Utrecht",C-1002,2024-02-15,2027-02-15,9780596009205,Head First Java,O'Reilly,Kathy Sierra; Bert Bates,java; beginners,BC-0004,A-02,2026-03-30 21:11,2026-04-20 21:11,2026-04-08 21:11,0.00 -nina@example.com,Nina,+31678901234,"Nobelstraat 7, Utrecht",C-1007,2024-05-22,2027-05-22,9780201633610,Design Patterns,Addison-Wesley,Erich Gamma; Richard Helm; Ralph Johnson; John Vlissides,design-patterns; oop,BC-0009,B-01,2026-01-28 05:02,2026-02-18 05:02,2026-02-07 05:02,0.00 -priya@example.com,Priya,+31634567890,"Maliebaan 22, Utrecht",C-1003,2024-03-01,2027-03-01,9780201633610,Design Patterns,Addison-Wesley,Erich Gamma; Richard Helm; Ralph Johnson; John Vlissides,design-patterns; oop,BC-0009,B-01,2026-05-19 23:46,2026-06-09 23:46,2026-06-05 23:46,0.00 -priya@example.com,Priya,+31634567890,"Maliebaan 22, Utrecht",C-1003,2024-03-01,2027-03-01,9780321125217,Domain-Driven Design,Addison-Wesley,Eric Evans,software-design; architecture,BC-0021,D-01,2026-03-31 18:59,2026-04-21 18:59,2026-04-29 18:59,2.00 -lena@example.com,Lena,+31656789012,"Voorstraat 3, Utrecht",C-1005,2024-04-05,2027-04-05,9780134494166,Clean Architecture,Prentice Hall,Robert C. Martin,architecture; best-practices,BC-0032,C-01,2026-05-12 04:26,2026-06-02 04:26,,0.00 -sam@example.com,Sam,+31612345678,"Oudegracht 10, Utrecht",C-1001,2024-01-10,2027-01-10,9780321774620,Continuous Delivery,Addison-Wesley,Jez Humble; David Farley,devops; best-practices,BC-0036,D-01,2025-12-15 01:01,2026-01-05 01:01,2026-01-04 01:01,0.00 -lena@example.com,Lena,+31656789012,"Voorstraat 3, Utrecht",C-1005,2024-04-05,2027-04-05,9780132181815,The Clean Coder,Prentice Hall,Robert C. Martin,best-practices; career,BC-0028,D-01,2025-12-14 19:38,2026-01-04 19:38,2025-12-17 19:38,0.00 -yuki@example.com,Yuki,+31690123456,"Springweg 2, Utrecht",C-1009,2023-12-08,2026-12-08,9780132181815,The Clean Coder,Prentice Hall,Robert C. Martin,best-practices; career,BC-0029,D-01,2026-05-17 21:26,2026-06-07 21:26,2026-06-12 21:26,1.25 -yuki@example.com,Yuki,+31690123456,"Springweg 2, Utrecht",C-1009,2023-12-08,2026-12-08,9780596009205,Head First Java,O'Reilly,Kathy Sierra; Bert Bates,java; beginners,BC-0003,A-02,2026-05-18 04:02,2026-06-08 04:02,,0.00 -james@example.com,James,+31667890123,"Catharijnesingel 14, Utrecht",C-1006,2023-09-12,2026-09-12,9780321774620,Continuous Delivery,Addison-Wesley,Jez Humble; David Farley,devops; best-practices,BC-0037,D-01,2026-01-12 18:47,2026-02-02 18:47,2026-01-25 18:47,0.00 -tom@example.com,Tom,+31645678901,"Biltstraat 88, Utrecht",C-1004,2023-11-20,2026-11-20,9781617294945,Spring in Action,Manning,Craig Walls,java; spring; frameworks,BC-0025,D-01,2025-12-08 13:07,2025-12-29 13:07,2025-12-18 13:07,0.00 -chen@example.com,Chen,+31601234567,"Wittevrouwenstraat 31, Utrecht",C-1010,2024-06-01,2027-06-01,9780132181815,The Clean Coder,Prentice Hall,Robert C. Martin,best-practices; career,BC-0028,D-01,2026-02-16 01:36,2026-03-09 01:36,2026-03-06 01:36,0.00 -priya@example.com,Priya,+31634567890,"Maliebaan 22, Utrecht",C-1003,2024-03-01,2027-03-01,9780321774620,Continuous Delivery,Addison-Wesley,Jez Humble; David Farley,devops; best-practices,BC-0039,D-01,2026-02-16 14:32,2026-03-09 14:32,2026-02-24 14:32,0.00 -priya@example.com,Priya,+31634567890,"Maliebaan 22, Utrecht",C-1003,2024-03-01,2027-03-01,9781491950357,Learning Python,O'Reilly,Mark Lutz,python; beginners,BC-0017,C-01,2025-12-11 06:06,2026-01-01 06:06,2026-01-06 06:06,1.25 -chen@example.com,Chen,+31601234567,"Wittevrouwenstraat 31, Utrecht",C-1010,2024-06-01,2027-06-01,9780201485677,Refactoring,Addison-Wesley,Martin Fowler,best-practices; java,BC-0040,B-01,2025-12-08 23:42,2025-12-29 23:42,,36.00 -lena@example.com,Lena,+31656789012,"Voorstraat 3, Utrecht",C-1005,2024-04-05,2027-04-05,9780321774620,Continuous Delivery,Addison-Wesley,Jez Humble; David Farley,devops; best-practices,BC-0036,D-01,2026-01-30 12:56,2026-02-20 12:56,2026-02-07 12:56,0.00 -priya@example.com,Priya,+31634567890,"Maliebaan 22, Utrecht",C-1003,2024-03-01,2027-03-01,9780321774620,Continuous Delivery,Addison-Wesley,Jez Humble; David Farley,devops; best-practices,BC-0036,D-01,2026-04-14 05:24,2026-05-05 05:24,2026-04-24 05:24,0.00 -priya@example.com,Priya,+31634567890,"Maliebaan 22, Utrecht",C-1003,2024-03-01,2027-03-01,9780321774620,Continuous Delivery,Addison-Wesley,Jez Humble; David Farley,devops; best-practices,BC-0038,D-01,2026-01-26 22:43,2026-02-16 22:43,2026-02-19 22:43,0.75 -priya@example.com,Priya,+31634567890,"Maliebaan 22, Utrecht",C-1003,2024-03-01,2027-03-01,9780134494166,Clean Architecture,Prentice Hall,Robert C. Martin,architecture; best-practices,BC-0033,C-01,2026-03-23 13:30,2026-04-13 13:30,2026-03-29 13:30,0.00 -james@example.com,James,+31667890123,"Catharijnesingel 14, Utrecht",C-1006,2023-09-12,2026-09-12,9780321774620,Continuous Delivery,Addison-Wesley,Jez Humble; David Farley,devops; best-practices,BC-0037,D-01,2026-05-08 01:26,2026-05-29 01:26,2026-05-27 01:26,0.00 -omar@example.com,Omar,+31689012345,"Rijnkade 19, Utrecht",C-1008,2024-01-30,2027-01-30,9781492056300,Designing Data-Intensive Applications,O'Reilly,Martin Kleppmann,databases; architecture; distributed-systems,BC-0035,A-02,2026-02-22 13:03,2026-03-15 13:03,2026-02-28 13:03,0.00 -yuki@example.com,Yuki,+31690123456,"Springweg 2, Utrecht",C-1009,2023-12-08,2026-12-08,9781617294945,Spring in Action,Manning,Craig Walls,java; spring; frameworks,BC-0027,D-01,2026-03-02 23:32,2026-03-23 23:32,2026-03-07 23:32,0.00 -nina@example.com,Nina,+31678901234,"Nobelstraat 7, Utrecht",C-1007,2024-05-22,2027-05-22,9780596007126,Head First Design Patterns,O'Reilly,Eric Freeman; Elisabeth Robson,design-patterns; java,BC-0024,A-02,2026-03-31 21:47,2026-04-21 21:47,2026-04-12 21:47,0.00 -nina@example.com,Nina,+31678901234,"Nobelstraat 7, Utrecht",C-1007,2024-05-22,2027-05-22,9780201633610,Design Patterns,Addison-Wesley,Erich Gamma; Richard Helm; Ralph Johnson; John Vlissides,design-patterns; oop,BC-0008,B-01,2026-02-10 21:05,2026-03-03 21:05,2026-02-14 21:05,0.00 -alex@example.com,Alex,+31623456789,"Lange Viestraat 5, Utrecht",C-1002,2024-02-15,2027-02-15,9780321774620,Continuous Delivery,Addison-Wesley,Jez Humble; David Farley,devops; best-practices,BC-0038,D-01,2025-12-28 09:32,2026-01-18 09:32,2026-01-05 09:32,0.00 -omar@example.com,Omar,+31689012345,"Rijnkade 19, Utrecht",C-1008,2024-01-30,2027-01-30,9780134685991,Effective Java,Addison-Wesley,Joshua Bloch,java; best-practices,BC-0001,B-02,2026-04-09 15:44,2026-04-30 15:44,2026-04-23 15:44,0.00 -chen@example.com,Chen,+31601234567,"Wittevrouwenstraat 31, Utrecht",C-1010,2024-06-01,2027-06-01,9780321125217,Domain-Driven Design,Addison-Wesley,Eric Evans,software-design; architecture,BC-0022,D-01,2025-12-16 16:13,2026-01-06 16:13,2025-12-26 16:13,0.00 -nina@example.com,Nina,+31678901234,"Nobelstraat 7, Utrecht",C-1007,2024-05-22,2027-05-22,9780132350884,Clean Code,Prentice Hall,Robert C. Martin,best-practices; software-design,BC-0005,C-01,2025-11-29 17:06,2025-12-20 17:06,2025-12-13 17:06,0.00 -lena@example.com,Lena,+31656789012,"Voorstraat 3, Utrecht",C-1005,2024-04-05,2027-04-05,9780132181815,The Clean Coder,Prentice Hall,Robert C. Martin,best-practices; career,BC-0029,D-01,2026-04-22 04:05,2026-05-13 04:05,2026-05-08 04:05,0.00 -omar@example.com,Omar,+31689012345,"Rijnkade 19, Utrecht",C-1008,2024-01-30,2027-01-30,9781491950357,Learning Python,O'Reilly,Mark Lutz,python; beginners,BC-0016,C-01,2026-04-05 10:05,2026-04-26 10:05,,6.75 -chen@example.com,Chen,+31601234567,"Wittevrouwenstraat 31, Utrecht",C-1010,2024-06-01,2027-06-01,9781491950357,Learning Python,O'Reilly,Mark Lutz,python; beginners,BC-0015,C-01,2026-01-10 16:55,2026-01-31 16:55,2026-01-29 16:55,0.00 -priya@example.com,Priya,+31634567890,"Maliebaan 22, Utrecht",C-1003,2024-03-01,2027-03-01,9780134494166,Clean Architecture,Prentice Hall,Robert C. Martin,architecture; best-practices,BC-0033,C-01,2026-04-28 13:17,2026-05-19 13:17,2026-05-10 13:17,0.00 -james@example.com,James,+31667890123,"Catharijnesingel 14, Utrecht",C-1006,2023-09-12,2026-09-12,9780201485677,Refactoring,Addison-Wesley,Martin Fowler,best-practices; java,BC-0041,B-01,2026-04-12 19:39,2026-05-03 19:39,2026-04-18 19:39,0.00 -omar@example.com,Omar,+31689012345,"Rijnkade 19, Utrecht",C-1008,2024-01-30,2027-01-30,9780596517748,JavaScript: The Good Parts,O'Reilly,Douglas Crockford,javascript; beginners,BC-0012,B-03,2026-04-18 09:31,2026-05-09 09:31,2026-04-28 09:31,0.00 -alex@example.com,Alex,+31623456789,"Lange Viestraat 5, Utrecht",C-1002,2024-02-15,2027-02-15,9780132350884,Clean Code,Prentice Hall,Robert C. Martin,best-practices; software-design,BC-0006,C-01,2026-03-20 04:12,2026-04-10 04:12,2026-04-19 04:12,2.25 -omar@example.com,Omar,+31689012345,"Rijnkade 19, Utrecht",C-1008,2024-01-30,2027-01-30,9780321125217,Domain-Driven Design,Addison-Wesley,Eric Evans,software-design; architecture,BC-0020,D-01,2026-03-17 03:57,2026-04-07 03:57,2026-04-02 03:57,0.00 -omar@example.com,Omar,+31689012345,"Rijnkade 19, Utrecht",C-1008,2024-01-30,2027-01-30,9780596517748,JavaScript: The Good Parts,O'Reilly,Douglas Crockford,javascript; beginners,BC-0011,B-03,2026-03-17 02:55,2026-04-07 02:55,2026-03-27 02:55,0.00 -nina@example.com,Nina,+31678901234,"Nobelstraat 7, Utrecht",C-1007,2024-05-22,2027-05-22,9780596517748,JavaScript: The Good Parts,O'Reilly,Douglas Crockford,javascript; beginners,BC-0012,B-03,2026-03-15 23:11,2026-04-05 23:11,2026-04-11 23:11,1.50 -nina@example.com,Nina,+31678901234,"Nobelstraat 7, Utrecht",C-1007,2024-05-22,2027-05-22,9780201633610,Design Patterns,Addison-Wesley,Erich Gamma; Richard Helm; Ralph Johnson; John Vlissides,design-patterns; oop,BC-0010,B-01,2026-01-14 09:15,2026-02-04 09:15,,27.00 -james@example.com,James,+31667890123,"Catharijnesingel 14, Utrecht",C-1006,2023-09-12,2026-09-12,9781491950357,Learning Python,O'Reilly,Mark Lutz,python; beginners,BC-0014,C-01,2025-12-22 00:00,2026-01-12 00:00,2026-01-15 00:00,0.75 -tom@example.com,Tom,+31645678901,"Biltstraat 88, Utrecht",C-1004,2023-11-20,2026-11-20,9780132350884,Clean Code,Prentice Hall,Robert C. Martin,best-practices; software-design,BC-0005,C-01,2026-03-02 13:05,2026-03-23 13:05,2026-03-10 13:05,0.00 -tom@example.com,Tom,+31645678901,"Biltstraat 88, Utrecht",C-1004,2023-11-20,2026-11-20,9780201485677,Refactoring,Addison-Wesley,Martin Fowler,best-practices; java,BC-0041,B-01,2026-05-16 06:07,2026-06-06 06:07,,0.00 -nina@example.com,Nina,+31678901234,"Nobelstraat 7, Utrecht",C-1007,2024-05-22,2027-05-22,9781492056300,Designing Data-Intensive Applications,O'Reilly,Martin Kleppmann,databases; architecture; distributed-systems,BC-0034,A-02,2026-03-24 17:05,2026-04-14 17:05,2026-04-04 17:05,0.00 -yuki@example.com,Yuki,+31690123456,"Springweg 2, Utrecht",C-1009,2023-12-08,2026-12-08,9780201633610,Design Patterns,Addison-Wesley,Erich Gamma; Richard Helm; Ralph Johnson; John Vlissides,design-patterns; oop,BC-0007,B-01,2025-12-30 21:57,2026-01-20 21:57,2026-01-07 21:57,0.00 -chen@example.com,Chen,+31601234567,"Wittevrouwenstraat 31, Utrecht",C-1010,2024-06-01,2027-06-01,9780134494166,Clean Architecture,Prentice Hall,Robert C. Martin,architecture; best-practices,BC-0031,C-01,2026-03-27 14:04,2026-04-17 14:04,,9.00 -alex@example.com,Alex,+31623456789,"Lange Viestraat 5, Utrecht",C-1002,2024-02-15,2027-02-15,9780132181815,The Clean Coder,Prentice Hall,Robert C. Martin,best-practices; career,BC-0028,D-01,2026-05-04 10:28,2026-05-25 10:28,,0.00 -priya@example.com,Priya,+31634567890,"Maliebaan 22, Utrecht",C-1003,2024-03-01,2027-03-01,9780132181815,The Clean Coder,Prentice Hall,Robert C. Martin,best-practices; career,BC-0029,D-01,2025-12-26 20:28,2026-01-16 20:28,2026-01-08 20:28,0.00 -lena@example.com,Lena,+31656789012,"Voorstraat 3, Utrecht",C-1005,2024-04-05,2027-04-05,9780596007126,Head First Design Patterns,O'Reilly,Eric Freeman; Elisabeth Robson,design-patterns; java,BC-0024,A-02,2026-04-22 16:21,2026-05-13 16:21,2026-05-02 16:21,0.00 -sam@example.com,Sam,+31612345678,"Oudegracht 10, Utrecht",C-1001,2024-01-10,2027-01-10,9780321774620,Continuous Delivery,Addison-Wesley,Jez Humble; David Farley,devops; best-practices,BC-0039,D-01,2026-05-16 06:51,2026-06-06 06:51,,0.00 -alex@example.com,Alex,+31623456789,"Lange Viestraat 5, Utrecht",C-1002,2024-02-15,2027-02-15,9780201633610,Design Patterns,Addison-Wesley,Erich Gamma; Richard Helm; Ralph Johnson; John Vlissides,design-patterns; oop,BC-0007,B-01,2026-01-12 23:14,2026-02-02 23:14,2026-02-10 23:14,2.00 -nina@example.com,Nina,+31678901234,"Nobelstraat 7, Utrecht",C-1007,2024-05-22,2027-05-22,9780201633610,Design Patterns,Addison-Wesley,Erich Gamma; Richard Helm; Ralph Johnson; John Vlissides,design-patterns; oop,BC-0008,B-01,2026-03-13 18:20,2026-04-03 18:20,2026-04-02 18:20,0.00 -nina@example.com,Nina,+31678901234,"Nobelstraat 7, Utrecht",C-1007,2024-05-22,2027-05-22,9780321125217,Domain-Driven Design,Addison-Wesley,Eric Evans,software-design; architecture,BC-0020,D-01,2026-04-07 04:41,2026-04-28 04:41,2026-04-11 04:41,0.00 -omar@example.com,Omar,+31689012345,"Rijnkade 19, Utrecht",C-1008,2024-01-30,2027-01-30,9781491950357,Learning Python,O'Reilly,Mark Lutz,python; beginners,BC-0015,C-01,2025-11-26 16:42,2025-12-17 16:42,2025-12-01 16:42,0.00 -james@example.com,James,+31667890123,"Catharijnesingel 14, Utrecht",C-1006,2023-09-12,2026-09-12,9781617294945,Spring in Action,Manning,Craig Walls,java; spring; frameworks,BC-0025,D-01,2026-04-03 12:56,2026-04-24 12:56,2026-04-09 12:56,0.00 -omar@example.com,Omar,+31689012345,"Rijnkade 19, Utrecht",C-1008,2024-01-30,2027-01-30,9781617294945,Spring in Action,Manning,Craig Walls,java; spring; frameworks,BC-0027,D-01,2026-04-21 18:58,2026-05-12 18:58,2026-05-02 18:58,0.00 -yuki@example.com,Yuki,+31690123456,"Springweg 2, Utrecht",C-1009,2023-12-08,2026-12-08,9780134685991,Effective Java,Addison-Wesley,Joshua Bloch,java; best-practices,BC-0002,B-02,2026-01-08 20:51,2026-01-29 20:51,2026-02-05 20:51,1.75 -priya@example.com,Priya,+31634567890,"Maliebaan 22, Utrecht",C-1003,2024-03-01,2027-03-01,9780321125217,Domain-Driven Design,Addison-Wesley,Eric Evans,software-design; architecture,BC-0021,D-01,2025-12-12 12:14,2026-01-02 12:14,2025-12-28 12:14,0.00 -tom@example.com,Tom,+31645678901,"Biltstraat 88, Utrecht",C-1004,2023-11-20,2026-11-20,9780596007126,Head First Design Patterns,O'Reilly,Eric Freeman; Elisabeth Robson,design-patterns; java,BC-0023,A-02,2026-05-15 01:20,2026-06-05 01:20,2026-06-11 01:20,1.50 -james@example.com,James,+31667890123,"Catharijnesingel 14, Utrecht",C-1006,2023-09-12,2026-09-12,9780134494166,Clean Architecture,Prentice Hall,Robert C. Martin,architecture; best-practices,BC-0033,C-01,2025-12-02 15:36,2025-12-23 15:36,2025-12-16 15:36,0.00 -sam@example.com,Sam,+31612345678,"Oudegracht 10, Utrecht",C-1001,2024-01-10,2027-01-10,9781491950357,Learning Python,O'Reilly,Mark Lutz,python; beginners,BC-0017,C-01,2026-05-19 18:25,2026-06-09 18:25,2026-05-29 18:25,0.00 -priya@example.com,Priya,+31634567890,"Maliebaan 22, Utrecht",C-1003,2024-03-01,2027-03-01,9780201485677,Refactoring,Addison-Wesley,Martin Fowler,best-practices; java,BC-0041,B-01,2026-01-06 04:53,2026-01-27 04:53,2026-01-26 04:53,0.00 -alex@example.com,Alex,+31623456789,"Lange Viestraat 5, Utrecht",C-1002,2024-02-15,2027-02-15,9780134494166,Clean Architecture,Prentice Hall,Robert C. Martin,architecture; best-practices,BC-0032,C-01,2025-12-05 12:37,2025-12-26 12:37,2025-12-24 12:37,0.00 -omar@example.com,Omar,+31689012345,"Rijnkade 19, Utrecht",C-1008,2024-01-30,2027-01-30,9780596517748,JavaScript: The Good Parts,O'Reilly,Douglas Crockford,javascript; beginners,BC-0013,B-03,2026-03-02 15:28,2026-03-23 15:28,2026-03-18 15:28,0.00 -sam@example.com,Sam,+31612345678,"Oudegracht 10, Utrecht",C-1001,2024-01-10,2027-01-10,9780137081073,The Pragmatic Programmer,Addison-Wesley,David Thomas; Andrew Hunt,best-practices; career,BC-0018,C-01,2026-04-10 22:51,2026-05-01 22:51,2026-04-18 22:51,0.00 -priya@example.com,Priya,+31634567890,"Maliebaan 22, Utrecht",C-1003,2024-03-01,2027-03-01,9781491950357,Learning Python,O'Reilly,Mark Lutz,python; beginners,BC-0015,C-01,2026-04-20 05:39,2026-05-11 05:39,2026-05-04 05:39,0.00 -tom@example.com,Tom,+31645678901,"Biltstraat 88, Utrecht",C-1004,2023-11-20,2026-11-20,9781491950357,Learning Python,O'Reilly,Mark Lutz,python; beginners,BC-0016,C-01,2026-02-01 04:12,2026-02-22 04:12,2026-03-01 04:12,1.75 -chen@example.com,Chen,+31601234567,"Wittevrouwenstraat 31, Utrecht",C-1010,2024-06-01,2027-06-01,9780132181815,The Clean Coder,Prentice Hall,Robert C. Martin,best-practices; career,BC-0029,D-01,2025-12-04 15:24,2025-12-25 15:24,2025-12-26 15:24,0.25 -chen@example.com,Chen,+31601234567,"Wittevrouwenstraat 31, Utrecht",C-1010,2024-06-01,2027-06-01,9781617294945,Spring in Action,Manning,Craig Walls,java; spring; frameworks,BC-0025,D-01,2026-02-09 13:01,2026-03-02 13:01,2026-02-22 13:01,0.00 -chen@example.com,Chen,+31601234567,"Wittevrouwenstraat 31, Utrecht",C-1010,2024-06-01,2027-06-01,9780596517748,JavaScript: The Good Parts,O'Reilly,Douglas Crockford,javascript; beginners,BC-0012,B-03,2026-04-30 17:43,2026-05-21 17:43,2026-05-04 17:43,0.00 -omar@example.com,Omar,+31689012345,"Rijnkade 19, Utrecht",C-1008,2024-01-30,2027-01-30,9780201485677,Refactoring,Addison-Wesley,Martin Fowler,best-practices; java,BC-0041,B-01,2026-03-11 18:49,2026-04-01 18:49,2026-04-05 18:49,1.00 -sam@example.com,Sam,+31612345678,"Oudegracht 10, Utrecht",C-1001,2024-01-10,2027-01-10,9780132350884,Clean Code,Prentice Hall,Robert C. Martin,best-practices; software-design,BC-0006,C-01,2026-05-04 18:49,2026-05-25 18:49,2026-05-23 18:49,0.00 -lena@example.com,Lena,+31656789012,"Voorstraat 3, Utrecht",C-1005,2024-04-05,2027-04-05,9781492056300,Designing Data-Intensive Applications,O'Reilly,Martin Kleppmann,databases; architecture; distributed-systems,BC-0034,A-02,2025-11-29 23:07,2025-12-20 23:07,2025-12-22 23:07,0.50 -alex@example.com,Alex,+31623456789,"Lange Viestraat 5, Utrecht",C-1002,2024-02-15,2027-02-15,9781491950357,Learning Python,O'Reilly,Mark Lutz,python; beginners,BC-0015,C-01,2025-12-06 22:01,2025-12-27 22:01,2025-12-18 22:01,0.00 -omar@example.com,Omar,+31689012345,"Rijnkade 19, Utrecht",C-1008,2024-01-30,2027-01-30,9780132181815,The Clean Coder,Prentice Hall,Robert C. Martin,best-practices; career,BC-0029,D-01,2026-02-11 15:54,2026-03-04 15:54,2026-02-19 15:54,0.00 -james@example.com,James,+31667890123,"Catharijnesingel 14, Utrecht",C-1006,2023-09-12,2026-09-12,9780134685991,Effective Java,Addison-Wesley,Joshua Bloch,java; best-practices,BC-0001,B-02,2026-03-09 18:37,2026-03-30 18:37,2026-03-26 18:37,0.00 -priya@example.com,Priya,+31634567890,"Maliebaan 22, Utrecht",C-1003,2024-03-01,2027-03-01,9780596007126,Head First Design Patterns,O'Reilly,Eric Freeman; Elisabeth Robson,design-patterns; java,BC-0023,A-02,2026-01-04 07:08,2026-01-25 07:08,2026-01-26 07:08,0.25 -yuki@example.com,Yuki,+31690123456,"Springweg 2, Utrecht",C-1009,2023-12-08,2026-12-08,9780596517748,JavaScript: The Good Parts,O'Reilly,Douglas Crockford,javascript; beginners,BC-0013,B-03,2025-12-20 21:10,2026-01-10 21:10,2025-12-24 21:10,0.00 -priya@example.com,Priya,+31634567890,"Maliebaan 22, Utrecht",C-1003,2024-03-01,2027-03-01,9780132350884,Clean Code,Prentice Hall,Robert C. Martin,best-practices; software-design,BC-0005,C-01,2026-01-04 04:52,2026-01-25 04:52,2026-01-23 04:52,0.00 -james@example.com,James,+31667890123,"Catharijnesingel 14, Utrecht",C-1006,2023-09-12,2026-09-12,9781492056300,Designing Data-Intensive Applications,O'Reilly,Martin Kleppmann,databases; architecture; distributed-systems,BC-0035,A-02,2026-04-10 15:31,2026-05-01 15:31,2026-05-10 15:31,2.25 diff --git a/task-1/schema.sql b/task-1/schema.sql index a3b443b..6308184 100644 --- a/task-1/schema.sql +++ b/task-1/schema.sql @@ -1 +1,125 @@ --- Write here your DDL statements to create the tables for the database. \ No newline at end of file +-- Write here your DDL statements to create the tables for the database. + + + + +- +CREATE TABLE cards +( + card_id SERIAL PRIMARY KEY, + card_number TEXT UNIQUE NOT NULL, + card_issued_on DATE NOT NULL, + card_expires_on DATE NOT NULL, + CHECK (card_expires_on > card_issued_on) +); + + +CREATE TABLE members +( + member_id SERIAL PRIMARY KEY, + card_id INTEGER UNIQUE, + member_email TEXT UNIQUE NOT NULL, + member_name TEXT NOT NULL, + member_phone TEXT NOT NULL, + member_address TEXT NOT NULL, + CONSTRAINT fk_card_id + FOREIGN KEY (card_id) + REFERENCES cards (card_id) ON DELETE CASCADE +); + + +CREATE TABLE publishers +( + publisher_id SERIAL PRIMARY KEY, + publisher_name TEXT UNIQUE NOT NULL +); + + +CREATE TABLE authors +( + author_id SERIAL PRIMARY KEY, + author_name TEXT NOT NULL +); + + +CREATE TABLE tags +( + tag_id SERIAL PRIMARY KEY, + tag_name VARCHAR(25) UNIQUE NOT NULL +); + + +CREATE TABLE books +( + book_id SERIAL PRIMARY KEY, + book_isbn VARCHAR(13) UNIQUE NOT NULL, + book_title TEXT NOT NULL, + publisher_id INTEGER NOT NULL, + CONSTRAINT fk_publisher_id + FOREIGN KEY(publisher_id) + REFERENCES publishers (publisher_id) +); + + +CREATE TABLE book_authors +( + book_id INTEGER NOT NULL, + author_id INTEGER NOT NULL, + PRIMARY KEY (book_id, author_id), + FOREIGN KEY(book_id) REFERENCES books (book_id) ON DELETE CASCADE, + FOREIGN KEY(author_id) REFERENCES authors(author_id) ON DELETE CASCADE + +); + +CREATE TABLE book_tags +( + book_id INTEGER NOT NULL, + tag_id INTEGER NOT NULL, + PRIMARY KEY (book_id, tag_id), + FOREIGN KEY(book_id) REFERENCES books(book_id) ON DELETE CASCADE, + FOREIGN KEY(tag_id) REFERENCES tags(tag_id) ON DELETE CASCADE +); + + +CREATE TABLE shelves +( + shelf_id SERIAL PRIMARY KEY, + shelf_code TEXT UNIQUE NOT NULL +); + + +CREATE TABLE copies +( + copy_id SERIAL PRIMARY KEY, + copy_barcode TEXT UNIQUE NOT NULL, + book_id INTEGER NOT NULL, + shelf_id INTEGER, + CONSTRAINT fk_book_id + FOREIGN KEY (book_id) + REFERENCES books(book_id) ON DELETE CASCADE, + CONSTRAINT fk_shelf_id + FOREIGN KEY (shelf_id) + REFERENCES shelves(shelf_id) +); + +CREATE TABLE borrows +( + borrow_id SERIAL PRIMARY KEY, + member_id INTEGER NOT NULL, + copy_id INTEGER NOT NULL, + borrowed_at TIMESTAMP NOT NULL, + due_date TIMESTAMP NOT NULL, + returned_at TIMESTAMP, + fine_eur NUMERIC(8,2) NOT NULL DEFAULT 0.00, + + CHECK (due_date >= borrowed_at ), + CHECK (returned_at IS NULL OR returned_at >= borrowed_at), + CHECK (fine_eur >= 0.00), + CONSTRAINT fk_member_id + FOREIGN KEY (member_id) + REFERENCES members(member_id), + CONSTRAINT fk_copy_id + FOREIGN KEY (copy_id) + REFERENCES copies(copy_id) +); + diff --git a/task-1/seed.sql b/task-1/seed.sql new file mode 100644 index 0000000..7373776 --- /dev/null +++ b/task-1/seed.sql @@ -0,0 +1,308 @@ + +INSERT INTO cards(card_id, card_number, card_issued_on, card_expires_on) VALUES + (1001, 'C-1001', '2024-01-10', '2027-01-10'), +(1002, 'C-1002', '2024-02-15', '2027-02-15'), +(1003, 'C-1003', '2024-03-01', '2027-03-01'), +(1004, 'C-1004', '2023-11-20', '2026-11-20'), +(1005, 'C-1005', '2024-04-05', '2027-04-05'), +(1006, 'C-1006', '2023-09-12', '2026-09-12'), +(1007, 'C-1007', '2024-05-22', '2027-05-22'), +(1008, 'C-1008', '2024-01-30', '2027-01-30'), +(1009, 'C-1009', '2023-12-08', '2026-12-08'), +(1010, 'C-1010', '2024-06-01', '2027-06-01'); + +INSERT INTO members ( member_id, card_id, member_email, member_name, member_phone, member_address) VALUES + (1, 1001, 'sam@example.com', 'Sam', 31612345678, 'Oudegracht 10, Utrecht'), + (2, 1002, 'alex@example.com', 'Alex', 31623456789, 'Lange Viestraat 5, Utrecht'), + (3, 1003, 'priya@example.com', 'Priya', 31634567890, 'Maliebaan 22, Utrecht'), + (4, 1004, 'tom@example.com', 'Tom', 31645678901, 'Biltstraat 88, Utrecht'), + (5, 1005, 'lena@example.com', 'Lena', 31656789012, 'Voorstraat 3, Utrecht'), + (6, 1006, 'james@example.com', 'James', 31667890123, 'Catharijnesingel 14, Utrecht'), + (7, 1007, 'nina@example.com', 'Nina', 31678901234, 'Nobelstraat 7, Utrecht'), + (8, 1008, 'omar@example.com', 'Omar', 31689012345, 'Rijnkade 19, Utrecht'), + (9, 1009, 'yuki@example.com', 'Yuki', 31690123456, 'Springweg 2, Utrecht'), + (10, 1010, 'chen@example.com', 'Chen', 31601234567, 'Wittevrouwenstraat 31, Utrecht'); + +INSERT INTO publishers (publisher_id, publisher_name) VALUES + (1, 'Addison-Wesley'), + (2, 'Manning'), + (3, 'O`Reilly'), + (4, 'Prentice Hall'); + +INSERT INTO authors (author_id, author_name) VALUES +(1, 'Andrew Hunt'), +(2, 'Bert Bates'), +(3, 'Craig Walls'), +(4, 'David Farley'), +(5, 'David Thomas'), +(6, 'Douglas Crockford'), +(7, 'Elisabeth Robson'), +(8, 'Eric Evans'), +(9, 'Eric Freeman'), +(10, 'Erich Gamma'), +(11, 'Jez Humble'), +(12, 'John Vlissides'), +(13, 'Joshua Bloch'), +(14, 'Kathy Sierra'), +(15, 'Mark Lutz'), +(16, 'Martin Fowler'), +(17, 'Martin Kleppmann'), +(18, 'Ralph Johnson'), +(19, 'Richard Helm'), +(20, 'Robert C. Martin'); + +INSERT INTO tags(tag_id, tag_name) VALUES +(1, 'architecture'), +(2, 'beginners'), +(3, 'best-practices'), +(4, 'career'), +(5, 'databases'), +(6, 'design-patterns'), +(7, 'devops'), +(8, 'distributed-systems'), +(9, 'frameworks'), +(10, 'java'), +(11, 'javascript'), +(12, 'oop'), +(13, 'python'), +(14, 'software-design'), +(15, 'spring'); + +INSERT INTO books (book_id, book_isbn, book_title, publisher_id) VALUES +(1, 9780134494166, 'Clean Architecture', 4), +(2, 9780132350884, 'Clean Code', 4), +(3, 9780321774620, 'Continuous Delivery', 1), +(4, 9780201633610, 'Design Patterns', 1), +(5, 9781492056300, 'Designing Data-Intensive Applications', 3), +(6, 9780321125217, 'Domain-Driven Design', 1), +(7, 9780134685991, 'Effective Java', 1), +(8, 9780596007126, 'Head First Design Patterns', 3), +(9, 9780596009205, 'Head First Java', 3), +(10, 9780596517748, 'JavaScript: The Good Parts', 3), +(11, 9781491950357, 'Learning Python', 3), +(12, 9780201485677, 'Refactoring', 1), +(13, 9781617294945, 'Spring in Action', 2), +(14, 9780132181815, 'The Clean Coder', 4), +(15, 9780137081073, 'The Pragmatic Programmer', 1); + +INSERT INTO book_authors (book_id, author_id) VALUES +(1, 20), +(2, 20), +(3, 11), +(3, 4), +(4, 10), +(4, 19), +(4, 18), +(4, 12), +(5, 17), +(6, 8), +(7, 13), +(8, 9), +(8, 7), +(9, 14), +(9, 2), +(10, 6), +(11, 15), +(12, 16), +(13, 3), +(14, 20), +(15, 5), +(15, 1); + + +INSERT INTO book_tags (book_id, tag_id) VALUES +(1, 1), +(1, 3), +(2, 3), +(2, 14), +(3, 7), +(3, 3), +(4, 6), +(4, 12), +(5, 5), +(5, 1), +(5, 8), +(6, 14), +(6, 1), +(7, 10), +(7, 3), +(8, 6), +(8, 10), +(9, 10), +(9, 2), +(10, 11), +(10, 2), +(11, 13), +(11, 2), +(12, 3), +(12, 10), +(13, 10), +(13, 15), +(13, 9), +(14, 3), +(14, 4), +(15, 3), +(15, 4); + +INSERT INTO shelves (shelf_id, shelf_code) VALUES + (1, 'A-02'), + (2, 'B-01'), + (3, 'B-02'), + (4, 'B-03'), + (5, 'C-01'), + (6, 'D-01'); + +INSERT INTO copies (copy_id, copy_barcode, book_id, shelf_id) VALUES +(1, 'BC-0001',7, 3), +(2, 'BC-0002',7, 3), +(3, 'BC-0003',9, 1), +(4, 'BC-0004',9, 1), +(5, 'BC-0005',2, 5), +(6, 'BC-0006',2, 5), +(7, 'BC-0007',4, 2), +(8, 'BC-0008', 4, 2), +(9, 'BC-0009',4, 2), +(10, 'BC-0010',4, 2), +(11, 'BC-0011',10, 4), +(12, 'BC-0012',10, 4), +(13, 'BC-0013',10, 4), +(14, 'BC-0014',11, 5), +(15, 'BC-0015',11, 5), +(16, 'BC-0016',11, 5), +(17, 'BC-0017',11, 5), +(18, 'BC-0018',15, 5), +(19, 'BC-0019',15, 5), +(20, 'BC-0020',6, 6), +(21, 'BC-0021',6, 6), +(22, 'BC-0022',6, 6), +(23, 'BC-0023',8, 1), +(24, 'BC-0024',8, 1), +(25, 'BC-0025',13, 6), +(26, 'BC-0026',13, 6), +(27, 'BC-0027',13, 6), +(28, 'BC-0028', 14, 6), +(29, 'BC-0029',14, 6), +(30, 'BC-0030',1, 5), +(31, 'BC-0031', 1, 5), +(32, 'BC-0032',1, 5), +(33, 'BC-0033',1, 5), +(34, 'BC-0034',5, 1), +(35, 'BC-0035',5, 1), +(36, 'BC-0036',3, 6), +(37, 'BC-0037',3, 6), +(38, 'BC-0038',3, 6), +(39, 'BC-0039',3, 6), +(40, 'BC-0040',12, 2), +(41, 'BC-0041',12, 2); + +INSERT INTO borrows (borrow_id, member_id, copy_id, borrowed_at, due_date, returned_at, fine_eur) VALUES +(1, 8, 15, '2025-11-26 16:42', '2025-12-17 16:42', '2025-12-01 16:42', 0.00), +(2, 7, 5, '2025-11-29 17:06', '2025-12-20 17:06', '2025-12-13 17:06', 0.00), +(3, 5, 34, '2025-11-29 23:07', '2025-12-20 23:07', '2025-12-22 23:07', 0.50), +(4, 8, 11, '2025-12-02 08:08', '2025-12-23 08:08', '2025-12-13 08:08', 0.00), +(5, 7, 19, '2025-12-02 14:01', '2025-12-23 14:01', NULL, 37.75), +(6, 6, 33, '2025-12-02 15:36', '2025-12-23 15:36', '2025-12-16 15:36', 0.00), +(7, 10, 29, '2025-12-04 15:24', '2025-12-25 15:24', '2025-12-26 15:24', 0.25), +(8, 2, 32, '2025-12-05 12:37', '2025-12-26 12:37', '2025-12-24 12:37', 0.00), +(9, 2, 15, '2025-12-06 22:01', '2025-12-27 22:01', '2025-12-18 22:01', 0.00), +(10, 4, 25, '2025-12-08 13:07', '2025-12-29 13:07', '2025-12-18 13:07', 0.00), +(11, 10, 40, '2025-12-08 23:42', '2025-12-29 23:42', NULL, 36.00), +(12, 3, 17, '2025-12-11 06:06', '2026-01-01 06:06', '2026-01-06 06:06', 1.25), +(13, 3, 21, '2025-12-12 12:14', '2026-01-02 12:14', '2025-12-28 12:14', 0.00), +(14, 5, 28, '2025-12-14 19:38', '2026-01-04 19:38', '2025-12-17 19:38', 0.00), +(15, 1, 36, '2025-12-15 01:01', '2026-01-05 01:01', '2026-01-04 01:01', 0.00), +(16, 10, 22, '2025-12-16 16:13', '2026-01-06 16:13', '2025-12-26 16:13', 0.00), +(17, 9, 13, '2025-12-20 21:10', '2026-01-10 21:10', '2025-12-24 21:10', 0.00), +(18, 1, 24, '2025-12-21 23:02', '2026-01-11 23:02', '2026-01-14 23:02', 0.75), +(19, 6, 14, '2025-12-22 00:00', '2026-01-12 00:00', '2026-01-15 00:00', 0.75), +(20, 3, 29, '2025-12-26 20:28', '2026-01-16 20:28', '2026-01-08 20:28', 0.00), +(21, 2, 38, '2025-12-28 09:32', '2026-01-18 09:32', '2026-01-05 09:32', 0.00), +(22, 9, 7, '2025-12-30 21:57', '2026-01-20 21:57', '2026-01-07 21:57', 0.00), +(23, 3, 5, '2026-01-04 04:52', '2026-01-25 04:52', '2026-01-23 04:52', 0.00), +(24, 3, 23, '2026-01-04 07:08', '2026-01-25 07:08', '2026-01-26 07:08', 0.25), +(25, 3, 41, '2026-01-06 04:53', '2026-01-27 04:53', '2026-01-26 04:53', 0.00), +(26, 9, 2, '2026-01-08 20:51', '2026-01-29 20:51', '2026-02-05 20:51', 1.75), +(27, 4, 13, '2026-01-10 08:51', '2026-01-31 08:51', '2026-01-25 08:51', 0.00), +(28, 1, 26, '2026-01-10 13:15', '2026-01-31 13:15', NULL, 28.00), +(29, 10, 15, '2026-01-10 16:55', '2026-01-31 16:55', '2026-01-29 16:55', 0.00), +(30, 6, 37, '2026-01-12 18:47', '2026-02-02 18:47', '2026-01-25 18:47', 0.00), +(31, 2, 7, '2026-01-12 23:14', '2026-02-02 23:14', '2026-02-10 23:14', 2.00), +(32, 7, 10, '2026-01-14 09:15', '2026-02-04 09:15', NULL, 27.00), +(33, 3, 25, '2026-01-15 13:38', '2026-02-05 13:38', '2026-01-18 13:38', 0.00), +(34, 3, 38, '2026-01-26 22:43', '2026-02-16 22:43', '2026-02-19 22:43', 0.75), +(35, 7, 9, '2026-01-28 05:02', '2026-02-18 05:02', '2026-02-07 05:02', 0.00), +(36, 5, 36, '2026-01-30 12:56', '2026-02-20 12:56', '2026-02-07 12:56', 0.00), +(37, 4, 16, '2026-02-01 04:12', '2026-02-22 04:12', '2026-03-01 04:12', 1.75), +(38, 10, 25, '2026-02-09 13:01', '2026-03-02 13:01', '2026-02-22 13:01', 0.00), +(39, 7, 8, '2026-02-10 21:05', '2026-03-03 21:05', '2026-02-14 21:05', 0.00), +(40, 8, 29, '2026-02-11 15:54', '2026-03-04 15:54', '2026-02-19 15:54', 0.00), +(41, 8, 36, '2026-02-15 11:43', '2026-03-08 11:43', '2026-03-02 11:43', 0.00), +(42, 10, 28, '2026-02-16 01:36', '2026-03-09 01:36', '2026-03-06 01:36', 0.00), +(43, 3, 39, '2026-02-16 14:32', '2026-03-09 14:32', '2026-02-24 14:32', 0.00), +(44, 9, 3, '2026-02-18 18:15', '2026-03-11 18:15', '2026-02-23 18:15', 0.00), +(45, 8, 35, '2026-02-22 13:03', '2026-03-15 13:03', '2026-02-28 13:03', 0.00), +(46, 4, 18, '2026-02-23 11:30', '2026-03-16 11:30', '2026-03-05 11:30', 0.00), +(47, 3, 30, '2026-02-28 05:49', '2026-03-21 05:49', NULL, 15.75), +(48, 4, 5, '2026-03-02 13:05', '2026-03-23 13:05', '2026-03-10 13:05', 0.00), +(49, 8, 13, '2026-03-02 15:28', '2026-03-23 15:28', '2026-03-18 15:28', 0.00), +(50, 9, 27, '2026-03-02 23:32', '2026-03-23 23:32', '2026-03-07 23:32', 0.00), +(51, 6, 1, '2026-03-09 18:37', '2026-03-30 18:37', '2026-03-26 18:37', 0.00), +(52, 10, 37, '2026-03-11 01:13', '2026-04-01 01:13', '2026-03-27 01:13', 0.00), +(53, 8, 41, '2026-03-11 18:49', '2026-04-01 18:49', '2026-04-05 18:49', 1.00), +(54, 7, 8, '2026-03-13 18:20', '2026-04-03 18:20', '2026-04-02 18:20', 0.00), +(55, 8, 15, '2026-03-14 20:16', '2026-04-04 20:16', '2026-04-06 20:16', 0.50), +(56, 7, 12, '2026-03-15 23:11', '2026-04-05 23:11', '2026-04-11 23:11', 1.50), +(57, 8, 29, '2026-03-16 11:54', '2026-04-06 11:54', '2026-04-15 11:54', 2.25), +(58, 8, 11, '2026-03-17 02:55', '2026-04-07 02:55', '2026-03-27 02:55', 0.00), +(59, 8, 20, '2026-03-17 03:57', '2026-04-07 03:57', '2026-04-02 03:57', 0.00), +(60, 2, 9, '2026-03-17 17:29', '2026-04-07 17:29', '2026-03-23 17:29', 0.00), +(61, 2, 6, '2026-03-20 04:12', '2026-04-10 04:12', '2026-04-19 04:12', 2.25), +(62, 6, 16, '2026-03-21 05:22', '2026-04-11 05:22', '2026-04-02 05:22', 0.00), +(63, 1, 5, '2026-03-22 05:23', '2026-04-12 05:23', '2026-04-04 05:23', 0.00), +(64, 3, 33, '2026-03-23 13:30', '2026-04-13 13:30', '2026-03-29 13:30', 0.00), +(65, 7, 34, '2026-03-24 17:05', '2026-04-14 17:05', '2026-04-04 17:05', 0.00), +(66, 10, 31, '2026-03-27 14:04', '2026-04-17 14:04', NULL, 9.00), +(67, 2, 4, '2026-03-30 21:11', '2026-04-20 21:11', '2026-04-08 21:11', 0.00), +(68, 3, 21, '2026-03-31 18:59', '2026-04-21 18:59', '2026-04-29 18:59', 2.00), +(69, 7, 24, '2026-03-31 21:47', '2026-04-21 21:47', '2026-04-12 21:47', 0.00), +(70, 9, 14, '2026-04-01 10:42', '2026-04-22 10:42', '2026-04-05 10:42', 0.00), +(71, 5, 8, '2026-04-03 11:47', '2026-04-24 11:47', '2026-04-13 11:47', 0.00), +(72, 6, 25, '2026-04-03 12:56', '2026-04-24 12:56', '2026-04-09 12:56', 0.00), +(73, 8, 16, '2026-04-05 10:05', '2026-04-26 10:05', NULL, 6.75), +(74, 7, 20, '2026-04-07 04:41', '2026-04-28 04:41', '2026-04-11 04:41', 0.00), +(75, 8, 1, '2026-04-09 15:44', '2026-04-30 15:44', '2026-04-23 15:44', 0.00), +(76, 6, 35, '2026-04-10 15:31', '2026-05-01 15:31', '2026-05-10 15:31', 2.25), +(77, 1, 18, '2026-04-10 22:51', '2026-05-01 22:51', '2026-04-18 22:51', 0.00), +(78, 6, 41, '2026-04-12 19:39', '2026-05-03 19:39', '2026-04-18 19:39', 0.00), +(79, 3, 36, '2026-04-14 05:24', '2026-05-05 05:24', '2026-04-24 05:24', 0.00), +(80, 8, 12, '2026-04-18 09:31', '2026-05-09 09:31', '2026-04-28 09:31', 0.00), +(81, 3, 15, '2026-04-20 05:39', '2026-05-11 05:39', '2026-05-04 05:39', 0.00), +(82, 8, 27, '2026-04-21 18:58', '2026-05-12 18:58', '2026-05-02 18:58', 0.00), +(83, 5, 29, '2026-04-22 04:05', '2026-05-13 04:05', '2026-05-08 04:05', 0.00), +(84, 5, 24, '2026-04-22 16:21', '2026-05-13 16:21', '2026-05-02 16:21', 0.00), +(85, 1, 7, '2026-04-26 09:59', '2026-05-17 09:59', '2026-05-07 09:59', 0.00), +(86, 3, 33, '2026-04-28 13:17', '2026-05-19 13:17', '2026-05-10 13:17', 0.00), +(87, 10, 12, '2026-04-30 17:43', '2026-05-21 17:43', '2026-05-04 17:43', 0.00), +(88, 2, 11, '2026-05-02 02:49', '2026-05-23 02:49', NULL, 0.00), +(89, 2, 28, '2026-05-04 10:28', '2026-05-25 10:28', NULL, 0.00), +(90, 1, 6, '2026-05-04 18:49', '2026-05-25 18:49', '2026-05-23 18:49', 0.00), +(91, 6, 37, '2026-05-08 01:26', '2026-05-29 01:26', '2026-05-27 01:26', 0.00), +(92, 2, 20, '2026-05-08 22:34', '2026-05-29 22:34', NULL, 0.00), +(93, 5, 32, '2026-05-12 04:26', '2026-06-02 04:26', NULL, 0.00), +(94, 4, 23, '2026-05-15 01:20', '2026-06-05 01:20', '2026-06-11 01:20', 1.50), +(95, 4, 41, '2026-05-16 06:07', '2026-06-06 06:07', NULL, 0.00), +(96, 1, 39, '2026-05-16 06:51', '2026-06-06 06:51', NULL, 0.00), +(97, 9, 29, '2026-05-17 21:26', '2026-06-07 21:26', '2026-06-12 21:26', 1.25), +(98, 9, 3, '2026-05-18 04:02', '2026-06-08 04:02', NULL, 0.00), +(99, 1, 17, '2026-05-19 18:25', '2026-06-09 18:25', '2026-05-29 18:25', 0.00), +(100, 3, 9, '2026-05-19 23:46', '2026-06-09 23:46', '2026-06-05 23:46', 0.00); + +SELECT setval('cards_card_id_seq', 1010); +SELECT setval('members_member_id_seq', 10); +SELECT setval('publishers_publisher_id_seq', 4); +SELECT setval('authors_author_id_seq', 20); +SELECT setval('tags_tag_id_seq', 15); +SELECT setval('books_book_id_seq', 15); +SELECT setval('shelves_shelf_id_seq', 6); +SELECT setval('copies_copy_id_seq', 41); +SELECT setval('borrows_borrow_id_seq', 100); diff --git a/task-2/src/main/java/org/hyf/Main.java b/task-2/src/main/java/org/hyf/Main.java index 7be95f2..6c5fd8c 100644 --- a/task-2/src/main/java/org/hyf/Main.java +++ b/task-2/src/main/java/org/hyf/Main.java @@ -1,7 +1,270 @@ package org.hyf; +import java.math.BigDecimal; +import java.sql.*; + public class Main { - public static void main(String[] args) { - System.out.println("You code goes here :) You can overwrite this entire method."); + public static void main(String[] args) throws SQLException { + String url = "jdbc:postgresql://localhost:5432/library"; + String user = "postgres"; + String password = "postgres"; + + try (Connection connection = DriverManager.getConnection(url, user, password)) { + System.out.println("Connected to database"); + + insertData(connection); + fetchData(connection); + } catch (SQLException e) { + e.printStackTrace(); + } + } + + private static void insertData(Connection connection) throws SQLException { + System.out.println("Inserting demo data..."); + + // Insert into publishers + String insertPublisher = "INSERT INTO publishers (publisher_name) VALUES (?) RETURNING publisher_id"; + int publisherId1, publisherId2; + try (PreparedStatement ps = connection.prepareStatement(insertPublisher)) { + ps.setString(1, "Tech Books Publishing"); + ResultSet rs1 = ps.executeQuery(); + rs1.next(); + publisherId1 = rs1.getInt(1); + + ps.setString(1, "Future Minds Press"); + ResultSet rs2 = ps.executeQuery(); + rs2.next(); + publisherId2 = rs2.getInt(1); + } + System.out.println("Publishers data inserted"); + + // Insert into authors + String insertAuthor = "INSERT INTO authors(author_name) VALUES (?) RETURNING author_id"; + int authorId1, authorId2; + try (PreparedStatement ps = connection.prepareStatement(insertAuthor)) { + ps.setString(1, "Ada Lovelace"); + ResultSet rs1 = ps.executeQuery(); + rs1.next(); + authorId1 = rs1.getInt(1); + + ps.setString(1, "Niklaus Wirth"); + ResultSet rs2 = ps.executeQuery(); + rs2.next(); + authorId2 = rs2.getInt(1); + } + System.out.println("Authors data inserted"); + + // Insert into tags + String insertTag = "INSERT INTO tags(tag_name) VALUES (?) RETURNING tag_id"; + int tagId1, tagId2; + try (PreparedStatement ps = connection.prepareStatement(insertTag)) { + ps.setString(1, "computing"); + ResultSet rs1 = ps.executeQuery(); + rs1.next(); + tagId1 = rs1.getInt(1); + + ps.setString(1, "innovation"); + ResultSet rs2 = ps.executeQuery(); + rs2.next(); + tagId2 = rs2.getInt(1); + } + System.out.println("Tags data inserted"); + + // Insert into shelves + String insertShelf = "INSERT INTO shelves(shelf_code) VALUES (?) RETURNING shelf_id"; + int shelfId1, shelfId2; + try (PreparedStatement ps = connection.prepareStatement(insertShelf)) { + ps.setString(1, "D-02"); + ResultSet rs1 = ps.executeQuery(); + rs1.next(); + shelfId1 = rs1.getInt(1); + + ps.setString(1, "E-01"); + ResultSet rs2 = ps.executeQuery(); + rs2.next(); + shelfId2 = rs2.getInt(1); + } + System.out.println("Shelves data inserted"); + + // Insert into books + String insertBook = "INSERT INTO books(book_isbn, book_title, publisher_id) VALUES (?, ?, ?) RETURNING book_id"; + int bookId1, bookId2; + try (PreparedStatement ps = connection.prepareStatement(insertBook)) { + ps.setString(1, "979013708"); + ps.setString(2, "The Future of Computing"); + ps.setInt(3, publisherId1); + ResultSet rs1 = ps.executeQuery(); + rs1.next(); + bookId1 = rs1.getInt(1); + + ps.setString(1, "9790137081"); + ps.setString(2, "Algorithms Reimagined"); + ps.setInt(3, publisherId2); + ResultSet rs2 = ps.executeQuery(); + rs2.next(); + bookId2 = rs2.getInt(1); + } + System.out.println("Books data inserted"); + + // Insert into cards + String insertCard = "INSERT INTO cards(card_number, card_issued_on, card_expires_on) VALUES (?, ?, ?) RETURNING card_id"; + int cardId1, cardId2; + try (PreparedStatement ps = connection.prepareStatement(insertCard)) { + ps.setString(1, "C-1011"); + ps.setDate(2, Date.valueOf("2026-06-01")); + ps.setDate(3, Date.valueOf("2029-06-01")); + ResultSet rs1 = ps.executeQuery(); + rs1.next(); + cardId1 = rs1.getInt(1); + + ps.setString(1, "C-1012"); + ps.setDate(2, Date.valueOf("2026-06-02")); + ps.setDate(3, Date.valueOf("2029-06-02")); + ResultSet rs2 = ps.executeQuery(); + rs2.next(); + cardId2 = rs2.getInt(1); + } + System.out.println("Cards data inserted"); + + // Insert into members + String insertMember = "INSERT INTO members(card_id, member_email, member_name, member_phone, member_address) VALUES (?, ?, ?, ?, ?) RETURNING member_id"; + int memberId1, memberId2; + try (PreparedStatement ps = connection.prepareStatement(insertMember)) { + ps.setInt(1, cardId1); + ps.setString(2, "elen_jo@example.com"); + ps.setString(3, "Elen Jo"); + ps.setString(4, "31601234567"); + ps.setString(5, "Visweg 17, Utrecht"); + ResultSet rs1 = ps.executeQuery(); + rs1.next(); + memberId1 = rs1.getInt(1); + + ps.setInt(1, cardId2); + ps.setString(2, "glen_marti@example.com"); + ps.setString(3, "Glen Marti"); + ps.setString(4, "31601234567"); + ps.setString(5, "Fruitweg 71, Utrecht"); + ResultSet rs2 = ps.executeQuery(); + rs2.next(); + memberId2 = rs2.getInt(1); + + } + System.out.println("Members data inserted"); + + // Insert into copies + String insertCopy = "INSERT INTO copies (copy_barcode, book_id, shelf_id) VALUES (?, ?, ?) RETURNING copy_id"; + int copyId1, copyId2; + try (PreparedStatement ps = connection.prepareStatement(insertCopy)) { + ps.setString(1, "BC-0042"); + ps.setInt(2, bookId1); + ps.setInt(3, shelfId1); + ResultSet rs1 = ps.executeQuery(); + rs1.next(); + copyId1 = rs1.getInt(1); + + ps.setString(1, "BC-0043"); + ps.setInt(2, bookId2); + ps.setInt(3, shelfId2); + ResultSet rs2 = ps.executeQuery(); + rs2.next(); + copyId2 = rs2.getInt(1); + } + System.out.println("Copies data inserted"); + + // Insert into borrows + String insertBorrows = "INSERT INTO borrows(member_id, copy_id, borrowed_at, due_date, returned_at, fine_eur) VALUES (?, ?, ?, ?, ?, ?)"; + try (PreparedStatement ps = connection.prepareStatement(insertBorrows)) { + ps.setInt(1, memberId1); + ps.setInt(2, copyId1); + ps.setTimestamp(3, Timestamp.valueOf("2026-06-01 10:00:00")); + ps.setTimestamp(4, Timestamp.valueOf("2026-07-01 10:00:00")); + ps.setTimestamp(5, null); + ps.setBigDecimal(6, BigDecimal.ZERO); + ps.executeUpdate(); + + ps.setInt(1, memberId2); + ps.setInt(2, copyId2); + ps.setTimestamp(3, Timestamp.valueOf("2026-04-04 10:00:00")); + ps.setTimestamp(4, Timestamp.valueOf("2026-05-04 10:00:00")); + ps.setTimestamp(5, null); + ps.setBigDecimal(6, new BigDecimal("4.15")); + ps.executeUpdate(); + } + System.out.println("Borrows data inserted"); + + // Insert into book_authors + String insertBookAuthor = "INSERT INTO book_authors(book_id, author_id) VALUES (?, ?)"; + try (PreparedStatement ps = connection.prepareStatement(insertBookAuthor)) { + ps.setInt(1, bookId1); + ps.setInt(2, authorId1); + ps.executeUpdate(); + + ps.setInt(1, bookId2); + ps.setInt(2, authorId1); + ps.executeUpdate(); + + ps.setInt(1, bookId2); + ps.setInt(2, authorId2); + ps.executeUpdate(); + } + System.out.println("Book_author data inserted"); + + // Insert into book_tags + String insertBookTag = "INSERT INTO book_tags(book_id, tag_id) VALUES (?, ?)"; + try (PreparedStatement ps = connection.prepareStatement(insertBookTag)) { + ps.setInt(1, bookId1); + ps.setInt(2, tagId1); + ps.executeUpdate(); + + ps.setInt(1, bookId1); + ps.setInt(2, tagId2); + ps.executeUpdate(); + + ps.setInt(1, bookId2); + ps.setInt(2, tagId2); + ps.executeUpdate(); + } + System.out.println("Book_tag data inserted"); + System.out.println("All data successfully inserted"); + + } + + private static void fetchData(Connection connection) throws SQLException{ + System.out.println("\nFetching data..."); + + String sql = """ + SELECT + br.borrow_id, + m.member_name, + b.book_title, + br.borrowed_at, + br.due_date, + br.returned_at, + br.fine_eur + FROM borrows br + JOIN members m ON br.member_id = m.member_id + JOIN copies c ON br.copy_id = c.copy_id + JOIN books b ON c.book_id = b.book_id + ORDER by br.borrow_id + """; + + try (PreparedStatement ps = connection.prepareStatement(sql); + ResultSet rs = ps.executeQuery()) { + + while (rs.next()) { + int borrowId = rs.getInt("borrow_id"); + String memberName = rs.getString("member_name"); + String bookTitle = rs.getString("book_title"); + Timestamp borrowedAt = rs.getTimestamp("borrowed_at"); + Timestamp dueDate = rs.getTimestamp("due_date"); + Timestamp returnedAt = rs.getTimestamp("returned_at"); + BigDecimal fine = rs.getBigDecimal("fine_eur"); + + System.out.println("Borrow #" + borrowId + " | Member: " + memberName + " | Book: " + bookTitle + " | Borrowed: " + borrowedAt + " | Due: " + dueDate + " | Returned: " + returnedAt + " | Fine: " + fine); + } + } + System.out.println("Fetch completed."); } } + + diff --git a/task-2/target/classes/org/hyf/Main.class b/task-2/target/classes/org/hyf/Main.class new file mode 100644 index 0000000..da6d7d8 Binary files /dev/null and b/task-2/target/classes/org/hyf/Main.class differ diff --git a/task-2/target/maven-status/maven-compiler-plugin/compile/default-compile/createdFiles.lst b/task-2/target/maven-status/maven-compiler-plugin/compile/default-compile/createdFiles.lst new file mode 100644 index 0000000..b91f49a --- /dev/null +++ b/task-2/target/maven-status/maven-compiler-plugin/compile/default-compile/createdFiles.lst @@ -0,0 +1 @@ +org/hyf/Main.class diff --git a/task-2/target/maven-status/maven-compiler-plugin/compile/default-compile/inputFiles.lst b/task-2/target/maven-status/maven-compiler-plugin/compile/default-compile/inputFiles.lst new file mode 100644 index 0000000..d9e1cb8 --- /dev/null +++ b/task-2/target/maven-status/maven-compiler-plugin/compile/default-compile/inputFiles.lst @@ -0,0 +1 @@ +/Users/yanapechenenko/IdeaProjects/c55-backend-week-5/task-2/src/main/java/org/hyf/Main.java