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

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