0% found this document useful (0 votes)
30 views

To design a simple library management system database schema in MySQL

Uploaded by

Nadeem Shakir
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views

To design a simple library management system database schema in MySQL

Uploaded by

Nadeem Shakir
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 17

To design a simple library management system database schema in MySQL, we'll need tables to store

information about books, authors, members, loans (borrowed books), and categories. Below is the SQL
code to create the schema:

1. authors Table

Stores details about authors.

sql

Copy code

CREATE TABLE authors (

author_id INT AUTO_INCREMENT PRIMARY KEY,

first_name VARCHAR(100) NOT NULL,

last_name VARCHAR(100) NOT NULL,

birth_date DATE,

nationality VARCHAR(100)

);

2. categories Table

Stores book categories (e.g., Fiction, Non-Fiction, Science, etc.).

sql

Copy code

CREATE TABLE categories (

category_id INT AUTO_INCREMENT PRIMARY KEY,

name VARCHAR(100) NOT NULL

);

3. books Table

Stores details about the books available in the library.

sql

Copy code

CREATE TABLE books (

book_id INT AUTO_INCREMENT PRIMARY KEY,

title VARCHAR(255) NOT NULL,


author_id INT,

category_id INT,

publication_date DATE,

isbn VARCHAR(20) UNIQUE,

copies_available INT DEFAULT 0,

FOREIGN KEY (author_id) REFERENCES authors(author_id),

FOREIGN KEY (category_id) REFERENCES categories(category_id)

);

4. members Table

Stores information about library members.

sql

Copy code

CREATE TABLE members (

member_id INT AUTO_INCREMENT PRIMARY KEY,

first_name VARCHAR(100) NOT NULL,

last_name VARCHAR(100) NOT NULL,

email VARCHAR(100) UNIQUE,

phone_number VARCHAR(15),

join_date DATE DEFAULT CURRENT_DATE

);

5. loans Table

Tracks which books have been borrowed by members.

sql

Copy code

CREATE TABLE loans (

loan_id INT AUTO_INCREMENT PRIMARY KEY,

member_id INT,

book_id INT,

loan_date DATE DEFAULT CURRENT_DATE,


due_date DATE,

return_date DATE,

FOREIGN KEY (member_id) REFERENCES members(member_id),

FOREIGN KEY (book_id) REFERENCES books(book_id)

);

6. book_reviews Table (Optional)

Stores reviews for books from members.

sql

Copy code

CREATE TABLE book_reviews (

review_id INT AUTO_INCREMENT PRIMARY KEY,

book_id INT,

member_id INT,

review_text TEXT,

rating INT CHECK (rating BETWEEN 1 AND 5),

review_date DATE DEFAULT CURRENT_DATE,

FOREIGN KEY (book_id) REFERENCES books(book_id),

FOREIGN KEY (member_id) REFERENCES members(member_id)

);

Example Query to Use the Database

1. Add a new author:

sql

Copy code

INSERT INTO authors (first_name, last_name, birth_date, nationality)

VALUES ('George', 'Orwell', '1903-06-25', 'British');

2. Add a new category:

sql

Copy code

INSERT INTO categories (name) VALUES ('Dystopian');


3. Add a new book:

sql

Copy code

INSERT INTO books (title, author_id, category_id, publication_date, isbn, copies_available)

VALUES ('1984', 1, 1, '1949-06-08', '9780451524935', 5);

4. Register a new member:

sql

Copy code

INSERT INTO members (first_name, last_name, email, phone_number)

VALUES ('John', 'Doe', '[email protected]', '555-1234');

5. Loan a book to a member:

sql

Copy code

INSERT INTO loans (member_id, book_id, loan_date, due_date)

VALUES (1, 1, '2024-11-25', '2024-12-09');

This schema allows you to track authors, categories, books, members, loans, and reviews, all of which
are central to a library management system.

4o mini

You said:

Create a database schema for a university management system. give code in mysql.

ChatGPT said:

ChatGPT

A University Management System typically involves several entities such as students, faculty, courses,
departments, enrollments, and schedules. Below is an example of a database schema for a University
Management System with MySQL code.

1. departments Table

Stores the departments within the university.

sql

Copy code

CREATE TABLE departments (


department_id INT AUTO_INCREMENT PRIMARY KEY,

name VARCHAR(100) NOT NULL,

description TEXT

);

2. courses Table

Stores courses offered by the university.

sql

Copy code

