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

File ddlscript10.sql, 5.6 KB (added by 201205, 28 hours ago)
Line 
1CREATE 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
14CREATE 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
22CREATE TABLE BookISBN(
23 BookISBNId SERIAL PRIMARY KEY,
24 BookId INTEGER NOT NULL REFERENCES Book(BookId)
25 ON DELETE CASCADE,
26 ISBN VARCHAR(30) NOT NULL
27);
28
29CREATE TABLE Genre(
30 GenreId SERIAL PRIMARY KEY,
31 BookId INTEGER NOT NULL REFERENCES Book(BookId)
32 ON DELETE CASCADE,
33 Genre VARCHAR(50) NOT NULL
34);
35
36CREATE TABLE Library(
37 InventoryId SERIAL PRIMARY KEY,
38 UserId INTEGER NOT NULL REFERENCES AppUser(UserId)
39 ON DELETE CASCADE,
40 Availability VARCHAR(20) NOT NULL DEFAULT 'Available' CHECK (Availability IN ('Available', 'Not Available', 'Reserved')),
41 Condition VARCHAR(20) NOT NULL CHECK (Condition IN ('New', 'Like New', 'Good', 'Poor'))
42);
43
44CREATE TABLE ContainsLibraryBook(
45 LibraryBookId SERIAL PRIMARY KEY,
46 InventoryId INTEGER NOT NULL REFERENCES Library(InventoryId)
47 ON DELETE CASCADE,
48 BookId INTEGER NOT NULL REFERENCES Book(BookId)
49 ON DELETE CASCADE
50);
51
52CREATE TABLE Wishlist(
53 WishId SERIAL PRIMARY KEY,
54 UserId INTEGER NOT NULL REFERENCES AppUser(UserId)
55 ON DELETE CASCADE,
56 Priority VARCHAR(10) CHECK (Priority IN ('High', 'Low', 'Medium'))
57);
58
59CREATE TABLE ContainsWishlistBook(
60 WishlistBookId SERIAL PRIMARY KEY,
61 WishId INTEGER NOT NULL REFERENCES Wishlist(WishId)
62 ON DELETE CASCADE,
63 BookId INTEGER NOT NULL REFERENCES Book(BookId)
64 ON DELETE CASCADE
65);
66
67CREATE TABLE BookRequest(
68 RequestId SERIAL PRIMARY KEY,
69 RequesterId INTEGER NOT NULL REFERENCES AppUser(UserId)
70 ON DELETE CASCADE,
71 OwnerId INTEGER NOT NULL REFERENCES AppUser(UserId)
72 ON DELETE CASCADE,
73 BookId INTEGER NOT NULL REFERENCES Book(BookId)
74 ON DELETE CASCADE,
75 InventoryId INTEGER NOT NULL REFERENCES Library(InventoryId)
76 ON DELETE CASCADE,
77 RequestStatus VARCHAR(20) NOT NULL CHECK (RequestStatus IN ('Approved', 'Pending', 'Declined')),
78 RequestDate DATE NOT NULL
79);
80
81CREATE TABLE Transaction(
82 TransactionId SERIAL PRIMARY KEY,
83 RequestId INTEGER NOT NULL REFERENCES BookRequest(RequestId),
84 BorrowerId INTEGER NOT NULL REFERENCES AppUser(UserId),
85 LenderId INTEGER NOT NULL REFERENCES AppUser(UserId),
86 InventoryId INTEGER NOT NULL REFERENCES Library(InventoryId),
87 BorrowDate DATE NOT NULL,
88 ReturnDate DATE NOT NULL CHECK (ReturnDate > BorrowDate),
89 BorrowDuration INT NOT NULL
90);
91
92
93CREATE TABLE Swap(
94 SwapId SERIAL PRIMARY KEY,
95 TransactionId1 INTEGER NOT NULL REFERENCES Transaction(TransactionId)
96 ON DELETE CASCADE,
97 TransactionId2 INTEGER NOT NULL REFERENCES Transaction(TransactionId)
98 ON DELETE CASCADE
99);
100
101CREATE TABLE ExchangingBook(
102 TransactionBookId SERIAL PRIMARY KEY,
103 TransactionId INTEGER NOT NULL REFERENCES Transaction(TransactionId)
104 ON DELETE CASCADE,
105 BookId INTEGER NOT NULL REFERENCES Book(BookId)
106 ON DELETE CASCADE
107);
108
109CREATE 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
119CREATE 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
128CREATE 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
137CREATE 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
150CREATE 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
167CREATE TABLE ReceivesNotification(
168 UserNotificationId SERIAL PRIMARY KEY,
169 UserId INTEGER NOT NULL REFERENCES AppUser(UserId),
170 NotificationId INTEGER NOT NULL REFERENCES Notification(NotificationId)
171);