| 1 | CREATE TABLE AppUser(
|
|---|
| 2 | UserId SERIAL PRIMARY KEY,
|
|---|
| 3 | FirstName VARCHAR(30) NOT NULL,
|
|---|
| 4 | LastName VARCHAR(30) NOT NULL,
|
|---|
| 5 | Email VARCHAR(100) UNIQUE NOT NULL,
|
|---|
| 6 | Username VARCHAR(30) UNIQUE NOT NULL,
|
|---|
| 7 | Password VARCHAR(50) NOT NULL,
|
|---|
| 8 | City VARCHAR(30) NOT NULL,
|
|---|
| 9 | Neighborhood VARCHAR(30) NOT NULL,
|
|---|
| 10 | Bio VARCHAR(150),
|
|---|
| 11 | Quote VARCHAR(250)
|
|---|
| 12 | );
|
|---|
| 13 |
|
|---|
| 14 | CREATE TABLE Book(
|
|---|
| 15 | BookId SERIAL PRIMARY KEY,
|
|---|
| 16 | Title VARCHAR(150) NOT NULL,
|
|---|
| 17 | Author VARCHAR(100) NOT NULL,
|
|---|
| 18 | Language VARCHAR(30) NOT NULL,
|
|---|
| 19 | ImageURL VARCHAR(300)
|
|---|
| 20 | );
|
|---|
| 21 |
|
|---|
| 22 | CREATE TABLE BookISBN(
|
|---|
| 23 | BookISBNId SERIAL PRIMARY KEY,
|
|---|
| 24 | ISBN VARCHAR(30) NOT NULL
|
|---|
| 25 | );
|
|---|
| 26 |
|
|---|
| 27 | CREATE TABLE Genre(
|
|---|
| 28 | GenreId SERIAL PRIMARY KEY,
|
|---|
| 29 | Genre VARCHAR(50) NOT NULL
|
|---|
| 30 | );
|
|---|
| 31 |
|
|---|
| 32 | CREATE TABLE Review(
|
|---|
| 33 | ReviewId SERIAL PRIMARY KEY,
|
|---|
| 34 | ReceiverId INTEGER NOT NULL REFERENCES AppUser(UserId),
|
|---|
| 35 | GiverId INTEGER NOT NULL REFERENCES AppUser(UserId),
|
|---|
| 36 | Rating INTEGER NOT NULL CHECK (Rating BETWEEN 1 AND 5),
|
|---|
| 37 | ReviewerComment VARCHAR (100),
|
|---|
| 38 | ReviewDate DATE NOT NULL
|
|---|
| 39 | );
|
|---|
| 40 |
|
|---|
| 41 | CREATE TABLE Report(
|
|---|
| 42 | ReportId SERIAL PRIMARY KEY,
|
|---|
| 43 | ReportedUserId INTEGER NOT NULL REFERENCES AppUser(UserId),
|
|---|
| 44 | ReportingUserId INTEGER NOT NULL REFERENCES AppUser(UserId),
|
|---|
| 45 | CHECK (ReportedUserId != ReportingUserId),
|
|---|
| 46 | ReportType VARCHAR(20) NOT NULL CHECK (ReportType IN ('Harassment', 'Bullying', 'Hate speech', 'Spamming')),
|
|---|
| 47 | ReportDate DATE NOT NULL,
|
|---|
| 48 | Details VARCHAR (1000),
|
|---|
| 49 | ReportStatus VARCHAR(20) NOT NULL CHECK (ReportStatus IN ('Pending', 'Accepted', 'Rejected', 'Resolved')),
|
|---|
| 50 | ReportedEntity VARCHAR(20) NOT NULL CHECK (ReportedEntity IN ('Message', 'Rating', 'Profile', 'Library'))
|
|---|
| 51 | );
|
|---|
| 52 |
|
|---|
| 53 |
|
|---|
| 54 | CREATE TABLE Notification(
|
|---|
| 55 | NotificationId SERIAL PRIMARY KEY,
|
|---|
| 56 | Type VARCHAR(20) NOT NULL CHECK (Type IN ('Transaction', 'Message', 'Friend Request', 'Book Request')),
|
|---|
| 57 | NotifTime TIME NOT NULL,
|
|---|
| 58 | NotifDate DATE NOT NULL,
|
|---|
| 59 | Description VARCHAR (100) NOT NULL,
|
|---|
| 60 | NotificationStatus VARCHAR(20) NOT NULL CHECK (NotificationStatus IN ('Read', 'Unread', 'Dismissed'))
|
|---|
| 61 | );
|
|---|
| 62 |
|
|---|
| 63 | CREATE TABLE Transaction(
|
|---|
| 64 | TransactionId SERIAL PRIMARY KEY,
|
|---|
| 65 | BorrowerId INTEGER NOT NULL REFERENCES AppUser(UserId),
|
|---|
| 66 | LenderId INTEGER NOT NULL REFERENCES AppUser(UserId),
|
|---|
| 67 | BorrowDate DATE NOT NULL,
|
|---|
| 68 | ReturnDate DATE NOT NULL CHECK (ReturnDate > BorrowDate),
|
|---|
| 69 | BorrowDuration INT NOT NULL
|
|---|
| 70 | );
|
|---|
| 71 |
|
|---|
| 72 | CREATE TABLE LibraryBook(
|
|---|
| 73 | InventoryId SERIAL PRIMARY KEY,
|
|---|
| 74 | Availability VARCHAR(20) NOT NULL DEFAULT 'Available' CHECK (Availability IN ('Available', 'Not Available', 'Reserved')),
|
|---|
| 75 | Condition VARCHAR(20) NOT NULL CHECK (Condition IN ('New', 'Like New', 'Good', 'Poor'))
|
|---|
| 76 | );
|
|---|
| 77 |
|
|---|
| 78 | CREATE TABLE WishlistBook(
|
|---|
| 79 | WishId SERIAL PRIMARY KEY,
|
|---|
| 80 | Priority VARCHAR(10) CHECK (Priority IN ('High', 'Low', 'Medium'))
|
|---|
| 81 | );
|
|---|
| 82 |
|
|---|
| 83 | CREATE TABLE Message(
|
|---|
| 84 | MessageId SERIAL PRIMARY KEY,
|
|---|
| 85 | MsgSenderId INTEGER NOT NULL REFERENCES AppUser(UserId),
|
|---|
| 86 | MsgReceiverId INTEGER NOT NULL REFERENCES AppUser(UserId),
|
|---|
| 87 | MsgTime TIME NOT NULL,
|
|---|
| 88 | MsgDate DATE NOT NULL,
|
|---|
| 89 | MessageContent VARCHAR(1000) NOT NULL
|
|---|
| 90 | );
|
|---|
| 91 |
|
|---|
| 92 | CREATE TABLE FriendRequest(
|
|---|
| 93 | FriendshipId SERIAL PRIMARY KEY,
|
|---|
| 94 | FriendshipSenderId INTEGER NOT NULL REFERENCES AppUser(UserId),
|
|---|
| 95 | FriendshipReceiverId INTEGER NOT NULL REFERENCES AppUser(UserId),
|
|---|
| 96 | CHECK (FriendshipSenderId != FriendshipReceiverId),
|
|---|
| 97 | DateCreated DATE NOT NULL,
|
|---|
| 98 | FriendshipStatus VARCHAR(20) NOT NULL CHECK (FriendshipStatus IN ('Accepted', 'Pending', 'Declined', 'Blocked'))
|
|---|
| 99 | );
|
|---|
| 100 |
|
|---|
| 101 | CREATE TABLE BookRequest(
|
|---|
| 102 | RequestId SERIAL PRIMARY KEY,
|
|---|
| 103 | RequesterId INTEGER NOT NULL REFERENCES AppUser(UserId)
|
|---|
| 104 | ON DELETE CASCADE,
|
|---|
| 105 | OwnerId INTEGER NOT NULL REFERENCES AppUser(UserId)
|
|---|
| 106 | ON DELETE CASCADE,
|
|---|
| 107 | RequestStatus VARCHAR(20) NOT NULL CHECK (RequestStatus IN ('Approved', 'Pending', 'Declined')),
|
|---|
| 108 | RequestDate DATE NOT NULL
|
|---|
| 109 | );
|
|---|
| 110 |
|
|---|
| 111 | CREATE TABLE Swap(
|
|---|
| 112 | SwapId SERIAL PRIMARY KEY,
|
|---|
| 113 | TransactionId1 INTEGER NOT NULL REFERENCES Transaction(TransactionId)
|
|---|
| 114 | ON DELETE CASCADE,
|
|---|
| 115 | TransactionId2 INTEGER NOT NULL REFERENCES Transaction(TransactionId)
|
|---|
| 116 | ON DELETE CASCADE
|
|---|
| 117 | );
|
|---|
| 118 |
|
|---|
| 119 | CREATE TABLE GenreLink(
|
|---|
| 120 | GenreId INTEGER NOT NULL REFERENCES Genre(GenreId) ON DELETE CASCADE,
|
|---|
| 121 | BookId INTEGER NOT NULL REFERENCES Book(BookId) ON DELETE CASCADE,
|
|---|
| 122 | PRIMARY KEY (GenreId, BookId)
|
|---|
| 123 | );
|
|---|
| 124 |
|
|---|
| 125 | CREATE TABLE ISBNLink(
|
|---|
| 126 | BookISBNId INTEGER NOT NULL REFERENCES BookISBN(BookISBNId) ON DELETE CASCADE,
|
|---|
| 127 | BookId INTEGER NOT NULL REFERENCES Book(BookId) ON DELETE CASCADE,
|
|---|
| 128 | PRIMARY KEY (BookISBNId)
|
|---|
| 129 | );
|
|---|
| 130 |
|
|---|
| 131 | CREATE TABLE WishlistBookLink(
|
|---|
| 132 | WishId INTEGER NOT NULL REFERENCES WishlistBook(WishId) ON DELETE CASCADE,
|
|---|
| 133 | BookId INTEGER NOT NULL REFERENCES Book(BookId) ON DELETE CASCADE,
|
|---|
| 134 | UserId INTEGER NOT NULL REFERENCES AppUser(UserId) ON DELETE CASCADE,
|
|---|
| 135 | PRIMARY KEY (WishId, BookId)
|
|---|
| 136 | );
|
|---|
| 137 |
|
|---|
| 138 | CREATE TABLE LibraryBookLink(
|
|---|
| 139 | InventoryId INTEGER NOT NULL REFERENCES LibraryBook(InventoryId) ON DELETE CASCADE,
|
|---|
| 140 | BookId INTEGER NOT NULL REFERENCES Book(BookId) ON DELETE CASCADE,
|
|---|
| 141 | UserId INTEGER NOT NULL REFERENCES AppUser(UserId) ON DELETE CASCADE,
|
|---|
| 142 | PRIMARY KEY (InventoryId)
|
|---|
| 143 | );
|
|---|
| 144 |
|
|---|
| 145 |
|
|---|
| 146 | CREATE TABLE ReviewLink(
|
|---|
| 147 | ReviewId INTEGER NOT NULL REFERENCES Review(ReviewId) ON DELETE CASCADE,
|
|---|
| 148 | TransactionId INTEGER NOT NULL REFERENCES Transaction(TransactionId) ON DELETE CASCADE,
|
|---|
| 149 | PRIMARY KEY (ReviewId)
|
|---|
| 150 | );
|
|---|
| 151 |
|
|---|
| 152 |
|
|---|
| 153 | CREATE TABLE TransactionLink(
|
|---|
| 154 | TransactionId INTEGER NOT NULL REFERENCES Transaction(TransactionId) ON DELETE CASCADE,
|
|---|
| 155 | RequestId INTEGER NOT NULL REFERENCES BookRequest(RequestId) ON DELETE CASCADE,
|
|---|
| 156 | PRIMARY KEY (TransactionId)
|
|---|
| 157 | );
|
|---|
| 158 |
|
|---|
| 159 | CREATE TABLE BookRequestLink(
|
|---|
| 160 | RequestId INTEGER NOT NULL REFERENCES BookRequest(RequestId) ON DELETE CASCADE,
|
|---|
| 161 | InventoryId INTEGER NOT NULL REFERENCES LibraryBook(InventoryId) ON DELETE CASCADE,
|
|---|
| 162 | PRIMARY KEY (RequestId)
|
|---|
| 163 | );
|
|---|
| 164 |
|
|---|
| 165 |
|
|---|
| 166 | CREATE TABLE MessageNotifLink(
|
|---|
| 167 | NotificationId INTEGER NOT NULL REFERENCES Notification(NotificationId) ON DELETE CASCADE,
|
|---|
| 168 | MessageId INTEGER NOT NULL REFERENCES Message(MessageId) ON DELETE CASCADE,
|
|---|
| 169 | PRIMARY KEY (NotificationId)
|
|---|
| 170 | );
|
|---|
| 171 |
|
|---|
| 172 | CREATE TABLE FriendNotifLink(
|
|---|
| 173 | NotificationId INTEGER NOT NULL REFERENCES Notification(NotificationId) ON DELETE CASCADE,
|
|---|
| 174 | FriendshipId INTEGER NOT NULL REFERENCES FriendRequest(FriendshipId) ON DELETE CASCADE,
|
|---|
| 175 | PRIMARY KEY (NotificationId)
|
|---|
| 176 | );
|
|---|
| 177 |
|
|---|
| 178 |
|
|---|
| 179 | CREATE TABLE BookRequestNotifLink(
|
|---|
| 180 | NotificationId INTEGER NOT NULL REFERENCES Notification(NotificationId) ON DELETE CASCADE,
|
|---|
| 181 | RequestId INTEGER NOT NULL REFERENCES BookRequest(RequestId) ON DELETE CASCADE,
|
|---|
| 182 | PRIMARY KEY (NotificationId)
|
|---|
| 183 | );
|
|---|
| 184 |
|
|---|
| 185 | CREATE TABLE TransactionNotifLink(
|
|---|
| 186 | NotificationId INTEGER NOT NULL REFERENCES Notification(NotificationId) ON DELETE CASCADE,
|
|---|
| 187 | TransactionId INTEGER NOT NULL REFERENCES Transaction(TransactionId) ON DELETE CASCADE,
|
|---|
| 188 | PRIMARY KEY (NotificationId)
|
|---|
| 189 | ); |
|---|