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

database lab manual

a lab manual about database management and administration subject.

Uploaded by

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

database lab manual

a lab manual about database management and administration subject.

Uploaded by

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

LAB MANUAL

For
Database Administration and Management

Department Of Information Technology


The Government College Women University Faisalabad
Sr Practical
No.
1 Find A Better Software
2 Installation Of Software
3 Database Design
3.1 Design a database schema for a simple library management system.
3.2 Create a database schema for a university management system.
3.3 Design a database schema for an e-commerce website.
4 SQL Queries
4.1 Write SQL queries to retrieve data from a given database schema.
4.2 Practice using SQL aggregate functions (e.g., SUM, AVG, MAX, MIN).
4.3 Write SQL queries to perform CRUD (Create, Read, Update, Delete) operations.
5 Data Modeling
5.1 Create an Entity-Relationship (ER) diagram for a given database schema.
Practical 1
Find A Better Software
Our group is collaborating to evaluate and select the most suitable software
for conducting database practical and exercises efficiently.

Here are six different database practical tools you can use for hands-on exercises:

1. MySQL Workbench – A popular tool for database design, query building,


and server administration, ideal for practicing SQL and database management
tasks.

2. SQL Server Management Studio (SSMS) – A comprehensive tool for


managing Microsoft SQL Server databases, allowing for query writing,
database design, and performance monitoring.
3. pgAdmin – An open-source administration and development platform for
PostgreSQL, allowing users to practice querying and database management.

4. Oracle SQL Developer – A free integrated development environment for


working with Oracle databases, offering SQL query capabilities and database
management features.

5. DB Visualizer – A universal database management tool that supports a wide


range of databases, including MySQL, PostgreSQL, Oracle, and SQL Server,
for querying and visualization tasks.
6. MongoDB Compass – A GUI tool for MongoDB, perfect for practicing
NoSQL database management, queries, and visualizing data structures.

Selected Software

Our group decided to select MySQL Workbench over other software options
because of its comprehensive tools, ease of use, and ability to streamline database
design and management.
Practical 2
Installation Of Software

1. Download MySQL Workbench

• Go to the official MySQL website: MySQL Workbench Downloads.


• Under the MySQL Workbench section, select Windows as your operating
system.
• Click on the Download button, and you'll be redirected to a page with various
download options.
• Choose the Windows (x86, 32-bit), MSI Installer option for most systems
(or 64-bit if you are on a 64-bit system).
• You may be asked to log in or create an Oracle account, but you can also click
No thanks, just start my download to proceed without logging in.

2. Run the Installer

• Once the download is complete, locate the .msi installer file in your
downloads folder and double-click to run it.
• If prompted by Windows User Account Control, click Yes to allow the
installation.

3. Choose Setup Type

• The MySQL installer will launch and ask for the setup type. You can select
from:
o Developer Default (recommended, as it installs MySQL Server,
MySQL Workbench, MySQL Shell, and other components).
o Server Only, Client Only, or Full depending on what you need.
• For most users, Developer Default is the best option because it includes
MySQL Server, MySQL Workbench, and other useful tools.
• Click Next.

4. Check Prerequisites

• The installer will check if your system has the required software (such as
Visual Studio Tools and Microsoft Redistributables). If any are missing, the
installer will offer to download and install them.
• Ensure that everything is checked off before proceeding. Click Next.

5. Install MySQL Workbench

• The installer will list the products to be installed. Make sure MySQL
Workbench is selected, and then click Execute to begin the installation.
• Wait while the installer downloads and installs the necessary components.

6. Configuration

• After the installation is complete, the MySQL installer will prompt you to
configure MySQL Server. If you don't already have MySQL Server installed,
you can install it now using the installer, or connect to an existing server.
• Set a root password for MySQL Server (ensure it’s a strong password).
• You can choose the default settings or customize the installation path and
other configurations if desired.

7. Complete Installation

• Once the configuration is complete, click Next to finish the installation.


• You may be asked to start MySQL Workbench and MySQL Server
automatically. Click Finish.

8. Launch MySQL Workbench

• Open the Start Menu and search for MySQL Workbench.


• Click on MySQL Workbench to launch the application.

9. Configure a MySQL Connection

