Логички и физички дизајн - Креирање база податоци (со SQL DDL): DDL-Script-Book-Tracker.sql

File DDL-Script-Book-Tracker.sql, 3.6 KB (added by 222039, 5 days ago)
Line 
1-- Drop tables if they exist
2DROP TABLE IF EXISTS Users CASCADE;
3DROP TABLE IF EXISTS Member CASCADE;
4DROP TABLE IF EXISTS Book CASCADE;
5DROP TABLE IF EXISTS Book_Details CASCADE;
6DROP TABLE IF EXISTS Book_Copies CASCADE;
7DROP TABLE IF EXISTS Book_Author CASCADE;
8DROP TABLE IF EXISTS Author CASCADE;
9DROP TABLE IF EXISTS Loan CASCADE;
10DROP TABLE IF EXISTS Fine CASCADE;
11DROP TABLE IF EXISTS FinePayment CASCADE;
12DROP TABLE IF EXISTS Cart CASCADE;
13
14CREATE TABLE Users (
15 UserID SERIAL PRIMARY KEY,
16 Username TEXT NOT NULL,
17 FirstName TEXT,
18 LastName TEXT,
19 Address TEXT,
20 Phone NUMERIC,
21 Password TEXT NOT NULL,
22 Email TEXT NOT NULL,
23 Role TEXT NOT NULL
24);
25
26CREATE TABLE Member (
27 MemberID SERIAL PRIMARY KEY,
28 Expired_Date DATE NOT NULL,
29 Membership_Status TEXT NOT NULL CHECK (Membership_Status IN ('Active', 'Inactive', 'Suspended')),
30 UserID INT REFERENCES Users(UserID) ON DELETE CASCADE
31);
32
33CREATE TABLE Book (
34 BookID SERIAL PRIMARY KEY,
35 ISBN TEXT NOT NULL,
36 Title TEXT NOT NULL,
37 Genre TEXT NOT NULL,
38 PublishedYear NUMERIC NOT NULL,
39 Description TEXT,
40 CoverImage TEXT,
41 TotalCopies NUMERIC DEFAULT 0
42);
43
44CREATE TABLE Book_Details (
45 DetailsID SERIAL PRIMARY KEY,
46 BookID INT REFERENCES Book(BookID) ON DELETE CASCADE,
47 Format TEXT NOT NULL CHECK (Format IN ('Hardcover', 'Paperback')),
48 Language TEXT NOT NULL,
49 Publisher TEXT NOT NULL,
50 Pages TEXT NOT NULL
51);
52
53CREATE TABLE Book_Copies (
54 CopyID SERIAL PRIMARY KEY,
55 BookID INT REFERENCES Book(BookID) ON DELETE CASCADE,
56 Condition TEXT NOT NULL CHECK (Condition IN ('New', 'Good', 'Damaged'))
57);
58
59CREATE TABLE Author (
60 AuthorID SERIAL PRIMARY KEY,
61 FirstName TEXT NOT NULL,
62 LastName TEXT NOT NULL,
63 Nationality TEXT NOT NULL,
64 DateOfBirth DATE NOT NULL,
65 Author_description text,
66 Author_image text
67);
68
69CREATE TABLE Book_Author (
70 BookID INT REFERENCES Book(BookID) ON DELETE CASCADE,
71 AuthorID INT REFERENCES Author(AuthorID) ON DELETE CASCADE,
72 PRIMARY KEY (BookID, AuthorID)
73);
74
75CREATE TABLE Loan (
76 LoanID SERIAL PRIMARY KEY,
77 LoanDate DATE NOT NULL,
78 ReturnDate DATE,
79 Status TEXT NOT NULL,
80 BookCopyID INT REFERENCES Book_Copies(CopyID) ON DELETE CASCADE,
81 MemberID INT REFERENCES Member(MemberID) ON DELETE CASCADE
82);
83
84CREATE TABLE Fine (
85 FineID SERIAL PRIMARY KEY,
86 FineAmount NUMERIC NOT NULL,
87 FineDate DATE NOT NULL,
88 Status TEXT NOT NULL,
89 LoanID INT REFERENCES Loan(LoanID) ON DELETE CASCADE
90);
91
92CREATE TABLE FinePayment (
93 FinePaymentID SERIAL PRIMARY KEY,
94 PaymentDate DATE NOT NULL,
95 PaymentAmount NUMERIC NOT NULL,
96 FineID INT REFERENCES Fine(FineID) ON DELETE CASCADE
97);
98
99CREATE TABLE Cart (
100 CartID SERIAL PRIMARY KEY,
101 BookID INT REFERENCES Book(BookID) ON DELETE CASCADE,
102 MemberID INT REFERENCES Member(MemberID) ON DELETE CASCADE
103);
104
105
106CREATE OR REPLACE FUNCTION update_total_copies()
107RETURNS TRIGGER AS $$
108BEGIN
109 -- If a new copy is inserted, increment the total copies count
110 IF TG_OP = 'INSERT' THEN
111 UPDATE Book
112 SET TotalCopies = TotalCopies + 1
113 WHERE BookID = NEW.BookID;
114 -- If a copy is deleted, decrement the total copies count
115 ELSIF TG_OP = 'DELETE' THEN
116 UPDATE Book
117 SET TotalCopies = TotalCopies - 1
118 WHERE BookID = OLD.BookID;
119 END IF;
120 RETURN NEW;
121END;
122$$ LANGUAGE plpgsql;
123
124
125CREATE TRIGGER trigger_update_total_copies
126AFTER INSERT OR DELETE ON Book_Copies
127FOR EACH ROW EXECUTE FUNCTION update_total_copies();