CREATE TABLE courses (

course_id INT AUTO_INCREMENT PRIMARY KEY,

department_id INT,

course_name VARCHAR(255) NOT NULL,

course_code VARCHAR(20) UNIQUE NOT NULL,

course_description TEXT,

credits INT NOT NULL,

FOREIGN KEY (department_id) REFERENCES departments(department_id)

);

3. students Table

Stores student information.

sql

Copy code

CREATE TABLE students (

student_id INT AUTO_INCREMENT PRIMARY KEY,

first_name VARCHAR(100) NOT NULL,

last_name VARCHAR(100) NOT NULL,

email VARCHAR(100) UNIQUE NOT NULL,

phone_number VARCHAR(15),

date_of_birth DATE,

enrollment_date DATE DEFAULT CURRENT_DATE


);

4. faculty Table

Stores faculty (professors/teachers) information.

sql

Copy code

CREATE TABLE faculty (

faculty_id INT AUTO_INCREMENT PRIMARY KEY,

first_name VARCHAR(100) NOT NULL,

last_name VARCHAR(100) NOT NULL,

email VARCHAR(100) UNIQUE NOT NULL,

phone_number VARCHAR(15),

department_id INT,

hire_date DATE DEFAULT CURRENT_DATE,

FOREIGN KEY (department_id) REFERENCES departments(department_id)

);

5. enrollments Table

Stores which students are enrolled in which courses.

sql

Copy code

CREATE TABLE enrollments (

enrollment_id INT AUTO_INCREMENT PRIMARY KEY,

student_id INT,

course_id INT,

enrollment_date DATE DEFAULT CURRENT_DATE,

grade CHAR(2),

FOREIGN KEY (student_id) REFERENCES students(student_id),

FOREIGN KEY (course_id) REFERENCES courses(course_id)

);

6. class_schedules Table
Stores course schedules, detailing when and where courses are offered.

sql

Copy code

CREATE TABLE class_schedules (

schedule_id INT AUTO_INCREMENT PRIMARY KEY,

course_id INT,

faculty_id INT,

start_date DATE,

end_date DATE,

days_of_week VARCHAR(20), -- Example: 'Monday, Wednesday, Friday'

start_time TIME,

end_time TIME,

room_number VARCHAR(10),

FOREIGN KEY (course_id) REFERENCES courses(course_id),

FOREIGN KEY (faculty_id) REFERENCES faculty(faculty_id)

);

7. grades Table (Optional)

Stores detailed grades for students in a specific course, including exam marks.

sql

Copy code

CREATE TABLE grades (

grade_id INT AUTO_INCREMENT PRIMARY KEY,

student_id INT,

course_id INT,

exam_date DATE,

marks INT,

FOREIGN KEY (student_id) REFERENCES students(student_id),

FOREIGN KEY (course_id) REFERENCES courses(course_id)

);
8. assignments Table (Optional)

Stores assignments given in various courses.

sql

Copy code

CREATE TABLE assignments (

assignment_id INT AUTO_INCREMENT PRIMARY KEY,

course_id INT,

assignment_title VARCHAR(255) NOT NULL,

due_date DATE,

max_marks INT,

FOREIGN KEY (course_id) REFERENCES courses(course_id)

);

9. assignment_submissions Table (Optional)

Stores student submissions for assignments.

sql

Copy code

CREATE TABLE assignment_submissions (

submission_id INT AUTO_INCREMENT PRIMARY KEY,

assignment_id INT,

student_id INT,

submission_date DATE DEFAULT CURRENT_DATE,

marks_obtained INT,

submission_file VARCHAR(255),

FOREIGN KEY (assignment_id) REFERENCES assignments(assignment_id),

FOREIGN KEY (student_id) REFERENCES students(student_id)

);

Example Queries

1. Add a new department:


sql

Copy code

INSERT INTO departments (name, description)

VALUES ('Computer Science', 'Department of Computer Science and Engineering');

2. Add a new course:

sql

Copy code

INSERT INTO courses (department_id, course_name, course_code, course_description, credits)

VALUES (1, 'Data Structures', 'CS101', 'Introduction to data structures', 4);

3. Add a new student:

sql

Copy code

INSERT INTO students (first_name, last_name, email, phone_number, date_of_birth)

VALUES ('John', 'Doe', '[email protected]', '555-1234', '1999-04-15');

4. Enroll a student in a course:

sql

Copy code

INSERT INTO enrollments (student_id, course_id, grade)

VALUES (1, 1, NULL); -- Student ID 1 enrolls in Course ID 1 with no grade assigned yet

