The document outlines the creation of a library database with tables for Books, Members, and BorrowedBooks, detailing their structure and relationships. It includes SQL commands for inserting sample data into these tables and querying the database for various information, such as book titles, borrowed books, and book categories. Additionally, it demonstrates the use of SQL functions like COUNT and GROUP BY to analyze the data.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
9 views4 pages
Ict Project
The document outlines the creation of a library database with tables for Books, Members, and BorrowedBooks, detailing their structure and relationships. It includes SQL commands for inserting sample data into these tables and querying the database for various information, such as book titles, borrowed books, and book categories. Additionally, it demonstrates the use of SQL functions like COUNT and GROUP BY to analyze the data.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4
—------------------------- creates a new database where all the data for
CREATE DATABASE LibraryM; library will be stored.
USE LibraryM; BookID is a number given to each book. Each number for each book is different, INT PRIMARY KEY CREATE TABLE Books ( AUTO_INCREMENT makes a number column that starts at BookID INT PRIMARY KEY AUTO_INCREMENT, 1 and adds 1 for each new row, it also Ensures each number is unique and Uses that number to identify each Title VARCHAR(255) NOT NULL, row. Title is The name of the book and NOT NULL makes ARCHAR(2uthor VA55) NOT NULL, sure that every book has a title.Author is the name of the PublishedYear INT person who wrote the book, PublishedYear a number Category VARCHAR(100) representing the time when the book was made ,Category ); is the type of book or the genre
MemberID is a number given to each member,
CREATE TABLE Members ( each number defines a different member, Name MemberID INT PRIMARY KEY is just the members name and JoinDate is the AUTO_INCREMENT, date the member joined the library Name VARCHAR(255) NOT NULL, JoinDate DATE );
CREATE TABLE BorrowedBooks ( BorrowID is a number for each record of the
BorrowID INT PRIMARY KEY book borrowed, and BookID stores the ID of AUTO_INCREMENT, the borrowed book. MemberID stores the BookID INT, ID of the member who borrowed the book, MemberID INT, and BorrowDate is the date when the book BorrowDate DATE, was borrowed. FOREIGN KEY (BookID) REFERENCES Books(BookID), Foreign Keys ensure that the BookID and FOREIGN KEY (MemberID) REFERENCES MemberID used here exist in the respective Members(MemberID) Books and Members tables, maintaining the ); integrity of your data.
—---------------------------
INSERT INTO Books (Title, Author, PublishedYear,
Category) VALUES (“The Lost World”, “Arthur Conan Doyle”, 1912, “Fiction”), This inserts 10 sample records into the (“1984”, “George Orwell”, 1949, “Dystopian”), (“Brave New World”, “Aldous Huxley”, 1932, Books table, giving details like title, author, “Dystopian”), year, and category. (“To Kill a Mockingbird”, “Harper Lee”, 1960, “Classic”), (“Moby-Dick”, “Herman Melville”, 1851, “Adventure”), (“The Great Gatsby”, “F. Scott Fitzgerald”, 1925, “Classic”), (“Hamlet”, “William Shakespeare”, 1603, “Drama”), (“The Hobbit”, “J.R.R. Tolkien”, 1937, “Fantasy”), (“The Catcher in the Rye”, “J.D. Salinger”, 1951, “Classic”), (“Frankenstein”, 'Mary Shelley”, 1818, “Gothic”);
INSERT INTO Members (Name, JoinDate)
VALUES (“Alice Johnson”, “2023-01-15”), This adds 3 sample members to the Members (“Bob Smith”, “2022-11-20”) table with their joining dates.
(“Charlie Brown”, “2024-02-05”);
INSERT INTO BorrowedBooks (BookID, This records the borrowing activity by
MemberID, BorrowDate) VALUES combining specific books by BookID to members by MemberID with the dates when (1, 1, “2024-02-10”), the books were borrowed. (3, 2, “2024-02-12”) (5, 3, “2024-02-15”);
This query returns all books whose publication
SELECT * FROM Books WHERE PublishedYear years is between 1900 and 2000 BETWEEN 1900 AND 2000;
This sorts the list of books by their titles in
SELECT * FROM Books ORDER BY Title; alphabetical order SELECT * FROM Books ORDER BY It gets the 5 most recent books by sorting PublishedYear DESC LIMIT 5; them in descending order of "PublishedYear". _ ASIAH ____ This SQL renames the titles of the SELECT Title AS “Book Title”, Author AS column using the AS function from the “Written By”, PublishedYear AS “Year books table. Published” FROM Books;
SELECT Members.Name AS “Member
it renames the columns. Here it is Name”, Books.Title AS “Borrowed Book”, renamed Members.Name as “Member BorrowedBooks.BorrowDate AS “Date Name”, and Books.Title as “Borrowed Borrowed” Book” also BorrowedBooks.BorrowDate FROM BorrowedBooks as “Date Borrowed”. The ON keyword is JOIN Members ON used to specify the conditions for the joins BorrowedBooks.MemberID = between the tables. Members.MemberID JOIN Books ON BorrowedBooks.BookID = Books.BookID;
SELECT Books.Title AS “Book Title”,
This one also renames the columns. Members.Name AS “Borrower Name” Here it renames Books.Title as “Book FROM BorrowedBooks Title”, and Members.Name as “Borrower JOIN Books ON BorrowedBooks.BookID = Name”. Books.BookID Then the tables are joined with the JOIN Members ON keyword ON to specify the conditions for BorrowedBooks.MemberID = the joins between the tables. Members.MemberID;
SELECT COUNT(*) AS “Total Books
Borrowed” FROM BorrowedBooks; The COUNT(*) function that counts the number of rows in the result set or output. Then it renames the output column name of the COUNT(*) counts the number of total books rows. SELECT Category, COUNT(*) AS “Number This one selects category, then the of Books” FROM Books GROUP BY COUNT(*) function will see how many rows Category; that books has and then it will rename category and COUNT(*) as “Number of Books”. The GROUP BY function groups the results based on the unique values in the Category column.
SELECT Category, COUNT(*) AS “Number
of Books” This one wants to select the names of FROM Books categories in the book table. Then This GROUP BY function groups the results GROUP BY Category based on the unique values in the category. HAVING COUNT(*) > 5; This means that we want to see the total number of books for each category separately. After counting the number of books in each category using the COUNT(*) function, it only shows the categories that have more than 5 books.