You are on page 1of 18

ASSIGNMENT 1

1.Create location, department, job_grade, and employee tables with the following columns.

a)Location: (location_id:number, city:string)


create table location(location int,city varchar(200));

b)Department:(department_id:number,department_name:stringhead:number, department_location:number)
create table department(dept_id int,dept_name varchar(255),head int,dept_location int);

c)Job_grade:(job_grade:string,lower_bound:number, upper_bound:number)
create table job_grade(job_grade varchar(255),lower_bound int,upper_bound int);

d)Employee:(employee_id:number,first_name:string,last_name:string,join_date:date, manager_id:number,
salary:number)
create table employee(emp_id int primary key,first_name varchar(200),last_name varchar(50),join_date
date,manager_id int,salary int);

2.Alter employee table to add job_grade column which is of string data type.
o alter table employee add job_grade varchar(255);
o alter table employee add dept_id int;
o alter table job_grade add primary key (job_grade);

3.Alter employee table to make job_grade a foreign key to job_grade table, manager_id a foreign key to
employee table, department_id a foreign key to department table.
o alter table employee add foreign key (job_grade) references job_grade(job_grade);
o alter table department add primary key(dept_id);
o alter table employee add foreign key (dept_id) references department(dept_id);
o create table man(manager_id int primary key);
o alter table employee add foreign key (manager_id) references man(manager_id);

4.Create a dummy table called my_employee with the same definition as employee table and than drop the
table.
Create table my_employee like employee;
drop table my_employee;

5.Insert data into location, department, job_grade & employee tables.


o insert into location values(1,'Puttur');
o insert into job_grade values('Staff',5000,10000);
o insert into department values(2,'CS',5,1);
o insert into man values(2);
o insert into employee values(101,'ram','charan','2007-12-11',2,7500,'Staff',2)

6.Give a list of all employees (names as first_name, last_name) who belong to one department_id.
select first_name,last_name from employee where dept_id=2;

7.Select employee last_names from employee table who belong to a certain department_id and have a salary
greater than 5000.
select last_name from employee where dept_id=2 and salary>5000;

8.Select employee last_name with first letter in capital, all smalls and all capitals from employee table for all
employees.
All caps)select upper(last_name) from employee;
all small)select lower(last_name) from employee;
first letter caps)select initcap(last_name) from employee;

https://stackoverflow.com/questions/12364086/how-can-i-achieve-initcap-functionality-in-mysql

9.Select the salary and additional HRA (7.5% of the salary) for each employee in employee table rounded to a
whole number
select salary,salary*0.075 as HRA from employee;

10.Select employee last_name, join_date, and the number of days he/she has been working in the firm as of
today.
select last_name,join_date,datediff(curdate(),join_date) as duration from employee;

11.Select employee last_name of all employees whose salary is greater than the salary of employee with id = 2.
select last_name from employee where salary>(select salary from employee where emp_id=101);

12.Select all employees whose salary is greater than the salaries of both employees with ids 2 & 3.
select last_name,salary from employee where salary>(select salary from employee where emp_id=103)
and salary>(select salary from employee where emp_id=101);

13.Select employee lastname and the corresponding department_name for all employees in employees table.
select last_name,dept_name from employee,department where department.dept_id=employee.dept_id;

14.Select all employees whose salary is lesser than all employees in the same job grade.
select first_name,last_name,salary,job_grade from employee where salary in(select min(salary) from
employee group by job_grade);

15.Select the average salary of all employees in department with department_id = 2


select dept_id,AVG(salary) as 'avg' from employee where dept_id=2;

16. Select AVG salary of each department which has employees in employee table.
select AVG(salary) as 'avg' from employee where dept_id='1';

17.Select minimum salary of all departments where the minimum salary is less than 10000.
select min(salary),dept_name from department,employee where salary<10000;

18. Give a list of all employees who earn a salary greater than 10000 or work in job grade MANAGER.
select first_name,last_name from employee where salary>30000 or(select job_grade from job_grade
where job_grade='professor');

19.Create a view that shows all employees of department_id = 10 and select from the view.
create view empview as select first_name,last_name,dept_id from employee where dept_id='1';
select * from empview;
20.Write a query that would give the employee name, his salary and the lower end and higher end salaries of
his job grade?
select first_name,last_name,salary,lower_bound,upper_bound from job_grade,employee where
employee.job_grade=job_grade.job_grade;

