LABS 1-12 & Bonus Task

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 75

LABS 1-12

Name: Muhammad Fahad


CMS ID: 356324

Submitted to
Mr. Waqas Khan,
Lab Engineer
MUHAMMAD FAHAD
CMS: 356324
Submitted to: Waqas Khan
19-03-2021

DATABASE SYSTEMS

LABS-1
TASKS
• Task-1: Write a query to Select columns “city_id” and “country_id”
from the table “city” which already exists in the “Sakila” database.
(Observe the output when you execute the queries.)

• Task-2: Write a query to Select column “actor_id” from the table “actor”
which already exists in the “Sakila” database where the “actor_id=58”.
(Observe the output when you execute the queries.)
• Task-3: Write a query to retrieve names of movies starting with P.

• Task-4:Write a query to retrieve movies which were released in year


2006?
• Task-5: What password did the DBA assign to the user “MIKE”?

• Task-6: Write a query to retrieve data of all actors whose names are not
ending on T.
• Task-7: Write a query to retrieve first and last name of actors

===========================================================================
MUHAMMAD FAHAD
CMS: 356324
Submitted to: Waqas Ahmed
21-10-2020

DATABASE SYSTEMS

LAB-2
TASKS
Create a table called EMPLOYEE table with the following structure and allow
NULL for all columns except EMPNO, ENAME, DOJ and JOB.

TASK CONST.
• Add two columns Commission and HireDate to the EMPLOYEE table.
Commission is a decimal type and can be NULL.
• Modify the column width of the JOB field of EMPLOYEE table with
VARCHAR(50).
• Write a query to drop a column DOJ to the EMPLOYEE table.
MUHAMMAD FAHAD
CMS: 356324
Submitted to: Waqas Ahmed
04-11-2020

DATABASE SYSTEMS

LAB-3
TASK-1: Create DEPARTMENT table with the following structure and set the
DEPTNO as primary key and DNAME is unique key.

STEPS
1) First, we create the database and the table named “DEPARTMENT”.
2) Then create a Primary Key named “DEPTNO” of integer type.
3) Now create a Unique Key named “DNAME” of varchar(10).
4) Create column “LOCATION” of varchar(10).
5) Create column “ESTABLISHED” of DATE type.
6) Insert values in the table as shown
TASK-2: Write a query to add constraints to the EMPLOYEE table that
EMPNO as the primary key and DEPTNO as the foreign key.

STEPS
1) First, we create another table named “EMPLOYEE” in the same
database
2) Then create a PRIMARY KEY named “EMPNO” of int type.
3) Create a column named “DEPTNO” referencing to “DEPTNO” of
Department as FOREIGN KEY.
TASK-3: WRITE the query, to create table, which have at least 16 columns
and each column has CHECK constraint then insert the data and observe the
results.

STEPS
1) First create a database
2) Then we add 16 columns each having a “CHECK”
3) We take PRIMARY KEY as “CMS ID”

OUTPUT
TASK-4: WRITE the query, to create table which have 16 columns and table
have at least two PRIMARY KEY then observe the results.

CODE

STEPS
1) First create a database
2) Then we add 16 columns each having a “CHECK”
3) We take “CMS ID” and “USERNAME” as PRIMARY KEY
4) As we can see TWO Primary key is not possible.
Task-5: WRITE the query, to create table which have 16 columns and each
column has to be UNIQUE KEY then insert the data and observe the results.
Also, apply check constraint to one column i-e one column needs to check
and accept only more than 3000 number of the entry

STEPS
1) First create a database
2) Then we add 16 columns each having a “CHECK”
3) Set all the columns as Unique Key.
4) Apply check constraint on “CMS ID” which only accepts numbers
greater than 3000.
Task-6: Create Four(4) tables

Code

STEPS
1) First create a database
2) Create a Table named “EMPLOYEE”
3) Add all the given columns with their data types
4) Create another table named “Potential”
5) Add all the given columns with their data types
6) Create a column named “employee_id” referencing to “employee_id” of
EMPLOYEE as FOREIGN KEY.
7) Create another table named “DEPARTMENT”
8) Add all the given columns with their data types
9) Create a column named “employee_id” referencing to “employee_id” of
EMPLOYEE as FOREIGN KEY.
Create a column named “potential_id” referencing to “potential_id” of
POTENTIAL as FOREIGN KEY.
10) Create another table named “DEPARTMENT”
11) Add all the given columns with their data
ENTITY RELATION DIAGRAM
MUHAMMAD FAHAD
CMS: 356324
Submitted to: Waqas Ahmed
12-11-2020

DATABASE SYSTEMS

