| 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 |
|
|---|
| 23 | CREATE TABLE BookISBN(
|
|---|
| 24 | BookISBNId SERIAL PRIMARY KEY,
|
|---|
| 25 | BookId INTEGER NOT NULL REFERENCES Book(BookId)
|
|---|
| 26 | ON DELETE CASCADE,
|
|---|
| 27 | ISBN VARCHAR(30) NOT NULL
|
|---|
| 28 | );
|
|---|
| 29 |
|
|---|
| 30 |
|
|---|
| 31 | CREATE TABLE Genre(
|
|---|
| 32 | GenreId SERIAL PRIMARY KEY,
|
|---|
| 33 | BookId INTEGER NOT NULL REFERENCES Book(BookId)
|
|---|
| 34 | ON DELETE CASCADE,
|
|---|
| 35 | Genre VARCHAR(50) NOT NULL
|
|---|
| 36 | );
|
|---|
| 37 |
|
|---|
| 38 | CREATE TABLE Library(
|
|---|
| 39 | InventoryId SERIAL PRIMARY KEY,
|
|---|
| 40 | UserId INTEGER NOT NULL REFERENCES AppUser(UserId)
|
|---|
| 41 | ON DELETE CASCADE,
|
|---|
| 42 | Availability VARCHAR(20) NOT NULL DEFAULT 'Available' CHECK (Availability IN ('Available', 'Not Available', 'Reserved')),
|
|---|
| 43 | Condition VARCHAR(20) NOT NULL CHECK (Condition IN ('New', 'Like New', 'Good', 'Poor'))
|
|---|
| 44 | );
|
|---|
| 45 |
|
|---|
| 46 |
|
|---|
| 47 | CREATE TABLE ContainsLibraryBook(
|
|---|
| 48 | InventoryId INTEGER NOT NULL REFERENCES Library(InventoryId) ON DELETE CASCADE,
|
|---|
| 49 | BookId INTEGER NOT NULL REFERENCES Book(BookId) ON DELETE CASCADE,
|
|---|
| 50 | PRIMARY KEY (InventoryId, BookId)
|
|---|
| 51 | );
|
|---|
| 52 |
|
|---|
| 53 | CREATE TABLE Wishlist(
|
|---|
| 54 | WishId SERIAL PRIMARY KEY,
|
|---|
| 55 | UserId INTEGER NOT NULL REFERENCES AppUser(UserId)
|
|---|
| 56 | ON DELETE CASCADE,
|
|---|
| 57 | Priority VARCHAR(10) CHECK (Priority IN ('High', 'Low', 'Medium'))
|
|---|
| 58 | );
|
|---|
| 59 |
|
|---|
| 60 |
|
|---|
| 61 | CREATE TABLE ContainsWishlistBook(
|
|---|
| 62 | WishId INTEGER NOT NULL REFERENCES Wishlist(WishId) ON DELETE CASCADE,
|
|---|
| 63 | BookId INTEGER NOT NULL REFERENCES Book(BookId) ON DELETE CASCADE,
|
|---|
| 64 | PRIMARY KEY (WishId, BookId)
|
|---|
| 65 | );
|
|---|
| 66 |
|
|---|
| 67 |
|
|---|
| 68 | CREATE TABLE BookRequest(
|
|---|
| 69 | RequestId SERIAL PRIMARY KEY,
|
|---|
| 70 | RequesterId INTEGER NOT NULL REFERENCES AppUser(UserId)
|
|---|
| 71 | ON DELETE CASCADE,
|
|---|
| 72 | OwnerId INTEGER NOT NULL REFERENCES AppUser(UserId)
|
|---|
| 73 | ON DELETE CASCADE,
|
|---|
| 74 | BookId INTEGER NOT NULL REFERENCES Book(BookId)
|
|---|
| 75 | ON DELETE CASCADE,
|
|---|
| 76 | InventoryId INTEGER NOT NULL REFERENCES Library(InventoryId)
|
|---|
| 77 | ON DELETE CASCADE,
|
|---|
| 78 | RequestStatus VARCHAR(20) NOT NULL CHECK (RequestStatus IN ('Approved', 'Pending', 'Declined')),
|
|---|
| 79 | RequestDate DATE NOT NULL
|
|---|
| 80 | );
|
|---|
| 81 |
|
|---|
| 82 |
|
|---|
| 83 | CREATE TABLE Transaction(
|
|---|
| 84 | TransactionId SERIAL PRIMARY KEY,
|
|---|
| 85 | RequestId INTEGER NOT NULL REFERENCES BookRequest(RequestId),
|
|---|
| 86 | BorrowerId INTEGER NOT NULL REFERENCES AppUser(UserId),
|
|---|
| 87 | LenderId INTEGER NOT NULL REFERENCES AppUser(UserId),
|
|---|
| 88 | InventoryId INTEGER NOT NULL REFERENCES Library(InventoryId),
|
|---|
| 89 | BorrowDate DATE NOT NULL,
|
|---|
| 90 | ReturnDate DATE NOT NULL CHECK (ReturnDate > BorrowDate),
|
|---|
| 91 | BorrowDuration INT NOT NULL
|
|---|
| 92 | );
|
|---|
| 93 |
|
|---|
| 94 | CREATE TABLE Swap(
|
|---|
| 95 | SwapId SERIAL PRIMARY KEY,
|
|---|
| 96 | TransactionId1 INTEGER NOT NULL REFERENCES Transaction(TransactionId)
|
|---|
| 97 | ON DELETE CASCADE,
|
|---|
| 98 | TransactionId2 INTEGER NOT NULL REFERENCES Transaction(TransactionId)
|
|---|
| 99 | ON DELETE CASCADE
|
|---|
| 100 | );
|
|---|
| 101 |
|
|---|
| 102 | CREATE TABLE ExchangingBook(
|
|---|
| 103 | TransactionId INTEGER NOT NULL REFERENCES Transaction(TransactionId) ON DELETE CASCADE,
|
|---|
| 104 | BookId INTEGER NOT NULL REFERENCES Book(BookId) ON DELETE CASCADE,
|
|---|
| 105 | PRIMARY KEY (TransactionId, BookId)
|
|---|
| 106 | );
|
|---|
| 107 |
|
|---|
| 108 |
|
|---|
| 109 | CREATE TABLE Review(
|
|---|
| 110 | ReviewId SERIAL PRIMARY KEY,
|
|---|
| 111 | TransactionId INTEGER NOT NULL REFERENCES Transaction(TransactionId),
|
|---|
| 112 | ReceiverId INTEGER NOT NULL REFERENCES AppUser(UserId),
|
|---|
| 113 | GiverId INTEGER NOT NULL REFERENCES AppUser(UserId),
|
|---|
| 114 | Rating INTEGER NOT NULL CHECK (Rating BETWEEN 1 AND 5),
|
|---|
| 115 | ReviewerComment VARCHAR (100),
|
|---|
| 116 | ReviewDate DATE NOT NULL
|
|---|
| 117 | );
|
|---|
| 118 |
|
|---|
| 119 | CREATE TABLE Message(
|
|---|
| 120 | MessageId SERIAL PRIMARY KEY,
|
|---|
| 121 | SenderId INTEGER NOT NULL REFERENCES AppUser(UserId),
|
|---|
| 122 | ReceiverId INTEGER NOT NULL REFERENCES AppUser(UserId),
|
|---|
| 123 | MsgTime TIME NOT NULL,
|
|---|
| 124 | MsgDate DATE NOT NULL,
|
|---|
| 125 | MessageContent VARCHAR(1000) NOT NULL
|
|---|
| 126 | );
|
|---|
| 127 |
|
|---|
| 128 | CREATE TABLE FriendRequest(
|
|---|
| 129 | FriendshipId SERIAL PRIMARY KEY,
|
|---|
| 130 | SenderId INTEGER NOT NULL REFERENCES AppUser(UserId),
|
|---|
| 131 | ReceiverId INTEGER NOT NULL REFERENCES AppUser(UserId),
|
|---|
| 132 | CHECK (SenderId != ReceiverId),
|
|---|
| 133 | DateCreated DATE NOT NULL,
|
|---|
| 134 | FriendshipStatus VARCHAR(20) NOT NULL CHECK (FriendshipStatus IN ('Accepted', 'Pending', 'Declined', 'Blocked'))
|
|---|
| 135 | );
|
|---|
| 136 |
|
|---|
| 137 | CREATE TABLE Report(
|
|---|
| 138 | ReportId SERIAL PRIMARY KEY,
|
|---|
| 139 | ReportedUserId INTEGER NOT NULL REFERENCES AppUser(UserId),
|
|---|
| 140 | ReportingUserId INTEGER NOT NULL REFERENCES AppUser(UserId),
|
|---|
| 141 | CHECK (ReportedUserId != ReportingUserId),
|
|---|
| 142 | ReportType VARCHAR(20) NOT NULL CHECK (ReportType IN ('Harassment', 'Bullying', 'Hate speech', 'Spamming')),
|
|---|
| 143 | ReportDate DATE NOT NULL,
|
|---|
| 144 | Details VARCHAR (1000),
|
|---|
| 145 | ReportStatus VARCHAR(20) NOT NULL CHECK (ReportStatus IN ('Pending', 'Accepted', 'Rejected', 'Resolved')),
|
|---|
| 146 | ReportedEntity VARCHAR(20) NOT NULL CHECK (ReportedEntity IN ('Message', 'Rating', 'Profile', 'Library'))
|
|---|
| 147 | );
|
|---|
| 148 |
|
|---|
| 149 |
|
|---|
| 150 | CREATE TABLE Notification(
|
|---|
| 151 | NotificationId SERIAL PRIMARY KEY,
|
|---|
| 152 | TransactionId INTEGER REFERENCES Transaction(TransactionId)
|
|---|
| 153 | ON DELETE CASCADE,
|
|---|
| 154 | MessageId INTEGER REFERENCES Message(MessageId)
|
|---|
| 155 | ON DELETE CASCADE,
|
|---|
| 156 | FriendRequestId INTEGER REFERENCES FriendRequest(FriendshipId)
|
|---|
| 157 | ON DELETE CASCADE,
|
|---|
| 158 | BookRequestId INTEGER REFERENCES BookRequest(RequestId)
|
|---|
| 159 | ON DELETE CASCADE,
|
|---|
| 160 | Type VARCHAR(20) NOT NULL CHECK (Type IN ('System Update', 'Transaction', 'Reminder', 'Message', 'Friend Request', 'Book Request')),
|
|---|
| 161 | NotifTime TIME NOT NULL,
|
|---|
| 162 | NotifDate DATE NOT NULL,
|
|---|
| 163 | Description VARCHAR (100) NOT NULL,
|
|---|
| 164 | NotificationStatus VARCHAR(20) NOT NULL CHECK (NotificationStatus IN ('Read', 'Unread', 'Dismissed'))
|
|---|
| 165 | );
|
|---|
| 166 |
|
|---|
| 167 |
|
|---|
| 168 | CREATE TABLE ReceivesNotification(
|
|---|
| 169 | UserId INTEGER NOT NULL REFERENCES AppUser(UserId) ON DELETE CASCADE,
|
|---|
| 170 | NotificationId INTEGER NOT NULL REFERENCES Notification(NotificationId) ON DELETE CASCADE,
|
|---|
| 171 | PRIMARY KEY (UserId, NotificationId)
|
|---|
| 172 | );
|
|---|
| 173 |
|
|---|
| 174 |
|
|---|
| 175 |
|
|---|