21.Write a query that would give the employee name and his department name using natural join.
select first_name,last_name,dept_name from employee natural join department;

22.Write a query which would give all employee names and their department names. The query should also
return employees who are currently not allotted to any department and departments that do not have any
employees.
SELECT D.dept_name,E.first_name,E.last_name FROM employee E,department D WHERE NOT
EXISTS (SELECT * FROM employee E WHERE D.dept_id = E.dept_id);

23. Give the list of all the employees who earn a salary greater than 30000 or work in job_grade D.
select * from employee where salary>30000 or job_grade='professor';

24.Create a procedure to count the total number of employees in each department.


DELIMITER //
create procedure employ()
begin
select d.dept_name,count(emp_id) as total from department d inner join employee e where
d.dept_id=e.dept_id group by d.dept_id;
end//
call employ;

25.Create a trigger to update total salary of a department when new employee is hired. create table depsal as
select dept_id,0 as totalsalary from department;
 create table depsal as select dept_id,0 as totalsalary from department;
 update depsal set totalsalary=(select sum(salary) from employee where dept_id=2) where
dept_id=2;

delimiter |
create trigger update_sal
after insert on employee
for each row
begin
if new.dept_id is not null then
update depsal set totalsalary=totalsalary+new.salary where dept_id=new.dept_id;
end if;
end|

insert into employee values(106,'vidya','bharadwaj','2012-3-4',4,25000,'professor',2);

mysql> show tables;


+----------------+
| Tables_in_ass1 |
+----------------+
| department |
| employee |
| empview |
| job_grade |
| location |
| man
depsal |

mysql> desc location;


+----------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+--------------+------+-----+---------+-------+
| location | int(11) | YES | | NULL | |
| city | varchar(200) | YES | | NULL | |
+----------+--------------+------+-----+---------+-------+

mysql> desc department;


+---------------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------------+--------------+------+-----+---------+-------+
| dept_id | int(11) | NO | PRI | 0 | |
| dept_name | varchar(255) | YES | | NULL | |
| head | int(11) | YES | | NULL | |
| dept_location | int(11) | YES | | NULL | |
+---------------+--------------+------+-----+---------+-------+

mysql> desc job_grade;


+-------------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------------+--------------+------+-----+---------+-------+
| job_grade | varchar(255) | NO | PRI | | |
| lower_bound | int(11) | YES | | NULL | |
| upper_bound | int(11) | YES | | NULL | |
+-------------+--------------+------+-----+---------+-------+

mysql> desc employee;


+------------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+------------+--------------+------+-----+---------+-------+
| emp_id | int(11) | NO | PRI | NULL | |
| first_name | varchar(200) | YES | | NULL | |
| last_name | varchar(255) | YES | | NULL | |
| join_date | date | YES | | NULL | |
| manager_id | int(11) | YES | MUL | NULL | |
| salary | int(11) | YES | | NULL | |
| job_grade | varchar(255) | YES | MUL | NULL | |
| dept_id | int(11) | YES | MUL | NULL | |
+------------+--------------+------+-----+---------+-------+

mysql> desc man;


+------------+---------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+------------+---------+------+-----+---------+-------+
| manager_id | int(11) | NO | PRI | NULL | |
+------------+---------+------+-----+---------+-------+

ASSIGNMENT 2

2.Create the tables with necessary columns with proper data types. Establish integrity constraints and
relationships
a)account(acc_no,branch_name, balance)
 create table account(acc_no int,branch_name varchar(255),balance int);
 alter table account add primary key(acc_no);

b)branch(branch_name,branch_city,assets);
 create table branch(branch_name varchar(255),branch_city varchar(255),assets int);
 alter table branch add primary key(branch_name);

c)customer(customer_name,customer_street,customer_city)
create table customer(customer_name varchar(255),customer_street int,customer_city varchar(255));

d)loan (loan_nu, branch_name,amount)


create table loan(loan_no int,branch_name varchar(255),amount int);

e)depositor(customer_name,acc_no)
create table depositor(customer_name varchar(255),acc_no int);