• When MySQL Workbench opens for the first time, you need to configure a
connection to a MySQL server.
• Click the + icon next to MySQL Connections.
• Enter a Connection Name, set Hostname to localhost (or the IP of your
server), and input the root username and the password you set earlier.
• Click Test Connection to ensure everything is working. If successful, click
OK to save the connection.

10. Verify Installation

• Now you can start managing your MySQL databases via MySQL Workbench.
You can run SQL queries, manage databases, and perform other tasks.
Practical 3
Database Design

3.1 Design a database schema for a simple library management


system.

Step 1: Create database

CREATE DATABASE Librarymanagementsystem_db;

Step 2: Use database

USE Librarymanagementsystem_db;

Step 3: Create Table for Books, Members, Staff, Publishers and Transactions Details.

CREATE TABLE Books (BookID INT PRIMARY KEY, Title VARCHAR (255) NOT NULL,
Author VARCHAR (255), Publisher VARCHAR (100),YearPublished YEAR, Quantity INT
NOT NULL);

CREATE TABLE Members (MemberID INT PRIMARY KEY, Name VARCHAR (255) NOT
NULL, ContactNumber VARCHAR (15),MembershipDate DATE NOT NULL);

CREATE TABLE Transactions (TransactionID INT PRIMARY KEY,MemberID INT NOT


NULL,BookID INT NOT NULL,IssueDate DATE NOT NULL,ReturnDate DATE, FOREIGN
KEY (MemberID) REFERENCES Members (MemberID), FOREIGN KEY (BookID)
REFERENCES Books (BookID));

CREATE TABLE Staff (Staff_ID INT AUTO_INCREMENT PRIMARY KEY, Name


VARCHAR (255) NOT NULL, Position VARCHAR (100), Salary DECIMAL (10,
2),Shift_Timings VARCHAR (50));

CREATE TABLE Publisher (Publisher_ID INT PRIMARY KEY, Name VARCHAR (255)
NOT NULL, Address TEXT,Contact_Number VARCHAR (15));
Step 4: Inserting Values into All Tables.

INSERT INTO Books (BookID, Title, Author, Publisher,YearPublished,Quantity)

