-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathcreate.sql
81 lines (73 loc) · 2.29 KB
/
create.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
.open BOOKSTORE.db
drop table USER;
drop table BOOK;
drop table LISTING;
drop table USER_REVIEW;
drop table BOOK_REVIEW;
drop table TRANSACT;
CREATE TABLE USER
(
Username VARCHAR(15) NOT NULL,
Email VARCHAR(20) NOT NULL,
Password VARCHAR(15) NOT NULL,
Fname VARCHAR(15),
Minit CHAR,
Lname VARCHAR(15),
Bdate DATE,
ZipCode VARCHAR(5),
State CHAR(2),
City VARCHAR(15),
StAddress VARCHAR(30),
PRIMARY KEY(Username)
);
CREATE TABLE BOOK
(
ISBN CHAR(14) NOT NULL,
Edition VARCHAR(15),
Title VARCHAR(50) NOT NULL,
AuthorName VARCHAR(30) NOT NULL,
PRIMARY KEY(ISBN)
);
CREATE TABLE LISTING
(
Seller VARCHAR(15) NOT NULL,
Book CHAR(14) NOT NULL,
Price DECIMAL(4,2) NOT NULL,
Quantity INT,
CHECK (Quantity >= 0),
PRIMARY KEY(Seller, Book),
FOREIGN KEY(Book) references BOOK(ISBN) on update cascade,
FOREIGN KEY(Seller) references USER(Username) on update cascade on delete restrict
);
CREATE TABLE USER_REVIEW
(
Reviewer VARCHAR(15) NOT NULL,
Reviewee VARCHAR(15) NOT NULL,
Review TEXT,
Rating INT NOT NULL,
CHECK (Rating >= 1 OR Rating <= 5),
PRIMARY KEY(Reviewer, Reviewee),
FOREIGN KEY(Reviewer) references USER(Username) on update cascade,
FOREIGN KEY(Reviewee) references USER(Username) on update cascade
);
CREATE TABLE BOOK_REVIEW
(
Reviewer VARCHAR(15) NOT NULL,
Book CHAR(14) NOT NULL,
Review TEXT,
Rating INT NOT NULL,
CHECK (Rating >= 1 OR Rating <= 5),
PRIMARY KEY(Reviewer, Book),
FOREIGN KEY(Reviewer) references USER(Username) on update cascade,
FOREIGN KEY(Book) references BOOK(ISBN) on update cascade
);
CREATE TABLE TRANSACT
(
BuyerUN VARCHAR(15) NOT NULL,
SellerUN VARCHAR(15) NOT NULL,
BookID CHAR(14) NOT NULL,
DateTime TEXT NOT NULL,
PRIMARY KEY(BuyerUN, SellerUN, BookID, DateTime),
FOREIGN KEY(BuyerUN) references USER(Username) on update cascade on delete restrict,
FOREIGN KEY(SellerUN,BookID) references LISTING(Seller, Book) on update cascade on delete restrict
);