f)insert the relevent data for all the tables.


 insert into account values(101,'vijaya',500);
 insert into branch values('Vijaya','Puttur',100);
 insert into customer values('varun',1,'puttur');
 insert into loan values(2,'karnataka',2000 );
 insert into depositor values('varun',101);
 alter table loan add foreign key(branch_name) references branch(branch_name);
 alter table depositor add foreign key(acc_no) references account(acc_no);
 alter table customer add primary key(customer_name);
 alter table depositor add foreign key(customer_name) references customer(customer_name);
 insert into depositor values('vidya',101);

1. list number of accounts with balance between 700 and 900


select * from account where balance between 700 and 900;

2.for all customers who have a loan from the bank find their names loan numbers and loan amount
select d.customer_name,a.acc_no,l.loan_no,l.amount,a.branch_name from depositor d,account a,loan l
where d.acc_no=a.acc_no and l.branch_name=a.branch_name;
3.find the customer names, loan number and loan amount for all the loans at the perryridge branch
select d.customer_name,l.loan_no,l.amount from depositor d,loan l where l.branch_name='Perryridge'
group by branch_name;

4.for all customers who have a loan from the bank , find their names and loan numbers with the attribute
loan_number replaced by loan_id
select d.customer_name,a.acc_no,l.loan_no as loan_id,l.amount,a.branch_name from depositor
d,account a,loan l where d.acc_no=a.acc_no and l.branch_name=a.branch_name;

5.find the names of all branches that have assets greater than atleast one branch located in brooklyn
select branch_name,assets from branch where assets>(select assets from branch where
branch_city='Brooklyn');

6.list loan data ordered by decreasing amount then increasing loan numbers.
select * from loan order by amount desc,loan_no asc;

7.find all the bank customers having a loan an account, or both at the bank.
 create table borrower(loan_no int,customer_name varchar(255));
 alter table loan add primary key(loan_no);
 alter table borrower add foreign key(loan_no) references loan(loan_no);
 alter table borrower add foreign key(customer_name) references customer(customer_name);
 insert into borrower values(1,'vidya');

(select customer_name from depositor) union (select customer_name from borrower);

8.find all customers having both a loan and an account at the bank
select distinct customer_name from borrower,loan where borrower.loan_no=loan.loan_no and
customer_name in(select customer_name from account,depositor where
account.acc_no=depositor.acc_no);

9.find all customers have an account but no loan at the bank


 insert into account values(200,'vijaya',2000);
 insert into customer values('ashish',4,'sulya');
 insert into depositor values('ashish',200);

select customer_name from depositor where customer_name not in (select customer_name from
borrower);

10.find the average account balance at the perryridge branch.


select avg(balance) as avg,branch_name from account where branch_name='Perryridge';

11.find the average account balance at each branch


select avg(balance) as avg,branch_name from account group by branch_name;

12.find the number of depositor for each branch


select branch_name,customer_name,count(distinctcustomer_name)from depositor,account where
depositor.acc_no=account.acc_no group by branch_name;
13.find those branches where the average accounts balance is more than Rs. 1200
select (avg(balance)>1200) as 'more than 1200',branch_name from account group by branch_name;

14.find all the customers who have both a loan and an account at the perryridge branch
select distinct customer_name from borrower,loan where borrower.loan_no=loan.loan_no and
branch_name='Perryridge' and customer_name in(select customer_name from account,depositor where
account.acc_no=depositor.acc_no and branch_name='Perryridge');

15..find the names of all branches that have an asset value greater than that of each branch in brooklyn
select branch_name,assets from branch where assets>(select assets from branch where
branch_city='Brooklyn');

16. create procedure to transfer Rs. 1000 from Account number A-215 to A-101
DELIMITER //
create procedure bank2()
begin
START TRANSACTION;
update account set balance=balance-100 where acc_no=103;
update account set balance=balance+100 where acc_no=104;
COMMIT;
end//

mysql> select * from account;

TABLES
->SHOW TABLES;
+--------------+
| Tables_in_a2 |
+--------------+
| account |
| borrower |
| branch |
| customer |
| depositor |
| loan |
+-------------+

mysql> desc account;


+-------------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------------+--------------+------+-----+---------+-------+
| acc_no | int(11) | NO | PRI | 0 | |
| branch_name | varchar(255) | YES | | NULL | |
| balance | int(11) | YES | | NULL | |
+-------------+--------------+------+-----+---------+-------+

mysql> desc branch;


