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

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