-
Notifications
You must be signed in to change notification settings - Fork 0
/
tables.sql
89 lines (76 loc) · 2.8 KB
/
tables.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
81
82
83
84
85
86
CREATE DATABASE IF NOT EXISTS library_db;
USE library_db;
CREATE TABLE IF NOT EXISTS authors(
author_id INT NOT NULL AUTO_INCREMENT,
name VARCHAR(100) NOT NULL,
description VARCHAR(512) DEFAULT NULL,
PRIMARY KEY (author_id),
UNIQUE KEY (name)
);
CREATE TABLE IF NOT EXISTS books (
isbn INT NOT NULL,
title VARCHAR(100) NOT NULL,
description VARCHAR(512) DEFAULT NULL,
author_id INT NOT NULL DEFAULT 0,
PRIMARY KEY (isbn),
FOREIGN KEY (author_id) REFERENCES authors(author_id) ON DELETE SET DEFAULT ON UPDATE CASCADE
);
CREATE TABLE IF NOT EXISTS reviews (
review_id INT NOT NULL AUTO_INCREMENT,
isbn INT NOT NULL,
rating INT NOT NULL,
review VARCHAR(512) DEFAULT NULL,
PRIMARY KEY (review_id),
Foreign Key (isbn) REFERENCES books(isbn) ON DELETE CASCADE ON UPDATE CASCADE
);
CREATE TABLE IF NOT EXISTS member_cards (
card_number INT NOT NULL AUTO_INCREMENT,
issued_date DATE NOT NULL,
expiry_date DATE DEFAULT NULL,
PRIMARY KEY (card_number)
);
CREATE TABLE IF NOT EXISTS members (
member_id INT NOT NULL AUTO_INCREMENT,
card_number INT NOT NULL,
name VARCHAR(100) NOT NULL,
date_of_birth DATE NOT NULL,
postal_code INT NOT NULL,
street VARCHAR(100) NOT NULL,
city VARCHAR(100) NOT NULL,
PRIMARY KEY(member_id),
Foreign Key (card_number) REFERENCES member_cards(card_number) ON DELETE CASCADE ON UPDATE CASCADE
);
CREATE TABLE IF NOT EXISTS telephone (
member_id INT NOT NULL,
tel_no VARCHAR(15) NOT NULL,
PRIMARY KEY (tel_no, member_id),
Foreign Key (member_id) REFERENCES members(member_id) ON DELETE CASCADE ON UPDATE CASCADE
);
CREATE TABLE IF NOT EXISTS employees (
employee_id INT NOT NULL AUTO_INCREMENT,
supervisor_id INT DEFAULT NULL,
name VARCHAR(100) NOT NULL,
nic VARCHAR(15) NOT NULL,
PRIMARY KEY (employee_id),
UNIQUE KEY (nic)
-- Foreign Key (supervisor_id) REFERENCES employees(supervisor_id) ON DELETE CASCADE ON UPDATE CASCADE
);
CREATE TABLE IF NOT EXISTS working_schedule (
schedule_id INT NOT NULL AUTO_INCREMENT,
employee_id INT NOT NULL,
schedule VARCHAR(512) DEFAULT NULL,
PRIMARY KEY (schedule_id),
Foreign Key (employee_id) REFERENCES employees(employee_id) ON DELETE CASCADE ON UPDATE CASCADE
);
CREATE TABLE IF NOT EXISTS borrowing (
borrowing_id INT NOT NULL AUTO_INCREMENT,
issued_date DATETIME NOT NULL,
due_date DATE NOT NULL,
isbn INT NOT NULL,
employee_id INT NOT NULL,
member_id INT NOT NULL,
PRIMARY KEY (borrowing_id),
Foreign Key (isbn) REFERENCES books(isbn) ON DELETE CASCADE ON UPDATE CASCADE,
Foreign Key (employee_id) REFERENCES employees(employee_id) ON DELETE CASCADE ON UPDATE CASCADE,
Foreign Key (member_id) REFERENCES members(member_id) ON DELETE CASCADE ON UPDATE CASCADE
);