+-------------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------------+--------------+------+-----+---------+-------+
| branch_name | varchar(255) | NO | PRI | | |
| branch_city | varchar(255) | YES | | NULL | |
| assets | int(11) | YES | | NULL | |
+-------------+--------------+------+-----+---------+-------+

mysql> desc customer;


+-----------------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------------+--------------+------+-----+---------+-------+
| customer_name | varchar(255) | NO | PRI | | |
| customer_street | int(11) | YES | | NULL | |
| customer_city | varchar(255) | YES| | NULL | |
+-----------------+--------------+------+-----+---------+-------+

mysql> desc depositor;


+---------------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------------+-------------+------+-----+---------+-------+
| customer_name | varchar(25) | YES | MUL | NULL | |
| acc_no | int(11) | YES | MUL | NULL | |
+---------------+-------------+------+-----+---------+-------+

mysql> desc loan;


+-------------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------------+-------------+------+-----+---------+-------+
| loan_no | int(11) | NO | PRI | 0 | |
| branch_name | varchar(25) | YES | MUL | NULL | |
| amount | int(11) | YES | | NULL | |
+-------------+-------------+------+-----+---------+-------+

mysql> desc borrower;


+---------------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------------+--------------+------+-----+---------+-------+
| loan_no | int(11) | YES | MUL | NULL | |
| customer_name | varchar(255) | YES | MUL | NULL | |
+---------------+--------------+------+-----+---------+-------+
ASSIGNMENT 3
create the tabless with necessary columns with proper data types. Establish integrity constraints and
relationships.

a)sales_order(order_no, order_date,client_name,delay_type,delay_date);
 create table sales_order(order_no int,order_date date,client_no int,client_name
varchar(255), delay_type varchar(255),delay_date date);
 alter table sales_order add primary key(order_no)

b)product(prod_no,description,qty_hand,cost_price,sell price);
 create table product(prod_no int,description varchar(255), qty_hand int, cost_price
int,sell_price int);
 alter table product add primary key(prod_no);

c)sales_order_details(orderno,prod_no,qty_order,qty_sup,prod_rate);
create table sales_order_details(order_no int,prod_no int,qty_order int,qty_sup int,prod_rate
int);

d)client(client_no,client_name,city,state,bal_due);
 create table client(client_no int,client_name varchar(25),city varchar(25),state
varchar(25),bal_due int);
 alter table client add primary key(client_no);
 alter table sales_order add foreign key(client_no) references client(client_no);
 alter table sales_order_details add foreign key(order_no) references sales_order(order_no);

insert the relevent data for all the tables


 insert into client values(102,'vidya','Puttur','Karnataka',500);
 insert into sales_order values(2,'2017-04-10',102,'packing problem','2017-04-20');
 insert into product values(11,'Good',12,1200,1000);
 insert into sales_order_details values(1,11,2,2,5);

1.select product_no who selling price is greater than 2000 and less than equal to 5000.
select prod_no,sell_price from product where sell_price>2000 and sell_price<=5000;

2.find the product_no,description of non moving products that is product not being sold.
select sales_order_details.prod_no,description from sales_order_details,product where qty_order =
0 and sales_order_details.prod_no = product.prod_no;

3.find the product and their quantities for order placed by client 'ivan Bayross'
select s.prod_no,s.qty_order,c.client_name from sales_order_details s,client c where
client_name='ivan' group by client_name;

4.write the query to select all details of sales order whose client name start with I.
select * from sales_order where client_name like 'i%';

5.write the query to select product_no and description where the product ordered = 5
select p.prod_no,p.description,s.qty_order from product p,sales_order_details s where qty_order=2
and p.prod_no=s.prod_no;

6.display order information for the clients C11 and C12


select sd.order_no,sd.qty_order,sd.qty_sup,sd.prod_rate,s.client_no from sales_order_details
sd,sales_order s,client c where sd.order_no=s.order_no and s.client_no=c.client_no group by
client_no having s.client_no=101 or s.client_no=102;

7.find out whether the product 1.44 Floppies has been ordered by any client and print the name to which it
was sold.
 alter table product add product_name varchar(255);
 update product set product_name='Mouse' where prod_no=12;

select c.client_name,c.city,sd.order_no,sd.prod_no,p.product_name from client c,product


p,sales_order_details sd,sales_order s where s.order_no=sd.order_no and sd.prod_no=p.prod_no
and s.client_no=c.client_no and product_name='floppy';

