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

Database Management Systems

Assignment

Uploaded by

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

Database Management Systems

Assignment

Uploaded by

Taskeen Zafar
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Database Management Systems (DBMS)

A Database Management System (DBMS) is a crucial component in modern information


systems, serving as the backbone for storing, managing, and retrieving data efficiently. In this
context, we'll explore the design and implementation of a relational database for a small
business, covering essential aspects such as Entity-Relationship (ER) diagrams and SQL queries
for CRUD (Create, Read, Update, Delete) operations.

1. Understanding the Small Business Scenario

Let's consider a small bookstore as our business example. The bookstore needs a database to
manage its inventory, customers, orders, and sales. Here’s how we can conceptualize and
implement this database using DBMS principles.

2. Entity-Relationship (ER) Diagram

An ER diagram helps visualize the database schema by defining entities, their attributes, and
relationships between them.

 Entities:
o Book: (BookID, Title, Author, Price, Genre, PublisherID)
o Author: (AuthorID, Name, Birthdate, Nationality)
o Publisher: (PublisherID, Name, Location)
o Customer: (CustomerID, Name, Email, Phone)
o Order: (OrderID, OrderDate, CustomerID)
o OrderItem: (OrderItemID, OrderID, BookID, Quantity)
 Relationships:
o Book - Author: Many-to-Many (A book can have multiple authors, an author can
write multiple books)
o Book - Publisher: Many-to-One (A book has one publisher, a publisher can have
many books)
o Customer - Order: One-to-Many (A customer can place many orders)
o Order - OrderItem: One-to-Many (An order can have multiple items)

3. SQL Schema Definition

Based on the ER diagram, we can define the SQL schema for our database:

sql
Copy code
CREATE TABLE Author (
AuthorID INT PRIMARY KEY,
Name VARCHAR(100),
Birthdate DATE,
Nationality VARCHAR(50)
);

CREATE TABLE Publisher (


PublisherID INT PRIMARY KEY,
Name VARCHAR(100),
Location VARCHAR(100)
);

CREATE TABLE Book (


BookID INT PRIMARY KEY,
Title VARCHAR(200),
AuthorID INT,
Price DECIMAL(10, 2),
Genre VARCHAR(50),
PublisherID INT,
FOREIGN KEY (AuthorID) REFERENCES Author(AuthorID),
FOREIGN KEY (PublisherID) REFERENCES Publisher(PublisherID)
);

CREATE TABLE Customer (


CustomerID INT PRIMARY KEY,
Name VARCHAR(100),
Email VARCHAR(100),
Phone VARCHAR(20)
);

CREATE TABLE Orders (


OrderID INT PRIMARY KEY,
OrderDate DATE,
CustomerID INT,
FOREIGN KEY (CustomerID) REFERENCES Customer(CustomerID)
);

CREATE TABLE OrderItem (


OrderItemID INT PRIMARY KEY,
OrderID INT,
BookID INT,
Quantity INT,
FOREIGN KEY (OrderID) REFERENCES Orders(OrderID),
FOREIGN KEY (BookID) REFERENCES Book(BookID)
);

4. SQL Queries for CRUD Operations

 Create (Insert Data):

sql
Copy code
-- Insert an author
INSERT INTO Author (AuthorID, Name, Birthdate, Nationality)
VALUES (1, 'J.K. Rowling', '1965-07-31', 'British');

-- Insert a publisher
INSERT INTO Publisher (PublisherID, Name, Location)
VALUES (1, 'Bloomsbury Publishing', 'London, UK');

-- Insert a book
INSERT INTO Book (BookID, Title, AuthorID, Price, Genre, PublisherID)
VALUES (1, 'Harry Potter and the Philosopher''s Stone', 1, 25.00, 'Fantasy',
1);

-- Insert a customer
INSERT INTO Customer (CustomerID, Name, Email, Phone)
VALUES (1, 'John Doe', '[email protected]', '+1234567890');

-- Insert an order
INSERT INTO Orders (OrderID, OrderDate, CustomerID)
VALUES (1, '2024-07-06', 1);

-- Insert order items


INSERT INTO OrderItem (OrderItemID, OrderID, BookID, Quantity)
VALUES (1, 1, 1, 2);

 Read (Retrieve Data):

sql
Copy code
-- Retrieve all books
SELECT * FROM Book;

-- Retrieve orders placed by a specific customer


SELECT * FROM Orders WHERE CustomerID = 1;

-- Retrieve details of a specific order including items


SELECT Orders.OrderID, Orders.OrderDate, OrderItem.Quantity, Book.Title
FROM Orders
JOIN OrderItem ON Orders.OrderID = OrderItem.OrderID
JOIN Book ON OrderItem.BookID = Book.BookID
WHERE Orders.OrderID = 1;

 Update (Modify Data):

sql
Copy code
-- Update book price
UPDATE Book SET Price = 29.99 WHERE BookID = 1;

-- Update customer phone number


UPDATE Customer SET Phone = '+1987654321' WHERE CustomerID = 1;

 Delete (Remove Data):

sql
Copy code
-- Delete an order
DELETE FROM Orders WHERE OrderID = 1;

-- Delete a book
DELETE FROM Book WHERE BookID = 1;

5. Conclusion
Designing and implementing a relational database involves careful planning, starting from
conceptualization with ER diagrams to actual implementation with SQL queries. A well-
designed database like the one outlined here can efficiently manage data for a small business,
ensuring reliability, scalability, and ease of maintenance. Understanding these principles is
crucial for anyone involved in software development, data management, or business operations.

You might also like