sql and e r diagram
TRANSCRIPT
05/01/2023 Prepared By- Shimul & Hirok,CSE,MBSTU 1
WELCOME TO OUR
PRESENTATION
05/01/2023 Prepared By- Shimul & Hirok,CSE,MBSTU 2
SQL and E-R diagram related problems and its solution.
Presentation on……
Presented By• NAME : ID:• MD.MAHBUBUR RAHMAN CE-12038• HIROK BISWAS CE-12032
05/01/2023 Prepared By- Shimul & Hirok,CSE,MBSTU 3
We are going to talking about
SQL expressions: sample bank database(3.19) relational schema(3.21,3.22)E-R diagram for a hospital(6.15)UML equivalents of the E-R diagrams(6.28)Conclution
Problem 1 :
Using the relations of our sample bank database, write SQL expressions to define the following views:
a. A view containing the account numbers and customer names (but not the balances) for all accounts at the Deer Park branch.
b. A view containing the names and addresses of all customers who have an account with the bank, but do not have a loan.
c. A view containing the name and average account balance of every customer of the Rock Ridge branch.
05/01/2023 Prepared By- Shimul & Hirok,CSE,MBSTU 5
Solution of Problem 1 (a)A view containing the account numbers and customer names (but not the balances) for all accounts at the Deer Park branch.
Table: accountTable: depositor
05/01/2023 Prepared By- Shimul & Hirok,CSE,MBSTU 6
Solution of Problem 1 (a)( Cont…)• Query:
select depositor.customer_name,depositor.account_numberfrom depositor,accountwhere depositor.account_number =account.account_numberand account.branch_name='Deer Park';
• Table view:
05/01/2023 Prepared By- Shimul & Hirok,CSE,MBSTU 7
Solution of Problem 1 (b)A view containing the names and addresses of all customers who have an account with the bank, but do not have a borrower.
Table: customer Table: depositor Table: borrower
05/01/2023 Prepared By- Shimul & Hirok,CSE,MBSTU 8
Solution of Problem 1 (b)(Cont…)
select c.customer_name, c.customer_street,c.customer_cityfrom customer c, depositor dwhere c.customer_name not in (selectcustomer_namefrom borrower)and c.customer_name = d.customer_name;
• Query:
• Table view:
05/01/2023 Prepared By- Shimul & Hirok,CSE,MBSTU 9
Solution of Problem 1 (c). A view containing the name and average account balance of every customer of the 'sidney' branch.Table: account Table: depositor
05/01/2023 Prepared By- Shimul & Hirok,CSE,MBSTU 10
Solution of Problem 1 (c) (Cont…)
select customer_name, avg(a.balance)from account a, depositor dwhere a.account_number = d.account_numberand branch_name='sidney'group by customer_name;
• Query:
• Table view:
05/01/2023 Prepared By- Shimul & Hirok,CSE,MBSTU 11
Problem 1 (EXP-02) :
create procedure get_emp(employee_id in varchar(10))return charBEGINselect employee.employee_id,department.department_name,salary.salary_scale,salary.salary_amount from departmentinner join employee_department on department.department_id=employee_department.department_id inner join employee on employee.employee_id=employee_department.employee_idinner join employee_salary on employee.employee_id=employee_salary.employee_id inner join salary on salary.salary_scale=employee_salary.salary_scalewhere employee.employee_id=employee_id;return(employee_id);END;Exec get_emp('emp02');
Solution of Problem 1 (Exp-2):
Create a procedure that will take Employee_ID it’s parameter return Employee_ID ,Department_Name, Salary_Scale and Salary_Amount.
05/01/2023 Prepared By- Shimul & Hirok,CSE,MBSTU 12
Problem 2 :Consider the following relational schema employee(emp_no , name, office, age) books(isbn , title, authors, publisher) loan(emp_no , isbn, date)
Write the following queries in SQL.a. Print the names of employees who have borrowed any book published by
McGraw-Hill.
b. Print the names of employees who have borrowed all books published by McGraw-Hill.
c. For each publisher, print the names of employees who have borrowed more than five books of that publisher.
05/01/2023 Prepared By- Shimul & Hirok,CSE,MBSTU 13
Solution of Problem 2 (a)Print the names of employees who have borrowed any book published
by McGraw-Hill.
Table: employee
Table: books
Table: loan
05/01/2023 Prepared By- Shimul & Hirok,CSE,MBSTU 14
Solution of Problem 2 (a) (cont…)• Query:
select distinct name as employee_name from employeeinner join loan on employee.emp_no=loan.emp_noinner join books on loan.isbn=books.isbnwhere books.publisher='Mcgraw-hill';
• Table view:
05/01/2023 Prepared By- Shimul & Hirok,CSE,MBSTU 15
Solution of Problem 2 (b)Print the names of employees who have borrowed all book published
by McGraw-Hill.
Table: employee
Table: books
Table: loanresult
05/01/2023 Prepared By- Shimul & Hirok,CSE,MBSTU 16
Solution of Problem 2 (b) (cont…)• Query:
select distinct name as employee_name from employeeinner join loan on employee.emp_no=loan.emp_noinner join books on loan.isbn=books.isbnwhere (select count(loan.isbn) from loan where loan.emp_no=employee.emp_no)=(select count(books.isbn) from books where books.publisher='Mcgraw-hill');
• Table view:
05/01/2023 Prepared By- Shimul & Hirok,CSE,MBSTU 17
Solution of Problem 2 (c)For each publisher, print the names of employees who have borrowed
more than five books of that publisher.
Table: employee
Table: books
Table: loanresult
More than5
Books from Mcgraw-hill
(only)
05/01/2023 Prepared By- Shimul & Hirok,CSE,MBSTU 18
Solution of Problem 2 (c) (cont…)• Query:
select name from employee,loan,bookswhere employee.emp_no=loan.emp_no and books.isbn=loan.isbn group by employee.emp_no,name,books.publisher having count(loan.isbn) >=5;
• Table view:
05/01/2023 Prepared By- Shimul & Hirok,CSE,MBSTU 19
Problem 3 :Consider the relational schema
student(student_id , student_name) registerd(student_id , course_id)
Write an SQL query to list the student-id and name of each student along with the total number of courses that the student is registered for. Students who are not registerd for any course must also be listed, with the number of registerdcourses shown as 0.
05/01/2023 Prepared By- Shimul & Hirok,CSE,MBSTU 20
Solution of Problem 3 Table: student
Table: registerd
a is registerd by3
course
b is registerd by1
course
C is not registerd
(0)
05/01/2023 Prepared By- Shimul & Hirok,CSE,MBSTU 21
Solution of Problem 3 (cont…)• Query:select student_id,student_name,(select count(registerd.course_id) from registerd where student.student_id=registerd.student_id) as taken_coursefrom student;• Table view:
05/01/2023 Prepared By- Shimul & Hirok,CSE,MBSTU 22
Problem 4:Consider the relational schema: Department (Department _ID , Department _Name) Employee (Employee_ID , Employee_Name,Employee_Age) Employee _Department( Employee _ID , Department _ID ) Salary (Salary _Scale , Salary _Amount) Employee_ Salary (Employee _ID , Salary _Scale)
a) Find the Department_names where the employee’s average salary is more than 10000.
b) Find the employee_names who has paid in salary scale 1.
c) Update the employee ages by increasing 10 years whose ages are not equal to average age.
05/01/2023 Prepared By- Shimul & Hirok,CSE,MBSTU 23
Solution of Problem 4 (a) Find the Department_names where the employee’s average salary is more than 10000.
Table: department Table: employee_department
Table: employee_salary Table: salary
05/01/2023 Prepared By- Shimul & Hirok,CSE,MBSTU 24
Solution of Problem 4(a) (cont…)• Query:select department_name,avg(salary.salary_amount) from departmentinner join employee_department on department.department_id=employee_department.department_id inner join employee_salary on employee_department.employee_id=employee_salary.employee_id inner join salary on salary.salary_scale=employee_salary.salary_scalehaving avg(salary.salary_amount)>10000group by department.department_name;
• Table view:
05/01/2023 Prepared By- Shimul & Hirok,CSE,MBSTU 25
Solution of Problem 4 (b) Find the employee_names who has paid in salary scale 1.Table: Employee Table: employee_salary
Table: salary
result
05/01/2023 Prepared By- Shimul & Hirok,CSE,MBSTU 26
Solution of Problem 4(b) (cont…)• Query:select employee_name from employee inner join employee_salaryon employee_salary.employee_id=employee.employee_id inner join salary on salary.salary_scale=employee_salary.salary_scalewhere salary.salary_scale=1;
• Table view:
05/01/2023
Solution of Problem 4(c)• Query:update employee set employee.employee_age=employee.employee_age+1where employee.employee_age not in (select avg(employee.employee_age) from employee);
• Table view:Before update:
After update:
Averageage
27Prepared By- Shimul & Hirok,CSE,MBSTU
05/01/2023 Prepared By- Shimul & Hirok,CSE,MBSTU 28
Problem 5Construct an E-R diagram for a hospital with a
set of patients and a set of medical doctors. Associate with each patient a log of the various tests and examinations conducted.
05/01/2023 Prepared By- Shimul & Hirok,CSE,MBSTU 29
Solution Of problem 5:
Fig: E-R diagram for a hospital system.
05/01/2023 Prepared By- Shimul & Hirok,CSE,MBSTU 30
Problem 6
Draw the UML equivalents of the E-R diagrams Of Figures 6.8c,6.9,6.17,6.72,And 6.20.
05/01/2023 Prepared By- Shimul & Hirok,CSE,MBSTU 31
Needed Figure of Problem 6 And Its Solution Given…………………….
05/01/2023 Prepared By- Shimul & Hirok,CSE,MBSTU 32
Fig:6.8c Relationship(One –to-one)
Solution of Fig 6.8c:
05/01/2023 Prepared By- Shimul & Hirok,CSE,MBSTU 33
Solution of Fig 6.9:
05/01/2023 Prepared By- Shimul & Hirok,CSE,MBSTU 34
Solution of Fig 6.11:
05/01/2023 Prepared By- Shimul & Hirok,CSE,MBSTU 35
Solution of Fig 6.12:
05/01/2023 Prepared By- Shimul & Hirok,CSE,MBSTU 36
05/01/2023 Prepared By- Shimul & Hirok,CSE,MBSTU 37
Solution of Fig 6.20(Cont….)
05/01/2023 Prepared By- Shimul & Hirok,CSE,MBSTU 38
THANK YOU