-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLibrary DDL.sql
More file actions
95 lines (85 loc) · 2.7 KB
/
Library DDL.sql
File metadata and controls
95 lines (85 loc) · 2.7 KB
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
87
88
89
90
91
92
93
94
95
--------------------------------------------------------------------------------------
create schema Library;
GO
create table Library.Account(
accountID INT PRIMARY KEY,
personID INT unique not null,
isActive BIT default 1,
FOREIGN KEY (personID) REFERENCES Education.Person(ID),
);
GO
create table Library.Category(
categoryID INT IDENTITY PRIMARY KEY,
name varchar(50) unique not null,
);
GO
create table Library.Publisher(
publisherID INT IDENTITY PRIMARY KEY,
name varchar(50) unique not null,
);
GO
create table Library.Books(
bookID INT IDENTITY PRIMARY KEY,
title varchar(50) not null,
author varchar(50),
categoryID INT not null,
FOREIGN KEY (categoryID) REFERENCES Library.Category(categoryID),
);
GO
create table Library.BooksAvailable(
editionID INT IDENTITY PRIMARY KEY,
bookID INT not null,
publisherID INT not null,
editionNumber INT not null,
totalCopies INT default 0,
availableCopies INT default 0,
unique (bookID, publisherID, editionNumber),
FOREIGN KEY (bookID) REFERENCES Library.Books(bookID),
FOREIGN KEY (publisherID) REFERENCES Library.Publisher(publisherID),
);
GO
create table Library.Librarian(
librarianID INT PRIMARY KEY,
FOREIGN KEY (librarianID) REFERENCES Education.Person(ID),
);
GO
create table Library.Fines(
fineID INT IDENTITY PRIMARY KEY,
accountID INT not null,
date DATETIME default getdate(),
amount INT check (amount > 0),
status varchar(10) default 'notPaid' check(status in ('paid', 'notPaid')),
unique (accountID, date),
FOREIGN KEY (accountID) REFERENCES Library.Account(accountID),
);
GO
create table Library.BorrowRecords(
recordID INT IDENTITY PRIMARY KEY,
accountID INT not null,
editionID INT not null,
librarianID INT not null,
status varchar(20) default 'not returned' check(status IN ('not returned', 'returned')),
borrowDate DATETIME default getdate(),
dueDate DATETIME not null,
returnDate DATETIME,
FOREIGN KEY (accountID) REFERENCES Library.Account(accountID),
FOREIGN KEY (editionID) REFERENCES Library.BooksAvailable(editionID),
FOREIGN KEY (librarianID) REFERENCES Library.Librarian(librarianID),
);
GO
create table Library.ShelfLocation(
locationID INT IDENTITY PRIMARY KEY,
editionID INT NOT NULL UNIQUE,
aisle VARCHAR(20) NOT NULL, -- شماره راهرو
shelfNumber VARCHAR(20) NOT NULL, -- شماره قفسه
FOREIGN KEY (editionID) REFERENCES Library.BooksAvailable(editionID)
);
GO
create table Library.Log(
log_id INT IDENTITY PRIMARY KEY,
user_type VARCHAR(20), -- student, librarian, system
user_id INT,
action VARCHAR(255),
log_time DATETIME DEFAULT getdate(),
);
GO