8.list product_no,description,sell_price where description contains the substring O


select prod_no,description,sell_price from product where description like"%o%";

9.display the total number of products in the firm and display the field renamed as total_product.
select sum(qty_hand) as 'total_product' from product;

10.print client_name and city who order the product mouse.


select c.client_name,c.city,sd.order_no,sd.prod_no,p.product_name from client c,product
p,sales_order_details sd,sales_order s where s.order_no=sd.order_no and sd.prod_no=p.prod_no
and s.client_no=c.client_no and product_name='Mouse';

11.select description from product where (cost_price-sell_price)>50;


select description,cost_price,sell_price from product where (cost_price-sell_price)>50;

show tables;
+---------------------+
| Tables_in_client |
+---------------------+
| client |
| product |
| sales_order |
| sales_order_details |
+---------------------+

desc client;
+-------------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------------+-------------+------+-----+---------+-------+
| client_no | int(11) | NO | PRI | 0 | |
| client_name | varchar(25) | YES | | NULL | |
| city | varchar(25) | YES | | NULL | |
| state | varchar(25) | YES | | NULL | |
| bal_due | int(11) | YES | | NULL | |

desc product;
+--------------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------------+--------------+------+-----+---------+-------+
| prod_no | int(11) | NO | PRI | 0 | |
| description | varchar(255) | YES | | NULL | |
| qty_hand | int(11) | YES | | NULL | |
| cost_price | int(11) | YES | | NULL | |
| sell_price | int(11) | YES | | NULL | |
| product_name | varchar(255) | YES | | NULL | |
+--------------+--------------+------+-----+---------+-------+

desc sales_order;
+-------------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------------+--------------+------+-----+---------+-------+
| order_no | int(11) | NO | PRI | 0 | |
| order_date | date | YES | | NULL | |
| client_no | int(11) | YES | MUL | NULL | |
| client_name | varchar(255) | YES | | NULL | |
| delay_type | varchar(255) | YES | | NULL | |
| delay_date | date | YES | | NULL | |
+-------------+--------------+------+-----+---------+-------+

desc sales_order_details;
+-----------+---------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------+---------+------+-----+---------+-------+
| order_no | int(11) | YES | MUL | NULL | |
| prod_no | int(11) | YES | | NULL | |
| qty_order | int(11) | YES | | NULL | |
| qty_sup | int(11) | YES | | NULL | |
| prod_rate | int(11) | YES | | NULL | |
+-----------+---------+------+-----+---------+-------+

ASSIGNMENT 4
create tables with necessary columns with proper data types.establish integrity constriants and
relationships.
student(RollNo,Student_name,Class,Major);
 create table student(rno int,std_name varchar(255),class int,major varchar(255));
 alter table student add primary key(rno);

course(C_no,C_name,department);
 create table course (cno int,std_name varchar(255),dept varchar(255));
 alter table course add primary key(cno);

report(RollNo,C_no,Grade);
create table report(rno int,cno int,grade varchar(255));

marks(RollNo,Mark);
create table marks(rno int ,mark int);

department(Dept_no,Prof_name,dept_name);
 create table department(dept_no int,prof_name varchar(255),dept_name varchar(255));
 alter table report add foreign key(rno) references student(rno);
 alter table marks add foreign key(rno) references student(rno);
 alter table report add foreign key(cno) references course(cno);

insert the relavent data for all the tables.


 insert into student values(101,'vidya',13,'cs');
 insert into course values(1,'v','cs');
 insert into report values(101,1,'oa');
 insert into marks values(101,595);
 insert into department values(1,'varu','cs');

1. Display the Details of all Attributes from Student with Roll no R13
select * from student where rno=101;

2.Retrieve the name and corresponding course of the students.


select s.std_name,c.dept from student s,course c where c.cno=s.rno;

3. Select name of the student which got 750 marks.


select s.std_name,s.rno from student s,marks m where s.rno=m.rno and mark>750;

4. Retrieve the names of the student and class who are doing the course through by Prof.Prakash.
select s.std_name,s.class,d.dept_name from student s,department d where s.major=d.dept_name
and prof_name='prakash';

5. Retrieve all department names offering more than one course.


select c.c_name,count(c_name) from department d,course c where d.dept_name=c.dept_name group
by c_name;

