Experiment 3
Title: SQL Query Writing 1
Submitted By: Ponugumatla Bhavish
URN: 2023-B-04072004
Semester: III
Program: BCA
Course: Fundamentals of SQL and Data Modeling for the
Web
Here’s a sample database schema for a Library Management System. It includes tables, columns,
data types, and relationships between them using primary and foreign keys.
1. Member Table
This table stores information about the library members.
Column Name Data Type Constraints Description
PRIMARY KEY, Unique identifier for each
MemberID INTEGER
AUTO_INCREMENT member
VARCHAR(25
Name NOT NULL Name of the member
5)
VARCHAR(25 Email address of the
Email UNIQUE, NOT NULL
5) member
VARCHAR(15 Phone number of the
PhoneNumber
) member
MembershipStartDa Start date of the
DATE NOT NULL
te membership
2. Book Table
This table stores information about books available in the library.
Column Name Data Type Constraints Description
Unique identifier for each
BookID INTEGER PRIMARY KEY, AUTO_INCREMENT
book
VARCHAR(25
Title NOT NULL Title of the book
5)
VARCHAR(25
Author NOT NULL Author of the book
5)
VARCHAR(10
Genre Genre of the book
0)
Year the book was
PublishedYear YEAR
published
VARCHAR(13 International Standard
ISBN UNIQUE, NOT NULL
) Book Number (ISBN)
AvailabilityStat VARCHAR(50 Availability status
NOT NULL
us ) (Available/Checked Out)
FOREIGN KEY (PublisherID)
Foreign key linking to
PublisherID INTEGER REFERENCES
Publisher
Publisher(PublisherID)
3. Staff Table
This table stores information about the library staff.
Column
Data Type Constraints Description
Name
PRIMARY KEY, Unique identifier for each staff
StaffID INTEGER
AUTO_INCREMENT member
VARCHAR(25
Name NOT NULL Name of the staff member
5)
VARCHAR(10
Position Position of the staff member
0)
VARCHAR(25
ContactInfo Contact information of the staff
5)
4. Publisher Table
This table stores information about the publishers of the books.
Column
Data Type Constraints Description
Name
PRIMARY KEY, Unique identifier for each
PublisherID INTEGER
AUTO_INCREMENT publisher
VARCHAR(25
Name NOT NULL Name of the publisher
5)
Address TEXT Address of the publisher
VARCHAR(25 Contact information of the
ContactInfo
5) publisher
5. BorrowingTransaction Table
This table records the borrowing transactions where members check out books.
Column
Data Type Constraints Description
Name
TransactionI Unique identifier for each
INTEGER PRIMARY KEY, AUTO_INCREMENT
D transaction
Date when the book was
BorrowDate DATE NOT NULL
borrowed
Date when the book is
DueDate DATE NOT NULL
due to be returned
Date when the book was
ReturnDate DATE
actually returned
FineAmount DECIMAL(10 Fine for late return (if
Column
Data Type Constraints Description
Name
, 2) any)
FOREIGN KEY (MemberID) REFERENCES Foreign key linking to the
MemberID INTEGER
Member(MemberID) Member table
FOREIGN KEY (BookID) REFERENCES Foreign key linking to the
BookID INTEGER
Book(BookID) Book table
FOREIGN KEY (StaffID) REFERENCES Foreign key linking to the
StaffID INTEGER
Staff(StaffID) Staff table
Relationships and Keys
● Primary Keys (PK):
o Each table has a primary key (e.g., MemberID, BookID, PublisherID, StaffID,
TransactionID).
● Foreign Keys (FK):
o PublisherID in the Book table is a foreign key referencing Publisher(PublisherID).
o MemberID in the BorrowingTransaction table is a foreign key referencing
Member(MemberID).
o BookID in the BorrowingTransaction table is a foreign key referencing
Book(BookID).
o StaffID in the BorrowingTransaction table is a foreign key referencing Staff(StaffID).
Relationships:
1. One-to-Many (1:M):
o A Publisher can have many Books (one publisher can publish many books).
o A Member can borrow many Books (one member can borrow multiple books over
time).
o A Staff can assist with many BorrowingTransactions (one staff member can manage
many borrowing transactions).
2. Many-to-Many (M:M):
o Members can borrow Books many times, and Books can be borrowed by many
Members. This relationship is handled by the BorrowingTransaction table.
Example of SQL Creation Statements
Here’s how you can create these tables with SQL:
sql
Copy code
CREATE TABLE Publisher (
PublisherID INT PRIMARY KEY AUTO_INCREMENT,
Name VARCHAR(255) NOT NULL,
Address TEXT,
ContactInfo VARCHAR(255)
);
CREATE TABLE Book (
BookID INT PRIMARY KEY AUTO_INCREMENT,
Title VARCHAR(255) NOT NULL,
Author VARCHAR(255) NOT NULL,
Genre VARCHAR(100),
PublishedYear YEAR,
ISBN VARCHAR(13) NOT NULL UNIQUE,
AvailabilityStatus VARCHAR(50) NOT NULL,
PublisherID INT,
FOREIGN KEY (PublisherID) REFERENCES Publisher(PublisherID)
);
CREATE TABLE Member (
MemberID INT PRIMARY KEY AUTO_INCREMENT,
Name VARCHAR(255) NOT NULL,
Email VARCHAR(255) NOT NULL UNIQUE,
PhoneNumber VARCHAR(15),
MembershipStartDate DATE NOT NULL
);
CREATE TABLE Staff (
StaffID INT PRIMARY KEY AUTO_INCREMENT,
Name VARCHAR(255) NOT NULL,
Position VARCHAR(100),
ContactInfo VARCHAR(255)
);
CREATE TABLE BorrowingTransaction (
TransactionID INT PRIMARY KEY AUTO_INCREMENT,
BorrowDate DATE NOT NULL,
DueDate DATE NOT NULL,
ReturnDate DATE,
FineAmount DECIMAL(10, 2),
MemberID INT,
BookID INT,
StaffID INT,
FOREIGN KEY (MemberID) REFERENCES Member(MemberID),
FOREIGN KEY (BookID) REFERENCES Book(BookID),
FOREIGN KEY (StaffID) REFERENCES Staff(StaffID)
);
Conclusion
This schema provides a well-organized structure for a Library Management System with tables,
columns, data types, and relationships. It ensures data integrity through the use of primary and
foreign keys, and it accommodates essential library functionalities like managing books, members,
borrowing transactions, and staff interactions.