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

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