-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlibrary_db.sql
75 lines (65 loc) · 1.91 KB
/
library_db.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
CREATE TABLE authors (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(255) NOT NULL,
biography TEXT
);
CREATE TABLE books (
id INT AUTO_INCREMENT PRIMARY KEY,
title VARCHAR(255) NOT NULL,
author_id INT,
isbn VARCHAR(13) NOT NULL,
publication_date DATE,
availability BOOLEAN DEFAULT 1,
FOREIGN KEY (author_id) REFERENCES authors(id)
);
CREATE TABLE users (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(255) NOT NULL,
library_id VARCHAR(10) NOT NULL UNIQUE
);
CREATE TABLE borrowed_books (
id INT AUTO_INCREMENT PRIMARY KEY,
user_id INT,
book_id INT,
borrow_date DATE NOT NULL,
return_date DATE,
FOREIGN KEY (user_id) REFERENCES users(id),
FOREIGN KEY (book_id) REFERENCES books(id)
);
CREATE TABLE reservations (
id INT AUTO_INCREMENT PRIMARY KEY,
user_id INT NOT NULL,
book_id INT NOT NULL,
reservation_date DATE NOT NULL,
FOREIGN KEY (user_id) REFERENCES users(id),
FOREIGN KEY (book_id) REFERENCES books(id)
);
CREATE TABLE genres (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(255) NOT NULL
);
CREATE TABLE book_genres (
book_id INT NOT NULL,
genre_id INT NOT NULL,
PRIMARY KEY (book_id, genre_id), # Composite primary key
FOREIGN KEY (book_id) REFERENCES books(id),
FOREIGN KEY (genre_id) REFERENCES genres(id)
);
CREATE TABLE fines (
id INT AUTO_INCREMENT PRIMARY KEY,
user_id INT NOT NULL,
borrowed_book_id INT NOT NULL,
fine_amount DECIMAL(10, 2) NOT NULL,
payment_status ENUM('pending', 'paid') NOT NULL DEFAULT 'pending',
payment_date DATE,
FOREIGN KEY (user_id) REFERENCES users(id),
FOREIGN KEY (borrowed_book_id) REFERENCES borrowed_books(id)
);
CREATE TABLE book_copies (
id INT AUTO_INCREMENT PRIMARY KEY,
book_id INT NOT NULL,
copy_number VARCHAR(50), # You can adjust the size as needed
availability BOOLEAN DEFAULT 1,
FOREIGN KEY (book_id) REFERENCES books(id)
);
SELECT * FROM Books