LAB-4
TASKS
Task-1: Insert 10 record into DEPARTMENT table, which is created in
previous Lab.

STEPS
• First we take the same table “DEPARTMENT” from the last lab database
• then we insert 10 records as mentioned above

Task-2: Insert 12 record into EMPLOYEE table, which is created in previous


Lab.
STEPS
• First we take the same table “EMPLOYEE” from the last lab database
• then we insert 12 records as mentioned above

Task-3: Update the EMPLOYEE table to set the commission of all employees
to Rs 1800/- who are working as MANAGER

STEPS
• First, we take the same table “EMPLOYEE” from the last lab database
• After inserting the data, we update it.
• Update the commission of all the managers to Rs1800/- by using the Update and
Set command

Task-4: Create a pseudo table EMPLOYEE_2 with the same structure as the
table EMPLOYEE and insert rows into the table using select clauses.
STEPS
• First, we take the same table “EMPLOYEE” from the last lab database
• To create a duplicate table we use the “create table as select” command
• Another table will be created “Employee_2” with the same records.

Task-5: Delete all employees, who are working as "Cleark".

STEPS
• First, we take the same table “EMPLOYEE” from the last lab database
• Now we delete all the records who are “Clerks” by using the delete command.
• The results are shown

Task-6: Delete the employee, whose name is Qasim.


STEPS
• First, we take the same table “EMPLOYEE” from the last lab database
• Now we delete all the records who are “QASIM” by using the delete command.
• The results are shown

Task-7: List all employees, order by Job


In ascending order.

In descending order

STEPS
• First, we take the same table “EMPLOYEE” from the last lab database
• Now we sort the entries of Job decription in Ascending order first and the
descending order as shown using the Select order by command.
Task-8 : Display JOBS from the EMPLOYEE table avoiding the
duplicated values.

STEPS
• First, we take the same table “EMPLOYEE” from the last lab database
• Now we remove all the duplicate records in the job entity using the Select Distinct
command
• The results are shown

Task-9 : Create the schema, where we can store multiple files


against one user.
STEPS
• First, we create a Database
• Then we create a table “userdata” for a user with all its details as entities
• Then we create another table “DOCUMENTS” and enter the details of the users
documents.
• The results are shown

Task-10 : Create the schema of feature of whatsapp. (most recent


contact use for forward message).

STEPS
• First, we create a Database
• Then we create a table “WHATSAPP” for a user with all its details as entities
• Then we order the messages in Descending order
• The results are shown below
Task-11 : Create the schema from the following ERD .You have to include
created_at, updated_at columns.

STEPS
• First, we create a Database
• Then we create a table “GROUP”, “STUDENTS”, “SUBJECTS”, “MARKS”,
“TEACHERS”, SUBJECT/TEACHER
• write the entities as shown in the ER Diagram.
• Then we Reference the Foreign keys to the respective tables as shown
• The results are shown below
===========================================================================
MUHAMMAD FAHAD
CMS: 356324
Submitted to: Waqas Ahmed
07-12-2020

DATABASE SYSTEMS

LAB-5
TASKS
Task-1: Select all employees, whose department name is "EE" and "CSE"
using IN operator.

STEPS
• First we CREATE the table “EMPLOYEE”
• then select the names of the employees in department CSE and EE using the IN
Command

Task-2: Display all the details of department, whose name does not start with
‘E’ using LIKE operator.
STEPS
• In this task we use the same table EMPLOYEE
• Then we see the details of the employees whose name does not start with E using
not like function

Task-3: Calculate the 4th times salary of all employees, who are woking as
‘Manager’ and display complete data of employee

STEPS
• First, we take the same table “EMPLOYEE” from the earlier task
• We multiply the salary of every MANAGER by 4 using the SELECT AS command

Task-4: Count and display the total employees working as ‘MANAGER’.


STEPS
• First, we take the same table “EMPLOYEE” from the last task
• Then we count the number of MANAGER in the JOB column using the Count()
command

Task-5: Calculate and display the total salary (salary + commission) of the
employees.

STEPS
• First, we take the same table “EMPLOYEE” from the last task
• Now using the sum command we add the salary and commission and show it in a
separate box

Task-6: Display total salary spent for employees who are working as
‘Manager’.
STEPS
• First, we take the same table “EMPLOYEE” from the task
• Now we add the salary of all the MANAGER using the SUM command
• The results are shown

Task-7: List all employee names, DOJ, salary and 30% rise in salary.

STEPS
• First, we take the same table “EMPLOYEE” from the Task
• Then we Add an additional column SalarRise
• Then we update the SalarRise using the formula above
• Then using the SELECT command display the output
Task-8 : Display highest paid employee details under each manager.