5. Assign a faculty member to a course:

sql

Copy code

INSERT INTO class_schedules (course_id, faculty_id, start_date, end_date, days_of_week, start_time,


end_time, room_number)

VALUES (1, 1, '2024-09-01', '2024-12-15', 'Monday, Wednesday, Friday', '09:00:00', '10:30:00', 'Room
101');

Summary of Tables:

1. departments: Stores information about university departments.


2. courses: Stores course details, linked to departments.

3. students: Stores information about students.

4. faculty: Stores details of faculty members and their departments.

5. enrollments: Records student enrollments in courses.

6. class_schedules: Tracks when and where courses are scheduled.

7. grades: Optionally stores student exam grades.

8. assignments: Optionally tracks assignments for courses.

9. assignment_submissions: Optionally stores student submissions for assignments.

This schema provides a basic structure for managing university-related data such as courses, students,
faculty, and schedules.

4o mini

You said:

Design a database schema for an e-commerce website. give code in mysql

ChatGPT said:

ChatGPT

To design a database schema for an e-commerce website, we need to account for several key entities,
including products, customers, orders, payments, categories, and reviews. Below is an example of a
MySQL database schema for an e-commerce website:

1. categories Table

Stores categories for products (e.g., Electronics, Apparel, Home & Kitchen, etc.).

sql

Copy code

CREATE TABLE categories (

category_id INT AUTO_INCREMENT PRIMARY KEY,

name VARCHAR(100) NOT NULL,

description TEXT

);

2. products Table

Stores product information, such as name, description, price, stock, and category.

sql
Copy code

CREATE TABLE products (

product_id INT AUTO_INCREMENT PRIMARY KEY,

category_id INT,

name VARCHAR(255) NOT NULL,

description TEXT,

price DECIMAL(10, 2) NOT NULL,

stock_quantity INT NOT NULL,

image_url VARCHAR(255),

created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,

updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,

FOREIGN KEY (category_id) REFERENCES categories(category_id)

);

3. customers Table

Stores customer information, such as name, email, and address.

sql

Copy code

CREATE TABLE customers (

customer_id INT AUTO_INCREMENT PRIMARY KEY,

first_name VARCHAR(100) NOT NULL,

last_name VARCHAR(100) NOT NULL,

email VARCHAR(100) UNIQUE NOT NULL,

password_hash VARCHAR(255) NOT NULL, -- Store hashed passwords

phone_number VARCHAR(15),

shipping_address TEXT,

billing_address TEXT,

created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP

);

4. orders Table
Stores orders placed by customers, with order status and shipping details.

sql

Copy code

CREATE TABLE orders (

order_id INT AUTO_INCREMENT PRIMARY KEY,

customer_id INT,

order_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,

status ENUM('Pending', 'Processing', 'Shipped', 'Delivered', 'Canceled') DEFAULT 'Pending',

total_amount DECIMAL(10, 2) NOT NULL,

shipping_address TEXT,

billing_address TEXT,

FOREIGN KEY (customer_id) REFERENCES customers(customer_id)

);

5. order_items Table

Stores details of each item in an order (product, quantity, price).

sql

Copy code

CREATE TABLE order_items (

order_item_id INT AUTO_INCREMENT PRIMARY KEY,

order_id INT,

product_id INT,

quantity INT NOT NULL,

price DECIMAL(10, 2) NOT NULL,

total DECIMAL(10, 2) NOT NULL,

FOREIGN KEY (order_id) REFERENCES orders(order_id),

FOREIGN KEY (product_id) REFERENCES products(product_id)

);

6. payments Table

Stores payment information for orders (e.g., payment method, amount, and status).
sql

Copy code

CREATE TABLE payments (

payment_id INT AUTO_INCREMENT PRIMARY KEY,

order_id INT,

payment_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,

payment_method ENUM('Credit Card', 'PayPal', 'Bank Transfer', 'Cash on Delivery') NOT NULL,

payment_status ENUM('Pending', 'Completed', 'Failed', 'Refunded') DEFAULT 'Pending',

amount DECIMAL(10, 2) NOT NULL,

FOREIGN KEY (order_id) REFERENCES orders(order_id)

);

7. reviews Table

Stores customer reviews for products.

sql

Copy code

CREATE TABLE reviews (

review_id INT AUTO_INCREMENT PRIMARY KEY,

product_id INT,

customer_id INT,

rating INT CHECK (rating BETWEEN 1 AND 5),

review_text TEXT,

review_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,

FOREIGN KEY (product_id) REFERENCES products(product_id),

FOREIGN KEY (customer_id) REFERENCES customers(customer_id)

);

8. shopping_cart Table

Stores items that customers have added to their shopping cart before completing the purchase.

sql

Copy code
CREATE TABLE shopping_cart (

cart_item_id INT AUTO_INCREMENT PRIMARY KEY,

customer_id INT,

product_id INT,

quantity INT NOT NULL,

added_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,

FOREIGN KEY (customer_id) REFERENCES customers(customer_id),

FOREIGN KEY (product_id) REFERENCES products(product_id)

);

9. product_images Table (Optional)

Stores additional images for products.

sql

Copy code

CREATE TABLE product_images (

image_id INT AUTO_INCREMENT PRIMARY KEY,

product_id INT,

image_url VARCHAR(255) NOT NULL,

FOREIGN KEY (product_id) REFERENCES products(product_id)

);

10. coupons Table (Optional)

Stores coupon codes that customers can apply to orders for discounts.

sql

Copy code

CREATE TABLE coupons (

coupon_id INT AUTO_INCREMENT PRIMARY KEY,

code VARCHAR(50) UNIQUE NOT NULL,

discount_percentage DECIMAL(5, 2) NOT NULL,

valid_from DATE,

valid_until DATE,
status ENUM('Active', 'Expired', 'Disabled') DEFAULT 'Active'

);

11. order_coupons Table (Optional)

Stores coupons used in specific orders.

sql

Copy code

CREATE TABLE order_coupons (

order_coupon_id INT AUTO_INCREMENT PRIMARY KEY,

order_id INT,

coupon_id INT,

FOREIGN KEY (order_id) REFERENCES orders(order_id),

FOREIGN KEY (coupon_id) REFERENCES coupons(coupon_id)

);

Example Queries

1. Add a new category:

sql

Copy code

INSERT INTO categories (name, description)

VALUES ('Electronics', 'Electronics and gadgets');

2. Add a new product:

sql

Copy code

INSERT INTO products (category_id, name, description, price, stock_quantity, image_url)

VALUES (1, 'Smartphone', 'Latest model with advanced features', 499.99, 100,
'https://example.com/images/smartphone.jpg');

3. Register a new customer:

sql

Copy code
INSERT INTO customers (first_name, last_name, email, password_hash, phone_number,
shipping_address, billing_address)

VALUES ('John', 'Doe', '[email protected]', 'hashed_password', '555-1234', '123 Elm St, City, Country',
'123 Elm St, City, Country');

4. Create a new order for a customer:

sql

Copy code

INSERT INTO orders (customer_id, status, total_amount, shipping_address, billing_address)

VALUES (1, 'Pending', 499.99, '123 Elm St, City, Country', '123 Elm St, City, Country');

5. Add items to an order:

sql

Copy code

INSERT INTO order_items (order_id, product_id, quantity, price, total)

VALUES (1, 1, 1, 499.99, 499.99);

6. Record a payment for an order:

sql

Copy code

INSERT INTO payments (order_id, payment_method, payment_status, amount)

VALUES (1, 'Credit Card', 'Completed', 499.99);

7. Add a review for a product:

sql

Copy code

INSERT INTO reviews (product_id, customer_id, rating, review_text)

VALUES (1, 1, 5, 'Great product! Highly recommend.');

8. Add a coupon:

sql

Copy code

INSERT INTO coupons (code, discount_percentage, valid_from, valid_until)

VALUES ('SUMMER20', 20.00, '2024-06-01', '2024-08-31');


9. Apply a coupon to an order:

sql

Copy code

INSERT INTO order_coupons (order_id, coupon_id)

VALUES (1, 1); -- Order ID 1 uses coupon ID 1 (SUMMER20)

Summary of Tables:

1. categories: Product categories (e.g., Electronics, Apparel).

2. products: Stores product details, linked to categories.

3. customers: Stores customer details, such as name, email, and addresses.

4. orders: Stores order details, including the total amount and status.

5. order_items: Stores products in each order, including quantity and price.

6. payments: Tracks payments for orders, including payment method and status.

7. reviews: Customer reviews for products, including ratings.

8. shopping_cart: Items added to the shopping cart before checkout.

9. product_images: Additional images for products.

10. coupons: Discount codes that can be applied to orders.

11. order_coupons: Tracks which coupons are applied to orders.

You might also like