Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 10

DBMS

Lab Assignment
Alekhya Gudibandla
22mc3015
Mathematics and Computing

Q1 Design an ER diagram for a simple library management system that tracks books, authors, and
library members. The system should keep track of which books are available in the library and which
are currently checked out by members. Each book has a unique ISBN and title and may be written by
one or more authors. Each author has a unique ID and a name. Each member of the library has a
unique ID and a name and can check out multiple books.

Write down the basic implementation of the library management system using the C programming
language, focusing on managing books and members. The program should define structures for books
and members to represent entities in the ER diagram. Add functions like addBook and addMember to
allow adding books and members to the system. The displayBooks and displayMembers functions
print the list of books and members, respectively.

ANSWER:

1. Entities
a. Books
This table contains all the books present in the library
b. Authors
This entity represents authors of the books.
c. Members
This entity represents library members
d. Checkout
This entity represents the checkout history of books by members.
e. Book_Authors
This entity represents the relationship between books and authors. This table allows us to
search for all the books written by an author or all the authors of a book.
2. Attributes
a. Books
• ISBN (Primary key)
• Title
• No. of copies
b. Authors
• Author_ID (Primary Key)
• Name.
c. Members
• Member_ID (Primary Key)
• Name
d. Checkout
• Checkout_ID (Primary Key)
• Book_ISBN (Foreign Key referencing ISBN in Book table)
• Member_ID (Foreign Key referencing Member_ID in Member table)
• Checkout_Date
• Return_Date

e. Book_Authors
• Book_ISBN
• Author_ID
3. Relationships
a. Book to Author
Many to many relationship

b. Book to checkout
One to many relationship

c. Members to checkout
One to many relationship

ER DIAGRAM:
CODE:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

// Define structures for Book and Member


typedef struct {
char ISBN[20];
char title[100];
int copies;
} Book;

typedef struct {
int member_id;
char name[100];
} Member;

// Function prototypes
void addBook(Book books[], int *num_books);
void addMember(Member members[], int *num_members);
void displayBooks(Book books[], int num_books);
void displayMembers(Member members[], int num_members);

