1 | -- Drop tables if they exist
|
---|
2 | DROP TABLE IF EXISTS Users CASCADE;
|
---|
3 | DROP TABLE IF EXISTS Member CASCADE;
|
---|
4 | DROP TABLE IF EXISTS Book CASCADE;
|
---|
5 | DROP TABLE IF EXISTS Book_Details CASCADE;
|
---|
6 | DROP TABLE IF EXISTS Book_Copies CASCADE;
|
---|
7 | DROP TABLE IF EXISTS Book_Author CASCADE;
|
---|
8 | DROP TABLE IF EXISTS Author CASCADE;
|
---|
9 | DROP TABLE IF EXISTS Loan CASCADE;
|
---|
10 | DROP TABLE IF EXISTS Fine CASCADE;
|
---|
11 | DROP TABLE IF EXISTS FinePayment CASCADE;
|
---|
12 | DROP TABLE IF EXISTS Cart CASCADE;
|
---|
13 |
|
---|
14 | CREATE 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 |
|
---|
26 | CREATE 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 |
|
---|
33 | CREATE 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 |
|
---|
44 | CREATE 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 |
|
---|
53 | CREATE 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 |
|
---|
59 | CREATE 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 |
|
---|
69 | CREATE 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 |
|
---|
75 | CREATE 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 |
|
---|
84 | CREATE 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 |
|
---|
92 | CREATE 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 |
|
---|
99 | CREATE 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 |
|
---|
106 | CREATE OR REPLACE FUNCTION update_total_copies()
|
---|
107 | RETURNS TRIGGER AS $$
|
---|
108 | BEGIN
|
---|
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;
|
---|
121 | END;
|
---|
122 | $$ LANGUAGE plpgsql;
|
---|
123 |
|
---|
124 |
|
---|
125 | CREATE TRIGGER trigger_update_total_copies
|
---|
126 | AFTER INSERT OR DELETE ON Book_Copies
|
---|
127 | FOR EACH ROW EXECUTE FUNCTION update_total_copies();
|
---|