Experiment 1: Aim:-Experiment To Design EER Diagram For Given Case Study, Convert It Into Relational

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 10

Experiment 1

Aim :- Experiment to Design EER diagram for given case study, convert it into relational
model (Schema) and execute different types of queries, procedures and triggers on it.
Queries:7,Procedure:-2,functions:-1,Trigger-2

Case Study :-
The students are evaluated only on the basis of their exams marks and the
projects that they do for their curriculum. This database is for storing data about all the
projects and assignments done by a particular student so that the person evaluating them
has easy access to all the data.

EER Diagram :-
Table Creation :-

create table Class (


class_id int identity(1, 1) primary key,
class_name varchar(10),
);

create table Users(


users_id int identity(1, 1) primary key,
users_name char(50),
email varchar(50),
pass varchar(50), --will be encrypted
users_role varchar(20) check(users_role='staff' or users_role='student'),
);

create table Belongs_to(


class_id int foreign key references Class(class_id),
users_id int foreign key references Users(users_id),
);

create table Staff(


users_id int foreign key references Users(users_id),
staff_id int primary key identity(1, 1),
);

create table Student(


users_id int foreign key references Users(users_id),
student_id int primary key identity(1, 1),
);

create table Evaluation_criteria(


evaluation_id int identity(1, 1) primary key,
staff_id int foreign key references Staff(staff_id),
scores int default 0,
);

create Table Project(


evaluation_id int foreign key references Evaluation_criteria(evaluation_id),
project_id int primary key identity(1, 1),
project_name varchar(30) not null,
file_path varchar(256) null,
project_status int check(project_status>=0 and project_status<=100),
);

create table Assignment(


evaluation_id int foreign key references Evaluation_criteria(evaluation_id),
assignment_id int primary key identity(1, 1),
assignment_name varchar(30) not null,
file_path varchar(256) not null,
due_on date,
);

create table Works_on(


student_id int foreign key references Student(student_id),
project_id int foreign key references Project(project_id),
);

create table Submits(


student_id int foreign key references Student(student_id),
assignment_id int foreign key references Assignment(assignment_id),
);

Trigger 1 :-
Inserts user into corresponding tables depending on their role attribute

create trigger insert_values on Users for insert as


begin
declare @role as varchar(20), @user_id as int
select @role=users_role from inserted
select @user_id = users_id from inserted

if @role = 'student'
begin
insert into Student(users_id) values(@user_id)
end
else
begin
insert into Staff(users_id) values(@user_id)
end

end

Output :-
Users

Staff

Student

Trigger 2:-
create trigger insert_values_evaluation on Evaluation_criteria for insert as
begin
declare @type as varchar(20), @evaluation_id as int
select @type=evaluation_type from inserted
select @evaluation_id = evaluation_id from inserted

if @role = ‘project’
begin
insert into Project(evaluation_id, project_name) values(@evaluation_id, ‘default’)
end
else
begin
insert into Assignment(evaluation_id) values(@evaluation_id)
end

end
Output:-
Evaluation_criteria table :-

Project table:-

Procedure 1:-
create procedure get_projects @student_id as int
as
begin
declare @project_ids Table (project_id int, project_name varchar(30));
insert into @project_ids select distinct Project.project_id, project_name from Project,
Works_on where student_id = @student_id
select * from @project_ids
end

Output :-

Procedure 2:-

create procedure get_assignments_under_evaluation @staff_id as int as


begin
declare @evaluations Table(id int identity(1, 1), evaluation_id int);
declare @iterator as int, @length as int;
--the assignment table will contain the ids of all evaluations by a particular
staff
insert into @evaluations(evaluation_id) select
Evaluation_criteria.evaluation_id from Evaluation_criteria where staff_id=@staff_id;

select @length=count(id) from @evaluations


set @iterator=1;

while @iterator<@length
begin
select Assignment.assignment_id, Assignment.assignment_name
from Assignment where evaluation_id=(
select evaluation_id from @evaluations where id=@iterator
);
set @iterator=@iterator+1;
end
end
Output:-

Function:-
Create function student_count()
returns table as
Return (select class_id,count(*) as No_of_students from belongs_to as b,student as s
where b.users_id=s.users_id group by class_id)

Output:-

Complex Queries :-
1. Display avg score for all projects of a particular student

SELECT AVG(scores) FROM Evalutation_criteria WHERE evaluation_id in (select


evaluation_id from Project where project_id in (select project_id from Works_on
where student_id = (select student_id From Student where users_id = 2)))

Output :-

2.Display list of students working in more than one project

select users_name from users as u,student as s where u.users_id=s.users_id and


s.student_id in (select w.student_id from Works_on w group by student_id
having COUNT(*)>1)
3.Display the count of students in each class

select class_id,count(*) as No_of_students from belongs_to as b,student as s where


b.users_id=s.users_id group by class_id

4.Display list of assignments in ascending order of maximum number of submissions

select a.assignment_id,a.assignment_name,count(*) as no_of_submission from


submits as s, assignment as a group by
s.assignment_id,a.assignment_id,a.assignment_name having s.assignment_id =
a.assignment_id order by no_of_submission
5. Display list of students whose avg project marks is > 50

select u.users_name,avg(e.scores) as Avg_Score from users as u,evalutation_criteria


as e where u.users_id in(select s.users_id from student as s where s.student_id in
(select w.student_id from works_on as w,project as p where w.project_id =
p.project_id and p.evaluation_id = e.evaluation_id)) group by u.users_name having
avg(e.scores)>50

Output :-

6.Display the name of staff who has posted maximum number of assignments.

create view max_assign as select e.staff_id,count(a.assignment_name) as count from


evalutation_criteria as e,assignment as a where e.evaluation_id = a.evaluation_id
group by staff_id

select u.users_name from users as u where u.users_id = (select users_id from staff
where staff_id = (select staff_id from max_assign where count = (select max(count)
from max_assign)))

Output :-
7. Display marks of all projects for a particular student.

Select p.project_name,e.scores from project as p, evalutation_criteria as e where


p.project_id in (select project_id from works_on where student_id = 2) and
p.evaluation_id = e.evaluation_id

Output :-

You might also like