sql and e r diagram

38
08/14/2022 Prepared By- Shimul & Hirok,CSE,MBSTU 1 WELCOME TO OUR PRESENTATION

Upload: mahbubur-rahman-shimul

Post on 25-Jan-2017

144 views

Category:

Engineering


3 download

TRANSCRIPT

Page 1: SQL and E R diagram

05/01/2023 Prepared By- Shimul & Hirok,CSE,MBSTU 1

WELCOME TO OUR

PRESENTATION

Page 2: SQL and E R diagram

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

Page 3: SQL and E R diagram

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

Page 4: SQL and E R diagram

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.

Page 5: SQL and E R diagram

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

Page 6: SQL and E R diagram

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:

Page 7: SQL and E R diagram

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

Page 8: SQL and E R diagram

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:

Page 9: SQL and E R diagram

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

Page 10: SQL and E R diagram

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:

Page 11: SQL and E R diagram

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.

Page 12: SQL and E R diagram

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.

Page 13: SQL and E R diagram

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

Page 14: SQL and E R diagram

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:

Page 15: SQL and E R diagram

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

Page 16: SQL and E R diagram

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:

Page 17: SQL and E R diagram

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)

Page 18: SQL and E R diagram

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:

Page 19: SQL and E R diagram

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.

Page 20: SQL and E R diagram

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)

Page 21: SQL and E R diagram

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:

Page 22: SQL and E R diagram

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.

Page 23: SQL and E R diagram

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

Page 24: SQL and E R diagram

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:

Page 25: SQL and E R diagram

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

Page 26: SQL and E R diagram

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:

Page 27: SQL and E R diagram

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

Page 28: SQL and E R diagram

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.

Page 29: SQL and E R diagram

05/01/2023 Prepared By- Shimul & Hirok,CSE,MBSTU 29

Solution Of problem 5:

Fig: E-R diagram for a hospital system.

Page 30: SQL and E R diagram

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.

Page 31: SQL and E R diagram

05/01/2023 Prepared By- Shimul & Hirok,CSE,MBSTU 31

Needed Figure of Problem 6 And Its Solution Given…………………….

Page 32: SQL and E R diagram

05/01/2023 Prepared By- Shimul & Hirok,CSE,MBSTU 32

Fig:6.8c Relationship(One –to-one)

Solution of Fig 6.8c:

Page 33: SQL and E R diagram

05/01/2023 Prepared By- Shimul & Hirok,CSE,MBSTU 33

Solution of Fig 6.9:

Page 34: SQL and E R diagram

05/01/2023 Prepared By- Shimul & Hirok,CSE,MBSTU 34

Solution of Fig 6.11:

Page 35: SQL and E R diagram

05/01/2023 Prepared By- Shimul & Hirok,CSE,MBSTU 35

Solution of Fig 6.12:

Page 36: SQL and E R diagram

05/01/2023 Prepared By- Shimul & Hirok,CSE,MBSTU 36

Page 37: SQL and E R diagram

05/01/2023 Prepared By- Shimul & Hirok,CSE,MBSTU 37

Solution of Fig 6.20(Cont….)

Page 38: SQL and E R diagram

05/01/2023 Prepared By- Shimul & Hirok,CSE,MBSTU 38

THANK YOU