STEPS
• First, we take the same table “EMPLOYEE” from the last Task
• Then using a SUB QUERY we display the employee with the highest salary using the
max() command
• The results are shown

Task-9 Display number of employees working in each department and their


department name.

STEPS
• First, we take the same table “EMPLOYEE” from the Task
• Then we Add an additional column CountOf
• Then we using HAVING and GROUP BY command we display the count of the
depatments
• Then using the SELECT command display the output
*BONUS*

Task-10 : Create the schema of feature chat app, where user seen the
message or not. (like whatsapp) refer to below image.

STEPS
• First, we create a Database
• Then we create a table “WHATSAPP” for a user with all its details as entities
• The results are shown below

===========================================================================
MUHAMMAD FAHAD
CMS: 356324
Submitted to: Waqas Ahmed
11-12-2020

DATABASE SYSTEMS

LAB-6
TASKS
Task-1: Select all employees from ‘EE’, ‘IS’ and ‘CSE’ department using
UNION operator.

STEPS
• First we CREATE the table “EMPLOYEE”
• then select the names of the employees in department CSE, IS and EE using UNION
Command

Task-2: Display all employees name and salary, whose salary is lesser than
maximum salary of the company and job title starts with ‘S’.
STEPS
• In this task we use the same table EMPLOYEE
• Then we see the details of the employees whose salary is lesser than maximum
salary of the company and job title starts with ‘S’ using SUBQUERIES

Task-3 Write a query to find all the employees who work in the same job as
‘ALI’.

STEPS
• First, we take the same table “EMPLOYEE” from the earlier task
• Now we use SELECT and Subquery to display the departments as asked

Task-4: Write a query to display information of employees who earn more


than any employee in dept 10.
STEPS
• First, we take the same table “EMPLOYEE” from the last task
• We use ANY and SUBQUERY to display the required task

Task-5: Display the employees who have the same job as ‘ALI’ and whose
salary <=‘NASIR’.

STEPS
• First, we take the same table “EMPLOYEE” from the last task
• We use AND and SUBQUERY to display the required result

Task-6: Write a query to display the name and job of all employees in dept 20
whohave a job that someone in the CSE dept as well.
STEPS
• First, we take the same table “EMPLOYEE” from the task
• Now we use the combination of AND and SUBQUERY to display the output

Task-7: Write a query to list all the employees who salary is < the average
salary of their own dept.

STEPS
• First, we take the same table “EMPLOYEE” from the Task
• We use GROUP BY and SUBQUERY in this task

Task-8 : Write a query to list the employees in dept 10 with the same job as
anyone in the ‘CSE’ department
STEPS
• First, we take the same table “EMPLOYEE” from the last Task
• Now we use the combination of AND and SUBQUERY to display the output
• The results are shown

Task-9 Write a query to create a view, which list the employees including dept
name.

STEPS
• First, we take the same table “EMPLOYEE” from the Task
• Then we create a ViewTable using the CREATE
• Then we display it using the SELECT command.
MUHAMMAD FAHAD
CMS: 356324
Submitted to: Waqas Ahmed
17-12-2020

DATABASE SYSTEMS

LAB-7
TASKS
Task-1: Display the departments that are accredited by the quality council.

STEPS
• First we CREATE the table “EMPLOYEE”
• then select the names of the department which are accredited by using the SELECT
clause

Task-2: Display the employees of departments which are not accredited by


the quality council
STEPS
• In this task we use the same table EMPLOYEE
• then select the names of the department which are not accredited by using the
SELECT clause

Task-3 Write a query to output the name, job, empno, deptname and
location for each dept, even if there are no employees.

STEPS
• First, we take the same table “EMPLOYEE” and “DEPARTMENT” from the earlier
task
• Then by using RIGHT JOIN we display the data as requested
Task-4: Find the name of the manager for each employee. Include the
following in the output: empno, empname, job and his manager’s
name.

STEPS
• First, we take the same table “EMPLOYEE” and “DEPARTMENT” from the last task
• Then using the join clause and AS clause we display the query as asked
Task-5: Display the details of those who draw the same salary

STEPS
• First, we take the same table “EMPLOYEE” from the last task
• Then using SELF JOIN we display the details of the person that draws same salary
as shown

===========================================================================

THANK YOU
MUHAMMAD FAHAD
CMS: 356324
Submitted to: Waqas Ahmed
25-12-2020

DATABASE SYSTEMS