6. Retrieve the student name and class of all students who could not get grade “A” in any of the course.
select s.std_name,s.class,r.grade from student s,report r where s.rno=r.rno and grade !='A';

7.Using Procedure retrieve the name of the student and sum of marks of that student .
DELIMITER //
create procedure stud()
select s.std_name,m.mark from student s,marks m where s.rno=m.rno;
end //

call stud();

mysql> show tables;


+--------------+
| Tables_in_a4 |
+--------------+
| course |
| department |
| marks |
| report |
| student |
+--------------+

desc course;
+----------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+--------------+------+-----+---------+-------+
| cno | int(11) | YES | | NULL | |
| std_name | varchar(255) | NO | PRI | | |
| dept | varchar(255) | YES | | NULL | |
+----------+--------------+------+-----+---------+-------+

desc marks;
+-------+---------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+---------+------+-----+---------+-------+
| rno | int(11) | YES | MUL | NULL | |
| mark | int(11) | YES | | NULL | |
+-------+---------+------+-----+---------+-------+

desc report;
+-------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+--------------+------+-----+---------+-------+
| rno | int(11) | YES | MUL | NULL | |
| cno | int(11) | YES | | NULL | |
| grade | varchar(255) | YES | | NULL | |
+-------+--------------+------+-----+---------+-------+

desc student;
+----------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+--------------+------+-----+---------+-------+
| rno | int(11) | NO | PRI | 0 | |
| std_name | varchar(255) | YES | | NULL | |
| class | int(11) | YES | | NULL | |
| major | varchar(255) | YES | | NULL | |
+----------+--------------+------+-----+---------+-------+

ASSIGNMENT 5

Create tables with necessary columns with proper data types. Establish integrity Constraints and
relationships.
Patient(pat_no,Date_admit,date_discharged,pat_name)
create table patient(pat_no int,pat_name varchar(255),date_admit date,date_discharged date);

Billed(bill_no,charge,item_no,pat_no)
create table billed(bill_no int,charge int,item_no int,pat_no int);

Item(description,item_no,normal_charge)
create table item(description varchar(255),item_no int,normal_charge int);

Room(room_location,extention, accomodation)
create table room(room_loc int,extension int,accomodation varchar(255));

Treat(pat_no,date_treated,treat_result,proce_no,phy_no)
create table treat(pat_no int,date_treated date,treat_result varchar(255),proce_no int,phy_no int);

procedures(proce_no, description)
create table procedures(proce_no int,description varchar(255));

Physician(phy_name,phy_phone,phy_no)
create table physician(phy_no int,phy_name varchar(255),phy_phone int);

 alter table patient add primary key(pat_no);


 alter table billed add primary key(item_no);
 alter table item add primary key(description);
 alter table procedures add primary key(proce_no);
 alter table physician add primary key(phy_no);
 alter table item add foreign key(item_no) references billed(item_no);
 alter table billed add foreign key(pat_no) references patient(pat_no);
 alter table treat add foreign key(pat_no) references patient(pat_no);
 alter table treat add foreign key(phy_no) references physician(phy_no);
 alter table treat add foreign key(proce_no) references procedures(proce_no);
 alter table procedures add foreign key(description) references item(description);
 alter table treat add foreign key(phy_no) references physician(phy_no);

insert tables
 insert into patient values(101,'gouri','2018-3-2','2018-4-3');
 insert into billed values(1,1000,1,103);
 insert into item values('tablets',1,500);
 insert into room values(10,2,'hospital');
 insert into procedures values(111,'tablets');
 insert into physician values(121,'vidyasagar',123);
 insert into treat values(101,'2018-3-2','cure',111,121);

1.get the pat_no,item_no,charge from for a specific pat_no.


select pat_no,item_no,charge from billed where pat_no=101;

2.display all columns and all rows from billed table.


select * from billed;

3.display all charges >Rs100 for pat_no=3.


select b.pat_no,b.charge,i.normal_charge from item i,billed b where i.item_no=b.item_no and
b.pat_no=103 and b.charge>100;

4.display all charges for either pat_no=2 or pat_no=5.


select b.pat_no,b.charge,i.normal_charge from item i,billed b where i.item_no=b.item_no group by
pat_no having b.pat_no=103 or pat_no=102;

5.count the number of times pat_no =3 has been charged for items.
select count(item_no) as 'total items' from billed where pat_no=103;