int main() {
Book books[100]; // Maximum 100 books
Member members[100]; // Maximum 100 members
int num_books = 0;
int num_members = 0;
int choice;

do {
printf("\nLibrary Management System Menu\n");
printf("1. Add Book\n");
printf("2. Add Member\n");
printf("3. Display Books\n");
printf("4. Display Members\n");
printf("5. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);

switch (choice) {
case 1:
addBook(books, &num_books);
break;
case 2:
addMember(members, &num_members);
break;
case 3:
displayBooks(books, num_books);
break;
case 4:
displayMembers(members, num_members);
break;
case 5:
printf("Exiting program.\n");
break;
default:
printf("Invalid choice! Please enter a number between 1 and 5.\n");
}
} while (choice != 5);

return 0;
}

void addBook(Book books[], int *num_books) {


if (*num_books >= 100) {
printf("Maximum number of books reached.\n");
return;
}

printf("Enter ISBN: ");


scanf("%s", books[*num_books].ISBN);

printf("Enter title: ");


scanf(" %[^\n]", books[*num_books].title);

printf("Enter number of copies: ");


scanf("%d", &books[*num_books].copies);

(*num_books)++;
printf("Book added successfully.\n");
}

void addMember(Member members[], int *num_members) {


if (*num_members >= 100) {
printf("Maximum number of members reached.\n");
return;
}

printf("Enter member ID: ");


scanf("%d", &members[*num_members].member_id);

printf("Enter name: ");


scanf(" %[^\n]", members[*num_members].name);

(*num_members)++;
printf("Member added successfully!\n");
}

void displayBooks(Book books[], int num_books) {


printf("\nList of Books:\n");

if (num_books == 0) {
printf("No books available.\n");
return;
}

for (int i = 0; i < num_books; i++) {


printf("ISBN: %s, Title: %s, Copies: %d\n", books[i].ISBN, books[i].title, books[i].copies);
}
}

void displayMembers(Member members[], int num_members) {


printf("\nList of Members:\n");

if (num_members == 0) {
printf("No members available.\n");
return;
}

for (int i = 0; i < num_members; i++) {


printf("Member ID: %d, Name: %s\n", members[i].member_id, members[i].name);
}
}

Q2 Consider an ER diagram for a university database system that includes entities like student,
course, and enrollment. Each student can enroll in multiple courses, and each course can have
multiple students enrolled. Design an ER diagram for this system and convert it into a relation model.
Then, implement basic functionalities in the C language to add students, courses, and enrollments and
display them. Add Functions like addStudent, addCourse, and enrollStudent allow adding students,
courses, and enrollments to the system. The displayStudents, displayCourses, and displayEnrollments
functions print the list of students, courses, and enrollments, respectively.

ANSWER:

1. Entities:
a. Student
b. Course
c. Enrollment
2. Attributes:
a. Student:
• StudentID (Primary Key)
• Name
• Email
• Address
• Phone number
b. Course:
• CourseID (Primary Key)
• Title
• Department
• Credits
c. Enrollment:
• EnrollmentID (Primary Key)
• StudentID (Foreign Key referencing Student)
• CourseID (Foreign Key referencing Course)
• Enrollment date
• Grade
3. Relationships:
a. Student – Enrollment
One-to-Many
b. Course – Enrollment
One-to-Many
4. Relational Model:
Tables:
• Student (StudentID, Name, Address, Email, PhoneNumber)
• Course (CourseID, Title, Credits, Department)
• Course (CourseID, Title, Credits, Department)
• Enrollment (EnrollmentID, StudentID, CourseID, EnrollmentDate, Grade)

ER Diagram
CODE:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct {
int id;
char name[100];
} Student;

typedef struct {
char code[20];
char name[100];
} Course;
typedef struct {
int studentID;
char courseCode[20];
} Enrollment;

static void addStudent(Student *students, int *numStudents) {


printf("Enter ID of the student: ");
if (scanf("%d", &students[*numStudents].id) != 1) {
printf("Invalid input. Please try again.\n");
return;
}
printf("Enter name of the student: ");
fflush(stdin);
if (scanf(" %99[^\n]", students[*numStudents].name) != 1) {
printf("Invalid input. Please try again.\n");
return;
}
(*numStudents)++;
printf("Student added successfully.\n");
}

static void addCourse(Course *courses, int *numCourses) {


printf("Enter code of the course: ");
if (scanf("%19s", courses[*numCourses].code) != 1) {
printf("Invalid input. Please try again.\n");
return;
}
printf("Enter name of the course: ");
fflush(stdin);
if (scanf(" %99[^\n]", courses[*numCourses].name) != 1) {
printf("Invalid input. Please try again.\n");
return;
}
(*numCourses)++;
printf("Course added successfully.\n");
}

static void enrollStudent(Enrollment *enrollments, int *numEnrollments, int studentID,


const char *courseCode) {
enrollments[*numEnrollments].studentID = studentID;
strcpy(enrollments[*numEnrollments].courseCode, courseCode);
(*numEnrollments)++;
printf("Student enrolled in the course successfully.\n");
}

static void displayStudents(const Student *students, int numStudents) {


printf("\nList of students:\n");
for (int i = 0; i < numStudents; i++) {
printf("ID: %d, Name: %s\n", students[i].id, students[i].name);
}
}

static void displayCourses(const Course *courses, int numCourses) {


printf("\nList of courses:\n");
for (int i = 0; i < numCourses; i++) {
printf("Code: %s, Name: %s\n", courses[i].code, courses[i].name);
}
}

static void displayEnrollments(const Enrollment *enrollments, int numEnrollments) {


printf("\nList of enrollments:\n");
for (int i = 0; i < numEnrollments; i++) {
printf("StudentID: %d, CourseCode: %s\n", enrollments[i].studentID,
enrollments[i].courseCode);
}
}

int main() {
Student students[100];
Course courses[100];
Enrollment enrollments[100];
int numStudents = 0;
int numCourses = 0;
int numEnrollments = 0;
int choice;

do {
printf("\nUniversity Database System\n");
printf("1. Add Student\n");
printf("2. Add Course\n");
printf("3. Enroll Student\n");
printf("4. Display Students\

You might also like