0% found this document useful (0 votes)
19 views5 pages

Lesson 03 Guided Practice

This document outlines an exercise focused on SQL components for managing library data, specifically for the Greenfield City Library. It includes tasks for creating tables for book and patron management, detailing the necessary fields, data types, and constraints. Additionally, it provides SQL code examples for creating these tables and inserting data, along with optional discussion questions about data integrity and data types.

Uploaded by

Prince
Copyright
© © All Rights Reserved
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

Topics covered

  • SQL SELECT Statement,
  • SQL Best Practices,
  • Database Tables,
  • SQL Queries,
  • Data Types,
  • CHAR Data Type,
  • SQL Development,
  • SQL Programming,
  • SQL Database Operations,
  • Data Types Importance
0% found this document useful (0 votes)
19 views5 pages

Lesson 03 Guided Practice

This document outlines an exercise focused on SQL components for managing library data, specifically for the Greenfield City Library. It includes tasks for creating tables for book and patron management, detailing the necessary fields, data types, and constraints. Additionally, it provides SQL code examples for creating these tables and inserting data, along with optional discussion questions about data integrity and data types.

Uploaded by

Prince
Copyright
© © All Rights Reserved
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

Topics covered

  • SQL SELECT Statement,
  • SQL Best Practices,
  • Database Tables,
  • SQL Queries,
  • Data Types,
  • CHAR Data Type,
  • SQL Development,
  • SQL Programming,
  • SQL Database Operations,
  • Data Types Importance

Lesson 03: Working with Operators, Constraints,

and Data Types

Overview

In this exercise, you will focus on the components of SQL in the context of library
data management. It aims to enhance your understanding of SQL operators,
constraints, and data types through a practical, real-world application.

Instructions

• Work through all tasks individually or within your group


• Divide your time evenly among the tasks to ensure a comprehensive
understanding

Task

Your task is to create a system for a library to manage its books and patrons
effectively. You will work on a database for Greenfield City Library.

1. Book Management
• Create a table for managing books. Include the book ID, title,
author, and availability status. Utilize data types and constraints
appropriately.
• Create table Books with fields: BookID (primary key), Title, Author,
and Status. Use VARCHAR for title and author, INT for BookID, and a
CHAR for status ('Available', 'Borrowed')
• Add NOT NULL constraints to ensure all fields are filled

2. Patron Management
• Develop a table to manage patron information, including ID, name,
and borrowed books. Incorporate key constraints and relationships
between the books and patron tables.
• Create table Patrons with fields: PatronID (primary key), Name, and
BorrowedBookID
• Use INT for PatronID, VARCHAR for Name, and INT for
BorrowedBookID.
• Establish a foreign key relationship between BorrowedBookID in
Patrons and BookID in Books to link the records

Discussion Questions (Optional)


If time permits, discuss the following questions:

• How can SQL constraints ensure data integrity in a library system?


• Discuss the importance of choosing the right data types for different fields
in a library database.

Answer Key
Note: Create a database and tables using the following
code:

1. Book Management:
• Create a table for managing books. Include the book ID, title,
author, and availability status. Utilize data types and constraints
appropriately.

SQL Code:

SQLCREATE DATABASE IF NOT EXISTS LIBRARY_DB;

USE LIBMAN_DB;

SET default_storage_engine = INNODB

• Create table Books with fields: BookID (primary key), Title,


Author, and Status. Use VARCHAR for title and author, INT for
BookID, and a CHAR for status ('Available', 'Borrowed')
SQL Code:

CREATE TABLE Books (

BookID INT AUTO_INCREMENT PRIMARY KEY,

Title VARCHAR(255) NOT NULL,

Author VARCHAR(255) NOT NULL,

Status CHAR(10) NOT NULL CHECK (Status IN ('Available', 'Borrowed'))

);

• Insert the data using the following code:

SQL Code:

INSERT INTO Books (Title, Author, Status) VALUES

('To Kill a Mockingbird', 'Harper Lee', 'Available'),

('1984', 'George Orwell', 'Borrowed'),

('The Great Gatsby', 'F. Scott Fitzgerald', 'Available'),

('One Hundred Years of Solitude', 'Gabriel Garcia Marquez',


'Available'),

('Pride and Prejudice', 'Jane Austen', 'Borrowed');

5. Show the table using the following code:

SELECT * FROM LIBRARY_DB.Books;

Output:
2. Patron Management:
• Create a Table named Patrons with the given items and use
Foreign Key using the reference as Books(BookID):

SQL Code:

CREATE TABLE Patrons (

PatronID INT AUTO_INCREMENT PRIMARY KEY,

Name VARCHAR(255) NOT NULL,

BorrowedBookID INT,

FOREIGN KEY (BorrowedBookID) REFERENCES


Books(BookID)

);

• Insert the data using the following code:

SQL Code:

INSERT INTO Patrons (Name, BorrowedBookID) VALUES

('John Doe', 2),

('Jane Smith', NULL),

('Emily Johnson', 5),


('Michael Brown', NULL),

('Linda White', NULL);

• Show the table using the given code:

SQL Code:

SELECT * FROM LIBRARY_DB.Books;

You might also like