This project provides a comprehensive SQL script to set up and manage a simple library database. The script covers schema definition, data population, and examples of using views, functions, and stored procedures.
The goal of this project is to demonstrate fundamental SQL database operations such as table creation, relationship definition, constraint implementation, and the use of advanced database objects (views, functions, procedures). The biblioteka database allows for tracking authors, clients, books, employees, and rental transactions.
The biblioteka database consists of the following tables:
autor: Information about book authors (ID, First Name, Last Name, Date of Birth).klient: Library client details (ID, First Name, Last Name, Address, PESEL).ksiazki: Book details (ID, Genre, Title, Author ID, Publication Year, Description).pracownicy: Library employee details (ID, First Name, Last Name, PESEL, Position).wypozyczenie: Book rental records (Rental ID, Employee ID, Client ID, Book ID, Rental Date).
ksiazkireferencesautor(id_autor)wypozyczeniereferencesklient(id_klient)wypozyczeniereferencesksiazki(id_ksiazki)wypozyczeniereferencespracownicy(id_pracownika)
- PESEL number validation:
CHECKconstraints are implemented forklientandpracownicytables to verify the correctness of the PESEL number (checksum) and exclude common invalid values. - Forbidden author last names: A
CHECKconstraint in theautortable prevents the insertion of certain last names. - Rental Date: The rental date must be less than or equal to the current date.
pracownicy.stanowisko: Defaults to 'Młodszy bibliotekarz' (Junior Librarian).ksiazki.Opis: Defaults to 'Brak opisu' (No description).klient.Adres: Defaults to 'Brak podanego adresu' (No address provided).
For improved query performance, non-clustered indexes have been added to the following columns:
ksiazki.Tytulklient.PESELpracownicy.Nazwisko
The project includes the following database objects:
- Views:
VIEW_Wypozyczone_Ksiazki: Displays details of rented books, joining data from multiple tables.VIEW_Ilosc_ksiazek_By_Autor: Counts the number of books by each author.VIEW_Ilosc_Wypozyczonych_ksiazek: Shows the number of times each book has been rented.VIEW_Ksiazki_Pomiedzy_Latami_1800_1900: A view utilizing a table-valued function to filter books within a specific year range.
- Functions:
- Scalar Function (
FUNC_Get_Data_By_IdKlienta_IdKsiazki): Returns the rental date for a specific client and book. - Table-Valued Function (
FUNC_Get_Ksiazki_Pomiedzy_Latami): Returns a table of books published within a specified range of years.
- Scalar Function (
- Stored Procedure:
InsertWypozyczenie: A procedure for inserting new records into thewypozyczenietable. It includes validation to check if the provided employee, client, and book IDs exist in the database before insertion. The rental date defaults to the current date if not provided.
- Clone the repository or copy the contents of the
.sqlfile. - Open your SQL Server database management environment (e.g., SQL Server Management Studio - SSMS).
- Connect to your SQL Server instance.
- Open a new query window (
New Query). - Paste the entire content of the
.sqlfile into the query window. - Ensure you are connected to the
masterdatabase (USE [master]). - Execute the script (click
Executeor pressF5). This will:- Drop (if it exists) and recreate the
bibliotekadatabase. - Create all tables, foreign keys, constraints, and indexes.
- Insert sample data.
- Create the views, functions, and stored procedures.
- Execute the example function and procedure calls.
- Drop (if it exists) and recreate the