6.display number of distinct procedures performed on a patient.


select count(distinct pat_no) as 'total procedures' from treat;

7.display a calculated values such as the current charge and the amount that would be changed if the
charge were increased by 6% for all rows in the item table.
select pat_no,charge,charge*0.06+charge from billed;

8.list the patients hospitalized more then 6 days.


select pat_no,pat_name,date_admit,date_discharged from patient where
DATEDIFF(date_discharged,date_admit)>6;

9.list the total charges per patient for expensive medical item(charge> 1500 item).
select b.pat_no,i.normal_charge from item i,billed b where i.item_no=b.item_no and
normal_charge>=1500;

10.list the patient who had either Dr.vidyasagar,Dr.paray,Dr.Gupta as a Physican.


select p.pat_name,t.pat_no,P.phy_name from treat t,patient p,physician P where
t.phy_no=P.phy_no and p.pat_no=t.pat_no group by pat_no having P.phy_name='vidyasagar' or
P.phy_name='paray' or phy_name='vidyasagar' or phy_name='pari';

11.show the patient name(pat_name field),and associated physicians name(phy_name field)along with the
patient information.
select p.pat_name,t.pat_no,p.date_admit,p.date_discharged,ph.phy_name from patient p,physician
ph,treat t where ph.phy_no=t.phy_no and p.pat_no=t.pat_no group by pat_no;

12.list pat_no and date discharged from the patient table and the associated charge from the billed table.
select p.pat_no,p.date_discharged,b.charge from patient p,billed b where p.pat_no=b.pat_no;

13.using procedure display particular pat_no,pat_name with total amount.


delimiter |
create procedure patient1()
begin
select p.pat_no,p.pat_name,sum(distinct b.charge) from billed b,patient p where
b.pat_no=p.pat_no;
select p.pat_no,p.pat_name,sum(distinct b.charge) from billed b,patient p where b.pat_no=p.pat_no
group by pat_no;
end;|
call patient1();

TABLES

mysql>+--------------------+
| Tables_in_patient1 |
+--------------------+
| billed |
| item |
| patient |
| physician |
| procedures |
| room |
| treat |
+--------------------+

DESC billed;
+---------+---------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------+---------+------+-----+---------+-------+
| bill_no | int(11) | YES | | NULL | |
| charge | int(11) | YES | | NULL | |
| item_no | int(11) | NO | PRI | 0 | |
| pat_no | int(11) | YES | MUL | NULL | |
+---------+---------+------+-----+---------+-------+

desc item;
+---------------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------------+--------------+------+-----+---------+-------+
| description | varchar(255) | NO | PRI | | |
| item_no | int(11) | YES | MUL | NULL | |
| normal_charge | int(11) | YES | | NULL | |
+---------------+--------------+------+-----+---------+-------+

mysql> desc patient;


+-----------------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------------+--------------+------+-----+---------+-------+
| pat_no | int(11) | NO | PRI | 0 | |
| pat_name | varchar(255) | YES | | NULL | |
| date_admit | date | YES | | NULL | |
| date_discharged | date | YES | | NULL | |
+-----------------+--------------+------+-----+---------+-------+

desc physician;
+-----------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------+--------------+------+-----+---------+-------+
| phy_no | int(11) | NO | PRI | 0 | |
| phy_name | varchar(255) | YES | | NULL | |
| phy_phone | int(11) | YES | | NULL | |
+-----------+--------------+------+-----+---------+-------+

desc procedures;
+-------------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------------+--------------+------+-----+---------+-------+
| proce_no | int(11) | NO | PRI | 0 | |
| description | varchar(255) | YES | MUL | NULL | |

desc room;

+--------------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------------+--------------+------+-----+---------+-------+
| room_loc | int(11) | YES | | NULL | |
| extension | int(11) | YES | | NULL | |
| accomodation | varchar(255) | YES | | NULL | |
+--------------+--------------+------+-----+---------+-------+

mysql> desc treat;|


+--------------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------------+--------------+------+-----+---------+-------+
| pat_no | int(11) | YES | MUL | NULL | |
| date_treated | date | YES | | NULL | |
| treat_result | varchar(255) | YES | | NULL | |
| proce_no | int(11) | YES | MUL | NULL | |
| phy_no | int(11) | YES | MUL | NULL | |
+--------------+--------------+------+-----+---------+-------+

You might also like