LAB-8
TASKS
Task-1: Write a procedure to add an amount of Rs.1000 for the employees
whose salaries is greater than 5000 and who belongs to the deptno
passed as an argument

STEPS
• First we CREATE a PROCEDURE “add_salary” that accepts integer type dep
• The we update the salary whose salary is more than 5000 by using UPDATE clause
• Then we call the function using CALL
• Calling the function, updates the value
• then select the names of employees of the department by using the SELECT clause
Task-2: Write a MySQL block to update the salary of the employee with a 10%
increase whose empno is to be passed as an argument for the procedure.

STEPS
• First we CREATE a PROCEDURE “upd_salary” that accepts integer type emplno
• The we update the salary whose salary by 10% whose employee id is given by using
UPDATE clause
• Then we call the function using CALL
• Calling the function, updates the value
• then select the names of employees of the department by using the SELECT clause

Task-3 Write a function to find the salary of the employee who is working in
the deptno 20 (to be passed as an argument).
STEPS
• First we CREATE a FUNCTION “dep_salary” that accepts integer type dep and
RETURNS integer type
• The we return the salary by using RETURN clause
• Then we call the function using SELECT

Task-4: Write a function to find the nature of job of the employee whose
deptno is 20(to be passed as an argument)

STEPS
• First we CREATE a FUNCTION “JOB_NATURE” that accepts integer type dep and
RETURNS varchar type
• The we return the salary by using RETURN clause
• Then we call the function using SELECT
Task-5: Write a MySQL block to obtain the department name of the employee
who works for deptno 10

STEPS
• First we CREATE a PROCEDURE “upd_salary” that accepts integer type emplno
• then select the names of employees of the department by using the SELECT clause
• Then we call the function using CALL
• Calling the function, displays the value

===========================================================================

THANK YOU
MUHAMMAD FAHAD
CMS: 356324
Submitted to: Waqas Ahmed
3-01-2021

DATABASE SYSTEMS

LAB-9
TASKS
Task-1: Write a TRIGGER to ensure that DEPT TABLE does not contain
duplicate of null values in DEPTNO column.

STEPS
• First, create a trigger using the CREATE TRIGGER command.
• As we want this event to happen before the action, so we use BEFORE ON
• In this task we want the event to be executed before insertion, so we use BEFORE
INSERT ON FOR EACH ROW
• We use the BEGIN and END before and after the main body of event
• In this task we only want to insert values which are not NULL, Therefore the Query is
as shown
• Testing the TRIGGER by Inserting
Task-2: Write a Trigger to carry out the following action: on deleting a deptno
from dept table, all the records with that deptno has to be deleted
from the emp table

STEPS
• First, create a trigger using the CREATE TRIGGER command.
• As we want this event to happen after the action, so we use AFTER ON
• In this task we want the event to be executed after deletion, so we use AFTER
DELETE ON FOR EACH ROW
• We use the BEGIN and END before and after the main body of event
• In this task we only want to delete values which correspond to the other table,
Therefore the Query is as shown
• Testing the TRIGGER by Deleting
Task-3 • Write a Trigger to carry out the following action: on deleting any
records from the emp table, the same values must be inserted into
the log table.

STEPS
• First, create a trigger using the CREATE TRIGGER command.
• As we want this event to happen after the action, so we use AFTER ON
• In this task we want the event to be executed after deletion, so we use AFTER
DELETE ON FOR EACH ROW
• We use the BEGIN and END before and after the main body of event
• In this task we only want to insert values in a LOG TABLE which we deleted from the
other table, Therefore the Query is as shown
• Testing the TRIGGER by Deleting
Task-4 (CURSORS)
Write a store procedure using cursors to assign grades to the students based
on the score as follows: (Referencing example program 1)
Grade F if score < 40
Grade C if 40 <= score < 60
Grade B if 60 <= score < 80
Grade A if score >= 80
STEPS
• First, create a procedure using the CREATE PROCEDURE command.
• We pass 4 values as the arguments.
• We use the BEGIN and END before and after the main body of event
• We declare the variables we want to use and fetch later using the DECLARE
command
• The we declare the cursor Grade_Cursor using the DECLARE CURSORE command
• The cursor command is attached with a SELECT command as shown
• We open the cursor using the OPEN command
• We start our main loop with LOOP
• All the constraints and conditions are applied in the loop using IF and IF ELSE
statements
• Then we insert the new values in the Cursor_Table table
• We then close the Grade_Cursor by using CLOSE command
• In this task we insert values using CALL command
• Testing the output by using SELECT command and displaying the Log table.

===========================================================================

THANK YOU
MUHAMMAD FAHAD
CMS: 356324
Submitted to: Waqas Ahmed
23-2-2021