VALUES (1, 'Al-Qur'an', 'Multiple Authors', 'Islamic Publishers',1992,610), (2, 'Hadith of the
Prophet', 'Imam Bukhari', 'Darussalam',2010,35), (3, 'The Life of Muhammad', 'Muhammad
Husayn Haykal', 'Islamic Book Trust',1960,50);

INSERT INTO Members (MemberID,Name,ContactNumber,MembershipDate)

VALUES(1,'Ali','1234567890','2023-01-10'),(2, 'Fatima','9876543210','2023-03-12'),(3,
'Omar','5555555555', '2023-05-15'); INSERT INTO Transactions (TransactionID, MemberID,
BookID, IssueDate, ReturnDate) VALUES(1, 1, 1, '2023-09-01', '2023-09-15'), (2, 2, 2, '2023-
10-01', '2023-10-15'),(3, 3, 3, '2023-11-10', '2023-11-24');

INSERT INTO Staff (Staff_ID, Name, Position, Salary, Shift_Timings)

VALUES (1, 'Zainab Ali', 'Librarian', 3000.00, 'Morning'),(2, 'Khalid Ahmed', 'Assistant',2500.00,
'Evening'),(3, 'Sara Tariq', 'Admin', 3500.00, 'Full-time');

INSERT INTO Publisher (Publisher_ID, Name, Address, Contact_Number)

VALUES (1, 'Islamic Publishers', 'Al-Nour St, Riyadh, KSA', '+966-11-2345678'),(2, 'Darussalam',
'Makkah, Saudi Arabia', '+966-12-1234567'), (3, 'Islamic Book Trust', 'Karachi, Pakistan', '+92-
21-5678901');

Step 5: For Results.

SELECT * FROM Members;

SELECT * FROM Books;

SELECT * FROM Staff;

SELECT * FROM Publisher;

SELECT * FROM Transactions;

Here is a View of Database


3.2 Create a database schema for a university management system.

Step 1: Create Database

CREATE DATABASE UniversityManagement;

Step 2: Use Database.

USE UniversityManagement;

Step 3: Create Tables of Departments, Students, Faculty, Courses, Classes,


Enrollment, Attendance, Grades.
CREATE TABLE Departments (department_id INT PRIMARY
KEY,department_name VARCHAR(100) NOT NULL,building
VARCHAR(100));

CREATE TABLE Students (student_id INT PRIMARY KEY,Name


VARCHAR(50) NOT NULL,dob DATE NOT NULL,phone_number
VARCHAR(15),department_id INT,FOREIGN KEY (department_id)
REFERENCES Departments(department_id));

CREATE TABLE Faculty (faculty_id INT PRIMARY KEY,Name


VARCHAR(50) NOT NULL,phone_number VARCHAR(15),position
VARCHAR(50),department_id INT,FOREIGN KEY (department_id)
REFERENCES Departments(department_id));

CREATE TABLE Courses (course_id INT PRIMARY KEY,course_name


VARCHAR(100) NOT NULL,credit_hours INT NOT NULL,department_id
INT,FOREIGN KEY (department_id) REFERENCES
Departments(department_id));

CREATE TABLE Classes (class_id INT PRIMARY KEY,course_id INT NOT


NULL,faculty_id INT NOT NULL,schedule VARCHAR(100),room_number
VARCHAR(10),semester VARCHAR(10),year YEAR,FOREIGN KEY
(course_id) REFERENCES Courses(course_id),FOREIGN KEY (faculty_id)
REFERENCES Faculty(faculty_id));

CREATE TABLE Enrollment (enrollment_id INT PRIMARY KEY,student_id


INT NOT NULL,class_id INT NOT NULL,enrollment_date DATE,FOREIGN
KEY (student_id) REFERENCES Students(student_id),FOREIGN KEY (class_id)
REFERENCES Classes(class_id));
CREATE TABLE Attendance (attendance_id INT PRIMARY KEY,student_id
INT NOT NULL,class_id INT NOT NULL,attendance_date DATE NOT
NULL,status ENUM('Present', 'Absent') NOT NULL,FOREIGN KEY (student_id)
REFERENCES Students(student_id),FOREIGN KEY (class_id) REFERENCES
Classes(class_id));

CREATE TABLE Grades (grade_id INT PRIMARY KEY,student_id INT NOT


NULL,class_id INT NOT NULL,grade CHAR(1) CHECK (grade IN ('A', 'B', 'C',
'D', 'F')),FOREIGN KEY (student_id) REFERENCES
Students(student_id),FOREIGN KEY (class_id) REFERENCES
Classes(class_id));

Step 4: Inserting Values into Tables.

INSERT INTO Departments (department_id, department_name,


building)VALUES (1,'Computer Science', 'Engineering Block
A'),(2,'Mathematics', 'Science Block B'),(3,'Physics', 'Science Block C');

INSERT INTO Students (student_id,Name, dob,


phone_number,department_id)VALUES (1,'Ahmed', '2000-01-15', '1234567890',
1),(2,'Yasir', '2001-08-10', '1122334455', 2),(3,'Ali', '1999-05-20', '0987654321', 3);

INSERT INTO Faculty (faculty_id,Name,phone_number, position,


department_id)VALUES (1,'Dr. Zain', '9876543210', 'Professor',
1),(2,'Hassan','8765432109', 'Lecturer', 2),(3,'Sheikh', '7654321098', 'Professor', 3);

INSERT INTO Courses (course_id,course_name, credit_hours,


department_id)VALUES (102,'Data Structures', 3, 1),(201,'Linear Algebra', 4,
2),(211,'Quantum Mechanics', 3, 3);
INSERT INTO Classes (class_id,course_id, faculty_id, schedule, room_number,
semester, year)VALUES (1,102, 1, 'Monday 10:00 AM - 12:00 PM', 'Room 101',
'Fall', 2024),(2,201, 2, 'Tuesday 2:00 PM - 4:00 PM', 'Room 202', 'Fall',
2024),(3,211, 3, 'Wednesday 9:00 AM - 11:00 AM', 'Room 303', 'Fall', 2024);

INSERT INTO Enrollment (enrollment_id,student_id, class_id,


enrollment_date)VALUES (1,1, 1, '2024-08-15'),(2,2, 2, '2024-08-16'),(3,3, 3,
'2024-08-17');

INSERT INTO Attendance (attendance_id,student_id, class_id, attendance_date,


status)VALUES (1,1, 1, '2024-09-01', 'Present'),(2,2, 2, '2024-09-01',
'Absent'),(3,3, 3, '2024-09-01', 'Present');

INSERT INTO Grades (grade_id,student_id, class_id, grade)VALUES (1,1, 1,


'A'),(2,2, 2, 'B'),(3,3, 3, 'A');

Step 5: Display Tables.

SELECT * FROM Departments;

SELECT * FROM Students;

SELECT * FROM Faculty;

SELECT * FROM Courses;

SELECT * FROM Classes;

SELECT * FROM Enrollment;

SELECT * FROM Attendance;

SELECT * FROM Grades;


Here is a view of Database
3.3 Design a database schema for an e-commerce website.

Step 1: Create Database.

CREATE DATABASE ECommerceDB;

Step 2: Use database.

USE ECommerceDB;

Step 3: Create Tables of Categories, Products, Users, Orders, Order_Items,


Shopping_Cart, Reviews.

CREATE TABLE Users (user_id INT PRIMARY KEY,Name


VARCHAR(50),email VARCHAR(100) UNIQUE NOT NULL,password_hash
VARCHAR(255) NOT NULL,phone_number VARCHAR(15),address
TEXT,user_role ENUM('customer', 'admin') DEFAULT 'customer',created_at
TIMESTAMP DEFAULT CURRENT_TIMESTAMP);

CREATE TABLE Categories (category_id INT PRIMARY KEY,category_name


VARCHAR(100) NOT NULL,description TEXT);

CREATE TABLE Products (product_id INT PRIMARY KEY,product_name


VARCHAR(255) NOT NULL,description TEXT,price DECIMAL(10, 2) NOT
NULL,stock_quantity INT NOT NULL,category_id INT,image_url
VARCHAR(255),created_at TIMESTAMP DEFAULT
CURRENT_TIMESTAMP,FOREIGN KEY (category_id) REFERENCES
Categories(category_id));

CREATE TABLE Orders (order_id INT AUTO_INCREMENT PRIMARY


KEY,user_id INT NOT NULL,order_status ENUM('pending', 'shipped',
'delivered', 'cancelled') DEFAULT 'pending',total_amount DECIMAL(10, 2) NOT
NULL,order_date TIMESTAMP DEFAULT
CURRENT_TIMESTAMP,shipping_address TEXT,payment_status
ENUM('unpaid', 'paid') DEFAULT 'unpaid',FOREIGN KEY (user_id)
REFERENCES Users(user_id));

CREATE TABLE Order_Items (order_item_id INT PRIMARY KEY,order_id


INT NOT NULL,product_id INT NOT NULL,quantity INT NOT NULL,price
DECIMAL(10, 2) NOT NULL,FOREIGN KEY (order_id) REFERENCES
Orders(order_id),FOREIGN KEY (product_id) REFERENCES
Products(product_id));

CREATE TABLE Shopping_Cart (cart_id INT AUTO_INCREMENT PRIMARY


KEY,user_id INT NOT NULL,product_id INT NOT NULL,quantity INT NOT
NULL,added_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,FOREIGN
KEY (user_id) REFERENCES Users(user_id),FOREIGN KEY (product_id)
REFERENCES Products(product_id));

CREATE TABLE Reviews (review_id INT AUTO_INCREMENT PRIMARY


KEY,user_id INT NOT NULL,product_id INT NOT NULL,rating INT CHECK
(rating >= 1 AND rating <= 5),review_text TEXT,review_date TIMESTAMP
DEFAULT CURRENT_TIMESTAMP,FOREIGN KEY (user_id) REFERENCES
Users(user_id),FOREIGN KEY (product_id) REFERENCES
Products(product_id));

Step 4: Inserting Values into Tables.

INSERT INTO Users (user_id,Name, email, password_hash, phone_number,


address, user_role) VALUES (1, 'Ali', '[email protected]',
'hashedpassword1', '1234567890', '123 Main St, Cityville', 'customer'),( 2,'Zahra',
'[email protected]', 'hashedpassword2', '0987654321', '456 Oak St,
Townsville', 'customer');

INSERT INTO Categories (category_id,category_name, description) VALUES


(1,'Electronics', 'Gadgets and devices such as phones, laptops, etc.'),(2,'Clothing',
'Men and women clothing including shirts, pants, dresses, etc.');

INSERT INTO Products (product_id,product_name, description, price,


stock_quantity, category_id, image_url) VALUES (1,'Laptop', 'High performance
laptop with 16GB RAM and 512GB SSD', 999.99, 50, 1, 'laptop.jpg'),(2,'T-Shirt',
'Comfortable cotton T-shirt in various sizes', 19.99, 200, 2, 'tshirt.jpg');

INSERT INTO Orders (order_id,user_id, order_status, total_amount,


shipping_address, payment_status) VALUES (1,1, 'pending', 1199.98, 'Main St,
Cityville', 'unpaid'),(2,2, 'shipped', 819.99, 'E_block, Townsville', 'paid'); INSERT
INTO Order_Items (order_item_id,order_id, product_id, quantity, price) VALUES
(1,1, 1, 1, 999.99),(2,1, 2, 1, 19.99);

INSERT INTO Shopping_Cart (cart_id,user_id, product_id, quantity) VALUES


(1,1, 1, 1),(2,1, 2, 1); INSERT INTO Reviews (review_id,user_id, product_id,
rating, review_text) VALUES (1,1, 1, 5, 'Great laptop, very fast and reliable!'),(2,2,
2, 4, 'Smartphone is good, but could use better battery life.');

Step 5: Display Tables.

SELECT * FROM Categories;

SELECT * FROM Products;

SELECT * FROM Users;

SELECT * FROM Orders;


SELECT * FROM Order_Items;

SELECT * FROM Shopping_Cart;

SELECT * FROM Reviews;

Here is a view of Database.


Practical 4
SQL Queries
4.1 Write SQL queries to retrieve data from a given database
schema.
Step 1: Connect With Database.

First, we connect to the database using the appropriate credentials and connection
details before executing any SQL queries. We connect with
UniversitymanagementDB.

Step 2: Apply query.

Once connected to the database, we apply the query to retrieve or modify the data
as needed.

Step 3: SELECT Query.

select *from attendance;

select *from classes;

select *from courses;

select *from departments;

select *from enrollment;

select *from faculty;

select *from grades;

select *from students;

select *from attendance;

Here is the result of the query.


4.2 Practice using SQL aggregate functions (e.g., SUM, AVG, MAX, MIN).

Step 1: Connect With Database.

First, we connect to the database using the appropriate credentials and connection
details before executing any SQL queries. We connect with
UniversitymanagementDB.

Step 2: Apply query.

Once connected to the database, we apply the query to retrieve or modify the data
as needed.

Step 3: AVG, MAX, MIN, SUM Query.

select AVG (grade) AS average_grade from grades;

select Max (grade) AS max_grade from grades;

select SUM (grade) AS sum_grade from grades;

select Min (grade) AS min_grade from grades;


Here is the result of the query.

4.3 Write SQL queries to perform CRUD (Create, Read, Update, Delete)
operations.

Step 1: Connect With Database.


First, we connect to the database using the appropriate credentials and connection
details before executing any SQL queries. We connect with
UniversitymanagementDB.

Step 2: Apply query.

Once connected to the database, we apply the query to retrieve or modify the data
as needed.

Step 3: CRUD Operations.

Update grades set grade='A' where grade_id = 1;

create table program (program_id int primary key,program_name varchar(50));


insert into program (program_id,Program_name)values(1,'BS');

delete from grades where grade_id = 2;

select *from grades;

select *from program;

Hers is the result of the query.


Practical 5
Data Modeling
5.1 Create an Entity-Relationship (ER) diagram for a given
database schema.
Step 1: Connect With Database.

First, we connect to the database using the appropriate credentials and connection
details before executing any SQL queries. We connect with
UniversitymanagementDB.

Step 2:

Create a new model or reverse engineer an existing database.

Step 3:

Add tables and define columns.

Step 4:

Define relationships (foreign keys) between tables.

Step 5:

Arrange the diagram for clarity.

Step 6:

Save the model and generate SQL if needed.

Here is the ER Diagram.

You might also like