0% found this document useful (0 votes)
26 views14 pages

DBMS LAB

DBMS LAB

Uploaded by

Krish Bhardwaj
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
0% found this document useful (0 votes)
26 views14 pages

DBMS LAB

DBMS LAB

Uploaded by

Krish Bhardwaj
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
You are on page 1/ 14

DBMS LAB 1

Krish Bhardwaj ( 2023sl70033 )


Ans : DBMS_LAB_1_SCRIPT.sql

-- LAB 1

-- Create schema
CREATE SCHEMA LAB_1;

-- Select schema
USE lAB_1;

-- Create Authors Table


CREATE TABLE Authors (
AuthorID INT PRIMARY KEY AUTO_INCREMENT,
FirstName VARCHAR(50),
LastName VARCHAR(50),
Biography TEXT
);

-- Create Categories Table


CREATE TABLE Categories (
CategoryID INT PRIMARY KEY AUTO_INCREMENT,
CategoryName VARCHAR(50)
);

-- Create Books Table


CREATE TABLE Books (
BookID INT PRIMARY KEY AUTO_INCREMENT,
Title VARCHAR(100),
AuthorID INT,
CategoryID INT,
PublicationYear YEAR,
ISBN VARCHAR(20),
CopiesAvailable INT,
FOREIGN KEY (AuthorID) REFERENCES Authors(AuthorID),
FOREIGN KEY (CategoryID) REFERENCES Categories(CategoryID)
);

-- Create Members Table


CREATE TABLE Members (
MemberID INT PRIMARY KEY AUTO_INCREMENT,
FirstName VARCHAR(50),
LastName VARCHAR(50),
Email VARCHAR(100),
PhoneNumber VARCHAR(15),
MembershipDate DATE
);

Krish Bhardwaj ( 2023sl70033 )


pg. 1
-- Create Borrower Table
CREATE TABLE Borrower (
LoanID INT PRIMARY KEY AUTO_INCREMENT,
BookID INT,
MemberID INT,
LoanDate DATE,
ReturnDate DATE,
DueDate DATE,
FOREIGN KEY (BookID) REFERENCES Books(BookID),
FOREIGN KEY (MemberID) REFERENCES Members(MemberID)
);

-- Insert sample data