DATABASE SYSTEMS

LAB 10
TASK
Entities
• Employee

STEPS
• First we CREATE the table “EMPLOYEE”
• We add the attributes as required
• We use Computed Columns to add a constraint to check whether the
employee is an OFFFICER, LABOURER, or a STAFF

• Department

STEPS
• Then we CREATE the table “DEPARTMENT”
• We add the attributes as required
• We reference deptD to deptID from Employee
• Files

STEPS
• First we CREATE the table “File”
• We add the attributes as required
• We reference fileID to empFILE from Employee
• fileSIZE is the file size of the file
• fileTYPE is the type of file
• fileCREATED tells the date file was create
• fileMODIFIED tells the modified date of the file

• Record

STEPS
• First we CREATE the table “Record”
• We add the attributes as required
• We reference recID to empRECORD from Employee

ER DIAGRAM

===========================================================================

THANK YOU
MUHAMMAD FAHAD
CMS: 356324
Submitted to: Waqas Ahmed
06-03-2021

DATABASE SYSTEMS

LAB-11
TASKS
Task-1: Create the previous schema, which we did in the last lab. Create
some data in the schema, and observe the results. And, prove that your
schema is enough smart to handle the requirements..

ENTITIES AND THEIR VALUES WITH RESULTS


EMPLOYEE

DEPARTMENT
RECORDS

FILES

STEPS
• First we CREATE the schema of “HR management system” from previous Lab
• then using the select and Insert queries we insert and then display the records
inserted
• The results are as shown
Task-2: Create two screen one is for Sign in and sign-up

• In sign-in: You need to create two textfields or input where user can put
email and password.

STEPS
• In this task we use React
• Using components in React, I made a .JS file of login and exported it to the App.js
file and applied appropriate styling in App.css
• In sign-up: You need to create 8 fields, which will get the information like
name, father name, DoB, CMS, semester, department, hostel and
joining_date of NUST.

STEPS
• In this task we use React
• Using components in React, I made a .JS file of login and exported it to the App.js
file and applied appropriate styling in App.css
• The textfields are inserted with appropriate data type
• A scroll feature is also available for overflow
• In sign-up and sign-in: You need to have submit button and that submit
button should fetch information from textfield and display in JSON
format in the log.

STEPS
• In this task we use React and hooks form
• As we can see by applying functions, The fields
entered are displayed under the console tab of Inspect as
shown

===========================================================================

THANK YOU
MUHAMMAD FAHAD

CMS: 356324
Submitted to

Waqas Khan
19-03-2021

DATABASE SYSTEMS

LAB-12
Create the POST endpoint (API), then push the sign up data(which you did in
last lab task) via API
• Note: store the password in the hash. Your task is considered wrong, If
your password is naked.

STEPS

First, we make connection with the database as shown below

Then we enter the values in the register section and press register
For storing the password we have used the following code for storing it
in Database as hash

After pressing the button the entries are stored in the database we
created as shown and the password is stored as hash

Then we retrieve and check the pushed data in the database


As we can see below the program checks the hash password and signs
it And prints an appropriate message

======================= THANK YOU =========================


MUHAMMAD FAHAD

CMS: 356324
Submitted to

Waqas Khan
19-03-2021

DATABASE SYSTEMS

Bonus Task
TASK
Entities
• Employee

STEPS
• First we CREATE the table “EMPLOYEE”
• We add the attributes as required
• paySCALE and Signature columns are added as required in the
task

• Department

STEPS
• Then we CREATE the table “DEPARTMENT”
• We add the attributes as required
• We reference deptID to depID from Employee
• Officer

STEPS
• First we CREATE the table “Officer”
• We add the attributes as required
• We apply a constraint to paySCALE, if paySCALE is equal or
greater than 17, he is an oficer

• File

STEPS
• First we CREATE the table “File”
• We add the attributes as required
• fileCAT is the file category
• filetype is the type of file
• fileSIGN checks If the file is signed or not
• FileOwner

STEPS
• First we CREATE the table “FileOwner”
• We add the attributes as required
• We reference ownID to empID from Employee

• Reciever

STEPS
• First we CREATE the table “RECIEVER”
• We add the attributes as required
• We reference FILE to fileD from FILE
• Then, We reference Signature to Signature from Officer
• Tracking

STEPS
• First we CREATE the table “Tracking”
• We add the attributes as required
• We reference TrackingID to fileID from FILE
• Then, We reference Sender to ownID from FileOwner
• Then, We reference reciever to recID from Reciever
ER DIAGRAM

===========================================================
THANK YOU

You might also like