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

DBMS Lab 06

Design and Populate a Personal Library Database  Objective: Apply the concepts learned in the lab to create a database for managing a personal library, focusing on table creation, data entry, and data types application.  Instructions: 1. Database Creation: Create a new Microsoft Access database named “PersonalLibrary.accdb”. 2. Table Design: Create a table named “Books” with the following fields:  BookID (Primary Key, AutoNumber)  Title (Short Text)  Author (Short Text)  PublishedYear (Num

Uploaded by

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

DBMS Lab 06

Design and Populate a Personal Library Database  Objective: Apply the concepts learned in the lab to create a database for managing a personal library, focusing on table creation, data entry, and data types application.  Instructions: 1. Database Creation: Create a new Microsoft Access database named “PersonalLibrary.accdb”. 2. Table Design: Create a table named “Books” with the following fields:  BookID (Primary Key, AutoNumber)  Title (Short Text)  Author (Short Text)  PublishedYear (Num

Uploaded by

Leema Ram
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

National University of Technology (NUTECH)

Database System Lab (CS4012)

Instructor Name: Madam Saima Yasmeen


Lab Engineer: Madam Tabinda Nasir
Department: Computer Science
Batch: CS 2022
Semester: Spring 2024
Lab no: 06

Name: Leema Ram


ID: F22605013 Due Date: April 6, 2024
IN LAB TASK

Task 1

Design a database system for a university. The university has multiple departments, each
offering several courses. Students enrol in courses offered by these departments. Professors
teach these courses. Each course has multiple assessments (exams, assignments, etc.), and
students receive grades for these assessments. Students may also belong to various clubs and
organizations within the university. Clubs organize events and have members.
• Create table for each entity and create attributes for each table also use appropriate
primary and foreign keys in tables where applicable.
• Enter data of 5 rows for each table using insert into statement
• Define tuple, relation, attributes, cardinality, and degree of a relation.
• Domain of each attribute of each entity
• Primary and foreign keys of a relation

Use [Lab 06];

-- Create Departments Table


CREATE TABLE Departments (
department_id INT PRIMARY KEY,
department_name VARCHAR(255)
);

-- Insert data into Departments Table


INSERT INTO Departments (department_id, department_name) VALUES
(1, 'Computer Science'),
(2, 'Artificial Intelligence'),
(3, 'Computer Engineering'),
(4, 'Electrical Engineering'),
(5, 'Information Technology');

-- Create Courses Table


CREATE TABLE Courses (
course_id INT PRIMARY KEY,
course_name VARCHAR(255),
department_id INT,
FOREIGN KEY (department_id) REFERENCES Departments(department_id)
);

-- Insert data into Courses Table


INSERT INTO Courses (course_id, course_name, department_id) VALUES
(101, 'Programming Fundamentals', 1),
(102, 'Database Systems', 2),
(103, 'Database Systems Lab', 3),
(104, 'Digital Electronics', 4),
(105, 'AI and ML', 5);

-- Create Professors Table


CREATE TABLE Professors (
professor_id INT PRIMARY KEY,
professor_name VARCHAR(255),
department_id INT,
FOREIGN KEY (department_id) REFERENCES Departments(department_id)
);

-- Insert data into Professors Table


INSERT INTO Professors (professor_id, professor_name, department_id)
VALUES
(1, 'Lec. Faria Sajjad', 1),
(2, 'Lec. Saima Yasmeen', 2),
(3, 'Lab eng. Tabinda Nasir', 3),
(4, 'Dr. Mohsin Waseem', 4),
(5, 'Lec. Yosha Jawad', 5);

-- Create Students Table


CREATE TABLE Students (
student_id INT PRIMARY KEY,
student_name VARCHAR(255)
);

-- Insert data into Students Table


INSERT INTO Students (student_id, student_name) VALUES
(1, 'Leema Ram'),
(2, 'Meer Ahmed'),
(3, 'Hammad Ali'),
(4, 'Abdul Rahman'),
(5, 'Khizer Ali');

-- Create Enrollments Table


CREATE TABLE Enrollments (
enrollment_id INT PRIMARY KEY,
student_id INT,
course_id INT,
FOREIGN KEY (student_id) REFERENCES Students(student_id),
FOREIGN KEY (course_id) REFERENCES Courses(course_id)
);

-- Insert data into Enrollments Table


INSERT INTO Enrollments (enrollment_id, student_id, course_id) VALUES
(1, 1, 101),
(2, 2, 102),
(3, 3, 103),
(4, 4, 104),
(5, 5, 105);

-- Create Assessments Table


CREATE TABLE Assessments (
assessment_id INT PRIMARY KEY,
assessment_name VARCHAR(255),
course_id INT,
FOREIGN KEY (course_id) REFERENCES Courses(course_id)
);

-- Insert data into Assessments Table


INSERT INTO Assessments (assessment_id, assessment_name, course_id) VALUES
(1, 'Midterm Exam', 101),
(2, 'Final Exam', 101),
(3, 'Assignments/Lab Assessments', 102),
(4, 'PBL/OEL', 102),
(5, 'Quiz', 103);

-- Create Grades Table


CREATE TABLE Grades (
grade_id INT PRIMARY KEY,
student_id INT,
assessment_id INT,
grade FLOAT,
FOREIGN KEY (student_id) REFERENCES Students(student_id),
FOREIGN KEY (assessment_id) REFERENCES Assessments(assessment_id)
);

-- Insert data into Grades Table


INSERT INTO Grades (grade_id, student_id, assessment_id, grade) VALUES
(1, 1, 1, 85),
(2, 1, 2, 78),
(3, 2, 3, 92),
(4, 2, 4, 87),
(5, 3, 5, 95);

-- Create Clubs Table


CREATE TABLE Clubs (
club_id INT PRIMARY KEY,
club_name VARCHAR(255)
);

-- Insert data into Clubs Table


INSERT INTO Clubs (club_id, club_name) VALUES
(1, 'Hack Club'),
(2, 'Media Club'),
(3, 'Photography Club'),
(4, 'Debate Club'),
(5, 'Sports Club');

-- Create ClubMembers Table


CREATE TABLE ClubMembers (
membership_id INT PRIMARY KEY,
student_id INT,
club_id INT,
FOREIGN KEY (student_id) REFERENCES Students(student_id),
FOREIGN KEY (club_id) REFERENCES Clubs(club_id)
);

-- Insert data into ClubMembers Table


INSERT INTO ClubMembers (membership_id, student_id, club_id) VALUES
(1, 1, 1),
(2, 2, 2),
(3, 3, 3),
(4, 4, 4),
(5, 5, 5);
Define tuple, relation, attributes, cardinality, and degree of a relation.

• Tuple: A single row in a relation (table).


• Relation: A table in a database schema.
• Attribute: A column in a relation that represents a specific characteristic.
• Cardinality: The number of tuples in a relation.
• Degree: The number of attributes in a relation.

Domain of each attribute of each entity

Primary and foreign keys of a relation

A primary key uniquely identifies a tuple within a relation. It cannot be null and should have
minimal redundancy.

A foreign key references the primary key of another table, establishing a relationship between
them.

Task 2
Write a report on all steps taken to achieve the above task with screenshots of each
step and query code. The report must include how you were able to identify
entities, attributes, and relations.
01. Select all columns of a table
Select a Specific column of a table

Select with WHERE clause

Select with DISTINCT

Select with ORDER BY


POST LAB TASK

Task 1: Create the below mentioned table using SQL and insert at least 10 records
in this table using insert into statement, also create primary key. After insertion
apply all above SQL variants on this table like
1. Select all data from the employee table
2. Select only employee_id and department from employee table
3. Select only those employee id where department is Finance
4. Select distinct departments from employee
5. Select hire date from employee in ascending order (order by clasue)

Use [Post Lab 06]

CREATE TABLE Employee (


employee_id INT PRIMARY KEY,
first_name VARCHAR(50) NOT NULL,
last_name VARCHAR(50) NOT NULL,
department VARCHAR(50) NOT NULL,
hire_date DATE NOT NULL
);

INSERT INTO Employee (employee_id, first_name, last_name, department,


hire_date)
VALUES
(11, 'John', 'Doe', 'IT', '2023-01-10'),
(12, 'Jane', 'Smith', 'HR', '2022-05-15'),
(13, 'Michael', 'Johnson', 'IT', '2023-03-20'),
(14, 'Emily', 'Brown', 'Finance', '2022-08-05'),
(15, 'David', 'Wilson', 'HR', '2023-02-28'),
(16, 'Leema', 'Ram', 'IT', '2021-12-01'),
(17, 'Amir', 'Ali', 'Finance', '2022-09-22'),
(18, 'Abdul', 'Rehman', 'Marketing', '2023-04-01'),
(19, 'Danish', 'Ahmed', 'Sales', '2021-01-15'),
(20, 'Meer', 'Ahmed', 'Marketing', '2022-11-10');
01. Select all data from the employee table

1. Select only employee_id and department from employee table.

1. Select only those employee id where department is Finance


1. Select distinct departments from employee

1. Select hire date from employee in ascending order (order by clasue)

Task 2:

Research and describe the fundamental SQL commands used for data manipulation and
database management, namely UPDATE, DELETE, ALTER, DROP, and TRUNCATE.
For each command, provide its syntax and a brief explanation of its functionality.
Additionally, discuss under what circumstances you would use each command and
provide an example of its application, preferably using the existing employee’s table.
Lastly, if you needed to remove an entire database, which command among DROP,
DELETE, and TRUNCATE would be most appropriate, and why?

1. UPDATE:

• Syntax: UPDATE table_name SET column_name = new_value [WHERE condition];


• Explanation: Modifies existing data in a table. You specify the table name, the
column(s) to update, the new value(s), and optionally, a WHERE clause to filter
which rows are affected.
• Use Case: When you need to change specific values in existing records. For example,
updating an employee's department after a promotion.
• Example: Update the department of employee with ID 14 to 'Sales':
UPDATE Employee
SET department = 'Sales'
WHERE employee_id = 14;
2. DELETE:

• Syntax: DELETE FROM table_name [WHERE condition];


• Explanation: Removes rows permanently from a table. Similar to UPDATE, you can
optionally use a WHERE clause to target specific rows.
• Use Case: When you need to remove outdated or irrelevant data from a table. For
example, deleting an employee record after their termination.
• Example: Delete the record for employee with ID 19:
DELETE FROM Employee
WHERE employee_id = 19;

3. ALTER TABLE:

• Syntax: ALTER TABLE table_name ADD/DROP/MODIFY column_name


data_type [CONSTRAINT constraint_name];
• Explanation: Modifies the structure of a table. It allows adding new columns,
removing existing ones, or changing their data types and constraints.
• Use Case: When you need to adapt your table structure to accommodate new data
requirements. For example, adding a 'phone_number' column to the Employee table.
• Example: Add a 'phone_number' column of type VARCHAR(20) to the Employee
table:
ALTER TABLE Employee
ADD phone_number VARCHAR(20);

4. DROP TABLE:

• Syntax: DROP TABLE table_name;


• Explanation: Permanently removes a table and all its data from the database.
• Use Case: When you no longer need a table and its data. Use with caution as dropped
data cannot be recovered.
• Example: Drop the Employee table (**WARNING: This will permanently delete the
data!):
DROP TABLE Employee;

5. TRUNCATE TABLE:

• Syntax: TRUNCATE TABLE table_name;


• Explanation: Deletes all rows from a table, but unlike DELETE, it doesn't use the
transaction log. It's generally faster but cannot be used with a WHERE clause.
• Use Case: When you need to quickly remove all data from a table while preserving
the table structure itself. Use with caution as truncated data cannot be recovered.
• Example: Truncate the Employee table (**WARNING: This will permanently delete
the data!):
TRUNCATE TABLE Employee;

Removing an Entire Database:


Out of the three commands, DROP DATABASE is the most appropriate for removing an
entire database. Here's why:

• DELETE operates on tables within a database. You would need to run separate
DELETE statements for each table, making it inefficient for an entire database.
• TRUNCATE also works on individual tables. While faster than DELETE for a single
table, it's not suitable for removing the entire database structure.
• DROP DATABASE removes the entire database, including all its tables, data, and
associated objects. This is the most efficient and appropriate way to delete a database
you no longer need.

Remember: Use DROP DATABASE with extreme caution as it's a permanent action.

You might also like