INSERT INTO Authors (FirstName, LastName, Biography)
VALUES
('Stephen', 'King', 'American author of horror'),
('Agatha', 'Christie', 'British author known for her detective novels'),
('J.K.', 'Rowling', 'British author, famous for the Harry Potter series'),
('George', 'Orwell', 'English novelist, best known for "1984"'),
('J.R.R.', 'Tolkien', 'English writer, best known for "The Lord of the
Rings"');

INSERT INTO Categories (CategoryName)


VALUES
('Horror'),
('Detective'),
('Fantasy'),
('Science Fiction'),
('Classic');

INSERT INTO Books (Title, AuthorID, CategoryID, PublicationYear, ISBN,


CopiesAvailable)
VALUES
('It', 1, 1, 1986, '978-0450411434', 5),
('Murder on the Orient Express', 2, 2, 1934, '978-0062693662', 3),
('Harry Potter and the Sorcerer\'s Stone', 3, 3, 1997, '978-0590353427', 10),
('1984', 4, 5, 1949, '978-0451524935', 4),
('The Hobbit', 5, 3, 1937, '978-0547928227', 2);

INSERT INTO Members (FirstName, LastName, Email, PhoneNumber, MembershipDate)


VALUES
('Alice', 'Johnson', '[email protected]', '555-1234', '2023-05-15'),
('Bob', 'Smith', '[email protected]', '555-5678', '2024-01-10'),
('Charlie', 'Davis', '[email protected]', '555-9876', '2022-11-20'),
('Diana', 'Miller', '[email protected]', '555-2468', '2024-03-01'),
('Eva', 'Wilson', '[email protected]', '555-1357', '2023-09-25');

INSERT INTO Borrower (BookID, MemberID, LoanDate, ReturnDate, DueDate)


VALUES
(1, 1, '2024-01-15', '2024-02-15', '2024-02-10'),
Krish Bhardwaj ( 2023sl70033 )
pg. 2
(2, 2, '2024-01-20', '2024-02-20', '2024-02-15'),
(3, 3, '2024-01-05', '2024-02-05', '2024-02-01'),
(4, 4, '2024-03-02', '2024-04-02', '2024-03-30'),
(5, 5, '2023-10-25', '2023-11-25', '2023-11-20');
-- Questions

-- 1. Add a new author named "Agatha Christie" with the biography "British
author known for her detective novels." to the Authors table in the Library
Management System database
INSERT INTO Authors (FirstName, LastName, Biography)
VALUES ('Agatha', 'Christie', 'British author known for her detective
novels.');

-- 2. Update the biography of "Stephen King" to "American author of horror"


UPDATE Authors
SET Biography = 'American author of horror'
WHERE LastName = 'King' AND FirstName = 'Stephen';

-- 3. Rename the category "Classics" to "Comedy” in the Categories table


UPDATE Categories
SET CategoryName = 'Comedy'
WHERE CategoryName = 'Classics';

-- 4. Delete the book "Animal Farm" from the Books table


DELETE FROM Books
WHERE Title = 'Animal Farm';

-- 5. Add a new column called "Address" to the Members table?


ALTER TABLE Members
ADD COLUMN Address VARCHAR(255);

-- 6. Write a query to retrieve all members who joined after January 1, 2024.
SELECT * FROM Members
WHERE MembershipDate > '2024-01-01';

-- 7. Write a query to Add a CoverImage column to the Books table to store the
URL of the book cover image
ALTER TABLE Books
ADD COLUMN CoverImage VARCHAR(255);

-- 8. Write a query to Add a unique constraint to the email column in the


Members table to ensure no two members can have the same email address.
ALTER TABLE Members
ADD CONSTRAINT unique_email UNIQUE (Email);

-- 9. Write a query to Add a composite key on BookID and MemberID in the


Borrower table to ensure that a member cannot borrow the same book more than
once at the same time.
ALTER TABLE Borrower
ADD CONSTRAINT unique_book_member UNIQUE (BookID, MemberID);

Krish Bhardwaj ( 2023sl70033 )


pg. 3
-- 10. Write a query to Add a check constraint to ensure that CopiesAvailable
in the Books table cannot be negative.
ALTER TABLE Books
ADD CONSTRAINT check_copies CHECK (CopiesAvailable >= 0);

-- 11. Write a query to Rename the FirstName column to MemberFirstName in the


Members table for clarity.
ALTER TABLE Members
CHANGE COLUMN FirstName MemberFirstName VARCHAR(50);

-- 12. Write a query to Add a foreign key constraint to the Borrower table that
ensures the MemberID must refer to an existing MemberID in the Members table.
ALTER TABLE Borrower
ADD CONSTRAINT fk_member FOREIGN KEY (MemberID) REFERENCES Members(MemberID);

-- 13. Write a query to Find all books published between 1990 and 2000.
SELECT * FROM Books
WHERE PublicationYear BETWEEN 1990 AND 2000;

-- 14. Write a query to Find all authors whose last names start with the letter
"S".
SELECT * FROM Authors
WHERE LastName LIKE 'S%';

-- 15. Write a query to return the current date, time, and difference in days
between two date values.
SELECT CURDATE() AS CurrentDate, NOW() AS CurrentDateTime,
DATEDIFF('2024-12-01', '2024-01-01') AS DaysDifference;

Krish Bhardwaj ( 2023sl70033 )


pg. 4
Outputs :

Krish Bhardwaj ( 2023sl70033 )


pg. 5
DBMS LAB 2
Krish Bhardwaj ( 2023sl70033 )
Ans : DBMS_LAB_2_SCRIPT.sql

-- LAB 1

-- Create schema
CREATE SCHEMA LAB_1;

-- Select schema
USE lAB_1;

-- Create Authors Table


CREATE TABLE Authors (
AuthorID INT PRIMARY KEY AUTO_INCREMENT,
FirstName VARCHAR(50),
LastName VARCHAR(50),
Biography TEXT
);

-- Create Categories Table


CREATE TABLE Categories (
CategoryID INT PRIMARY KEY AUTO_INCREMENT,
CategoryName VARCHAR(50)
);

-- Create Books Table


CREATE TABLE Books (
BookID INT PRIMARY KEY AUTO_INCREMENT,
Title VARCHAR(100),
AuthorID INT,
CategoryID INT,
PublicationYear YEAR,
ISBN VARCHAR(20),
CopiesAvailable INT,
FOREIGN KEY (AuthorID) REFERENCES Authors(AuthorID),
FOREIGN KEY (CategoryID) REFERENCES Categories(CategoryID)
);

-- Create Members Table


CREATE TABLE Members (
MemberID INT PRIMARY KEY AUTO_INCREMENT,
FirstName VARCHAR(50),
LastName VARCHAR(50),
Email VARCHAR(100),
PhoneNumber VARCHAR(15),
MembershipDate DATE
);

Krish Bhardwaj ( 2023sl70033 )


pg. 1
-- Create Borrower Table
CREATE TABLE Borrower (
LoanID INT PRIMARY KEY AUTO_INCREMENT,
BookID INT,
MemberID INT,
LoanDate DATE,
ReturnDate DATE,
DueDate DATE,
FOREIGN KEY (BookID) REFERENCES Books(BookID),
FOREIGN KEY (MemberID) REFERENCES Members(MemberID)
);

-- Insert sample data


INSERT INTO Authors (FirstName, LastName, Biography)
VALUES
('Stephen', 'King', 'American author of horror'),
('Agatha', 'Christie', 'British author known for her detective novels'),
('J.K.', 'Rowling', 'British author, famous for the Harry Potter series'),
('George', 'Orwell', 'English novelist, best known for "1984"'),
('J.R.R.', 'Tolkien', 'English writer, best known for "The Lord of the
Rings"');

INSERT INTO Categories (CategoryName)


VALUES
('Horror'),
('Detective'),
('Fantasy'),
('Science Fiction'),
('Classic');

INSERT INTO Books (Title, AuthorID, CategoryID, PublicationYear, ISBN,


CopiesAvailable)
VALUES
('It', 1, 1, 1986, '978-0450411434', 5),
('Murder on the Orient Express', 2, 2, 1934, '978-0062693662', 3),
('Harry Potter and the Sorcerer\'s Stone', 3, 3, 1997, '978-0590353427', 10),
('1984', 4, 5, 1949, '978-0451524935', 4),
('The Hobbit', 5, 3, 1937, '978-0547928227', 2);

INSERT INTO Members (FirstName, LastName, Email, PhoneNumber, MembershipDate)


VALUES
('Alice', 'Johnson', '[email protected]', '555-1234', '2023-05-15'),
('Bob', 'Smith', '[email protected]', '555-5678', '2024-01-10'),
('Charlie', 'Davis', '[email protected]', '555-9876', '2022-11-20'),
('Diana', 'Miller', '[email protected]', '555-2468', '2024-03-01'),
('Eva', 'Wilson', '[email protected]', '555-1357', '2023-09-25');

INSERT INTO Borrower (BookID, MemberID, LoanDate, ReturnDate, DueDate)


VALUES
(1, 1, '2024-01-15', '2024-02-15', '2024-02-10'),
Krish Bhardwaj ( 2023sl70033 )
pg. 2
(2, 2, '2024-01-20', '2024-02-20', '2024-02-15'),
(3, 3, '2024-01-05', '2024-02-05', '2024-02-01'),
(4, 4, '2024-03-02', '2024-04-02', '2024-03-30'),
(5, 5, '2023-10-25', '2023-11-25', '2023-11-20');
-- Questions

-- 1. Write a query to calculate number of authors present in the Authors


table?

SELECT COUNT(*) AS NumberOfAuthors FROM Authors;

-- 2. Use aggregate functions to analyze book data.


SELECT AVG(CopiesAvailable) AS AverageCopiesAvailable
FROM Books;

-- 3. Write a query to calculate minimum publication year of books in the Books


table?
SELECT MIN(PublicationYear) AS OldestPublicationYear
FROM Books;

Krish Bhardwaj ( 2023sl70033 )


pg. 3
-- 4. Retrieve a list of all books along with their authors' names.
SELECT
Books.Title AS BookTitle,
CONCAT(Authors.FirstName, ' ', Authors.LastName) AS AuthorName
FROM
Books
INNER JOIN
Authors ON Books.AuthorID = Authors.AuthorID;

-- 5. Write a query to get a list of all members and the books they have
borrowed.
SELECT
Members.FirstName AS MemberFirstName,
Members.LastName AS MemberLastName,
Books.Title AS BorrowedBookTitle
FROM
Members
LEFT JOIN
Borrower ON Members.MemberID = Borrower.MemberID
LEFT JOIN
Books ON Borrower.BookID = Books.BookID;

Krish Bhardwaj ( 2023sl70033 )


pg. 4
-- 6. Retrieve a list of all authors and the total number of books they have
SELECT
CONCAT(Authors.FirstName, ' ', Authors.LastName) AS AuthorName,
COUNT(Books.BookID) AS TotalBooksWritten
FROM
Authors
LEFT JOIN
Books ON Authors.AuthorID = Books.AuthorID
GROUP BY
Authors.AuthorID;

-- 7. List all books along with their author’s full name and publication year.
SELECT
Books.Title AS BookTitle,
CONCAT(Authors.FirstName, ' ', Authors.LastName) AS AuthorName,
Books.PublicationYear
FROM
Books
INNER JOIN
Authors ON Books.AuthorID = Authors.AuthorID;

Krish Bhardwaj ( 2023sl70033 )


pg. 5
DBMS LAB 3
Krish Bhardwaj ( 2023sl70033 )
Ans : DBMS_LAB_3_SCRIPT.sql

-- LAB 3

-- Create schema
CREATE SCHEMA LAB_3;

-- Select schema
USE lAB_3;

-- Step 1: Create the Database Schema

-- Create the Accounts table


CREATE TABLE Accounts (
AccountID INT AUTO_INCREMENT PRIMARY KEY,
AccountNumber VARCHAR(20),
AccountHolder VARCHAR(50),
Balance DECIMAL(10, 2)
);

-- Create the Transactions table


CREATE TABLE Transactions (
TransactionID INT AUTO_INCREMENT PRIMARY KEY,
AccountID INT,
TransactionDate DATE,
TransactionAmount DECIMAL(10, 2),
TransactionType VARCHAR(10), -- 'Debit' or 'Credit'
FOREIGN KEY (AccountID) REFERENCES Accounts(AccountID)
);

-- Create the Overdrafts table


CREATE TABLE Overdrafts (
OverdraftID INT AUTO_INCREMENT PRIMARY KEY,
AccountID INT,
OverdraftDate DATE,
OverdraftAmount DECIMAL(10, 2),
FOREIGN KEY (AccountID) REFERENCES Accounts(AccountID)
);

-- Insert sample data into Accounts


INSERT INTO Accounts (AccountNumber, AccountHolder, Balance) VALUES
('ACC001', 'John Doe', 5000.00),
('ACC002', 'Jane Smith', 3000.00),
('ACC003', 'Alice Johnson', 7000.00),
('ACC004', 'Bob Brown', 2000.00);

Krish Bhardwaj ( 2023sl70033 )


pg. 1
-- Insert sample data into Overdrafts
INSERT INTO Overdrafts (AccountID, OverdraftDate, OverdraftAmount) VALUES
(1, '2024-12-01', 1000.00),
(2, '2024-12-02', 500.00),
(3, '2024-12-03', 1500.00),
(4, '2024-12-04', 2000.00);

-- Step 2: Create a Stored Procedure to Transfer Amounts Between Accounts


DELIMITER $$

CREATE PROCEDURE TransferAmount(


IN SourceAccountID INT,
IN TargetAccountID INT,
IN TransferAmount DECIMAL(10, 2)
)
BEGIN
DECLARE CurrentBalance DECIMAL(10, 2);

-- Check the source account's balance


SELECT Balance INTO CurrentBalance FROM Accounts WHERE AccountID =
SourceAccountID;

IF CurrentBalance >= TransferAmount THEN


-- Start the transaction
START TRANSACTION;

-- Deduct the amount from the source account


UPDATE Accounts SET Balance = Balance - TransferAmount WHERE AccountID
= SourceAccountID;

-- Add the amount to the target account


UPDATE Accounts SET Balance = Balance + TransferAmount WHERE AccountID
= TargetAccountID;

-- Insert the transaction records


INSERT INTO Transactions (AccountID, TransactionDate,
TransactionAmount, TransactionType)
VALUES (SourceAccountID, CURDATE(), TransferAmount, 'Debit'),
(TargetAccountID, CURDATE(), TransferAmount, 'Credit');

-- Commit the transaction


COMMIT;

SELECT 'Transfer Successful' AS Message;


ELSE
-- Rollback the transaction if insufficient funds
ROLLBACK;
SELECT 'Insufficient Balance in Source Account' AS Message;
END IF;
END$$

DELIMITER ;
Krish Bhardwaj ( 2023sl70033 )
pg. 2
-- Step 3: Create a Trigger to Automatically Update Balance
DELIMITER $$

CREATE TRIGGER UpdateBalanceAfterTransaction


AFTER INSERT ON Transactions
FOR EACH ROW
BEGIN
IF NEW.TransactionType = 'Debit' THEN
UPDATE Accounts SET Balance = Balance - NEW.TransactionAmount WHERE
AccountID = NEW.AccountID;
ELSEIF NEW.TransactionType = 'Credit' THEN
UPDATE Accounts SET Balance = Balance + NEW.TransactionAmount WHERE
AccountID = NEW.AccountID;
END IF;
END$$

DELIMITER ;

-- Step 4: Create a Cursor to Display Transactions with Amount > 100


DELIMITER $$

CREATE PROCEDURE DisplayLargeTransactions()


BEGIN
DECLARE Done INT DEFAULT FALSE;
DECLARE TransactionID INT;
DECLARE AccountID INT;
DECLARE TransactionDate DATE;
DECLARE TransactionAmount DECIMAL(10, 2);
DECLARE TransactionType VARCHAR(10);
DECLARE TransactionCursor CURSOR FOR
SELECT TransactionID, AccountID, TransactionDate, TransactionAmount,
TransactionType
FROM Transactions
WHERE TransactionAmount > 100;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET Done = TRUE;

OPEN TransactionCursor;

TransactionLoop: LOOP
FETCH TransactionCursor INTO TransactionID, AccountID, TransactionDate,
TransactionAmount, TransactionType;
IF Done THEN
LEAVE TransactionLoop;
END IF;
-- Display each transaction
SELECT TransactionID, AccountID, TransactionDate, TransactionAmount,
TransactionType;
END LOOP;
CLOSE TransactionCursor;
END$$
DELIMITER ;
Krish Bhardwaj ( 2023sl70033 )
pg. 3
-- Test the Stored Procedure
CALL TransferAmount(1, 2, 1000.00);

-- Test the Cursor


CALL DisplayLargeTransactions();

Krish Bhardwaj ( 2023sl70033 )


pg. 4

You might also like