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

File ddlscript4.sql, 4.9 KB (added by 201205, 4 days 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 BookId INTEGER NOT NULL REFERENCES Book(BookId)
24 ON DELETE CASCADE,
25 ISBN VARCHAR(30) NOT NULL
26);
27
28CREATE TABLE Genre(
29 BookId INTEGER NOT NULL REFERENCES Book(BookId)
30 ON DELETE CASCADE,
31 Genre VARCHAR(50) NOT NULL
32);
33
34CREATE TABLE Library(
35 InventoryId SERIAL PRIMARY KEY,
36 UserId INTEGER NOT NULL REFERENCES AppUser(UserId)
37 ON DELETE CASCADE,
38 BookId INTEGER NOT NULL REFERENCES Book(BookId)
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
44
45CREATE TABLE Wishlist(
46 WishId SERIAL PRIMARY KEY,
47 UserId INTEGER NOT NULL REFERENCES AppUser(UserId)
48 ON DELETE CASCADE,
49 BookId INTEGER NOT NULL REFERENCES Book(BookId)
50 ON DELETE CASCADE,
51 Priority VARCHAR(10) CHECK (Priority IN ('High', 'Low', 'Medium'))
52);
53
54
55
56CREATE TABLE BookRequest(
57 RequestId SERIAL PRIMARY KEY,
58 RequesterId INTEGER NOT NULL REFERENCES AppUser(UserId)
59 ON DELETE CASCADE,
60 OwnerId INTEGER NOT NULL REFERENCES AppUser(UserId)
61 ON DELETE CASCADE,
62 BookId INTEGER NOT NULL REFERENCES Book(BookId)
63 ON DELETE CASCADE,
64 InventoryId INTEGER NOT NULL REFERENCES Library(InventoryId)
65 ON DELETE CASCADE,
66 RequestStatus VARCHAR(20) NOT NULL CHECK (RequestStatus IN ('Approved', 'Pending', 'Declined')),
67 RequestDate DATE NOT NULL
68);
69
70CREATE TABLE Transaction(
71 TransactionId SERIAL PRIMARY KEY,
72 RequestId INTEGER NOT NULL REFERENCES BookRequest(RequestId),
73 BorrowerId INTEGER NOT NULL REFERENCES AppUser(UserId),
74 LenderId INTEGER NOT NULL REFERENCES AppUser(UserId),
75 InventoryId INTEGER NOT NULL REFERENCES Library(InventoryId),
76 BorrowDate DATE NOT NULL,
77 ReturnDate DATE NOT NULL CHECK (ReturnDate > BorrowDate),
78 BorrowDuration INT NOT NULL,
79 SwapTransactionId INTEGER REFERENCES Transaction(TransactionId)
80 ON DELETE SET NULL
81);
82
83
84CREATE TABLE TransactionBook(
85 TransactionId INTEGER NOT NULL REFERENCES Transaction(TransactionId)
86 ON DELETE CASCADE,
87 BookId INTEGER NOT NULL REFERENCES Book(BookId)
88 ON DELETE CASCADE
89);
90
91CREATE TABLE Review(
92 ReviewId SERIAL PRIMARY KEY,
93 TransactionId INTEGER NOT NULL REFERENCES Transaction(TransactionId),
94 ReceiverId INTEGER NOT NULL REFERENCES AppUser(UserId),
95 GiverId INTEGER NOT NULL REFERENCES AppUser(UserId),
96 Rating INTEGER NOT NULL CHECK (Rating BETWEEN 1 AND 5),
97 ReviewerComment VARCHAR (100),
98 Date DATE NOT NULL
99);
100
101CREATE TABLE Message(
102 MessageId SERIAL PRIMARY KEY,
103 SenderId INTEGER NOT NULL REFERENCES AppUser(UserId),
104 ReceiverId INTEGER NOT NULL REFERENCES AppUser(UserId),
105 Time TIME NOT NULL,
106 Date DATE NOT NULL,
107 MessageContent VARCHAR(1000) NOT NULL
108);
109
110CREATE TABLE FriendRequest(
111 FriendshipId SERIAL PRIMARY KEY,
112 SenderId INTEGER NOT NULL REFERENCES AppUser(UserId),
113 ReceiverId INTEGER NOT NULL REFERENCES AppUser(UserId),
114 CHECK (SenderId != ReceiverId),
115 DateCreated DATE NOT NULL,
116 Status VARCHAR(20) NOT NULL CHECK (Status IN ('Accepted', 'Pending', 'Declined', 'Blocked'))
117);
118
119CREATE TABLE Report(
120 ReportId SERIAL PRIMARY KEY,
121 ReportedUserId INTEGER NOT NULL REFERENCES AppUser(UserId),
122 ReportingUserId INTEGER NOT NULL REFERENCES AppUser(UserId),
123 CHECK (ReportedUserId != ReportingUserId),
124 ReportType VARCHAR(20) NOT NULL CHECK (ReportType IN ('Harassment', 'Bullying', 'Hate speech', 'Spamming')),
125 ReportDate DATE NOT NULL,
126 Details VARCHAR (1000),
127 ReportStatus VARCHAR(20) NOT NULL CHECK (ReportStatus IN ('Pending', 'Accepted', 'Rejected', 'Resolved')),
128 ReportedEntity VARCHAR(20) NOT NULL CHECK (ReportedEntity IN ('Message', 'Rating', 'Profile', 'Library'))
129);
130
131
132CREATE TABLE Notification(
133 NotificationId SERIAL PRIMARY KEY,
134 TransactionId INTEGER REFERENCES Transaction(TransactionId)
135 ON DELETE CASCADE,
136 MessageId INTEGER REFERENCES Message(MessageId)
137 ON DELETE CASCADE,
138 FriendRequestId INTEGER REFERENCES FriendRequest(FriendshipId)
139 ON DELETE CASCADE,
140 BookRequestId INTEGER REFERENCES BookRequest(RequestId)
141 ON DELETE CASCADE,
142 Type VARCHAR(20) NOT NULL CHECK (Type IN ('System Update', 'Transaction', 'Reminder', 'Message', 'Friend Request', 'Book Request')),
143 Time TIME NOT NULL,
144 Date DATE NOT NULL,
145 Description VARCHAR (100) NOT NULL,
146 Status VARCHAR(20) NOT NULL CHECK (Status IN ('Read', 'Unread', 'Dismissed'))
147);
148
149CREATE TABLE UserNotification(
150 UserId INTEGER NOT NULL REFERENCES AppUser(UserId),
151 NotificationId INTEGER NOT NULL REFERENCES Notification(NotificationId)
152);