PR

You might also like

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

mysql -h 192.168.68.

252 -u 23BMIIT00102 -p 23Bmiit@00102

Practical set-1

1. Write a procedure that will display ‘MY FIRST PROCEDURE’ message.


=> create procedure sp_message() select 'MY FIRST PROCEDURE' MESSAGE;
mysql> call sp_message;
-> &
+--------------------+
| MESSAGE |
+--------------------+
| MY FIRST PROCEDURE |
+--------------------+
1 row in set (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

2. Write a procedure that will display ‘Hello world!’ as message1 and ‘’ This is my
first stored procedure‘’ as message2.
=> create procedure sp_two_message()
Begin
select 'Hello World' 1_Message , 'This is my first procedure' 2_Message;
end;
mysql> call sp_two_message;
-> &
+-------------+----------------------------+
| 1_Message | 2_Message |
+-------------+----------------------------+
| Hello World | This is my first procedure |
+-------------+----------------------------+
1 row in set (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

3. Write a procedure that will print following details.


Enrollment number: 101
Name : Rahul
DOB: 2001/01/01
Contact: 8000000000
City: Surat
=> create procedure sp_details()
Begin
select '101' Enrollment_number , 'Rahul' Name , '2001/01/01' DOB ,
'8000000000' Contact , 'Surat' City;
End;
mysql> call sp_details;
-> &
+-------------------+-------+------------+------------+-------+
| Enrollment_number | Name | DOB | Contact | City |
+-------------------+-------+------------+------------+-------+
| 101 | Rahul | 2001/01/01 | 8000000000 | Surat |
+-------------------+-------+------------+------------+-------+
1 row in set (0.00 sec)

Query OK, 0 rows affected (0.00 sec)


4.creation of table.
=> create table Student
(
sid int primary key,
sname varchar(20),
scity varchar(20),
contact bigint,
gender varchar(10),
total_marks int
);
Query OK, 0 rows affected (0.30 sec)

➢ INSERT ATLEAST FIVE RECORDS WITH THE HELP OF STORED PROCEDURE.


=> create procedure sp_insert()
Begin
insert into Student values (101,'Rahul','Surat',8000000000,'Male',380),
(102,'Manthan','Surat',9898187393,'Male',430),
(103,'Kartik','Navsari',9638527410,'Male',350),
(104,'Dharmil','Bardoli',7410852963,'Male',380),
(105,'Janki','Surat',9909145893,'Female',400);
End;&
mysql> call sp_insert;&
Query OK, 5 rows affected (0.03 sec)

➢ Write a procedure that will display student names.


=> create procedure sp_name()
Begin
select sname from Student;
End;&
mysql> call sp_name;&
+---------+
| sname |
+---------+
| Rahul |
| Manthan |
| Kartik |
| Dharmil |
| Janki |
+---------+
5 rows in set (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

➢ Write a procedure that will display student name, city and contact number whose
student id is 5.
=> create procedure sp_stud_detail()
Begin
select sname,scity,contact from Student where sid=105;
End;&
mysql> call sp_stud_detail;&
+-------+-------+------------+
| sname | scity | contact |
+-------+-------+------------+
| Janki | Surat | 9909145893 |
+-------+-------+------------+
1 row in set (0.00 sec)
Query OK, 0 rows affected (0.00 sec)

➢ Write a procedure that will display all the details of students who belong to
‘Surat’ city.
=> create procedure sp_all()
Begin
select * from Student where scity='Surat';
End;&
mysql> call sp_all;&
+-----+---------+-------+------------+--------+-------------+
| sid | sname | scity | contact | gender | total_marks |
+-----+---------+-------+------------+--------+-------------+
| 101 | Rahul | Surat | 8000000000 | Male | 380 |
| 102 | Manthan | Surat | 9898187393 | Male | 430 |
| 105 | Janki | Surat | 9909145893 | Female | 400 |
+-----+---------+-------+------------+--------+-------------+
3 rows in set (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

➢ Write a procedure that will count total number of students whose marks is more
than 50.
=> create procedure sp_number()
Begin
select count(sname) from Student where total_marks>380;
End;&
mysql> call sp_number;&
+--------------+
| count(sname) |
+--------------+
| 2 |
+--------------+
1 row in set (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

➢ Write a procedure that will display highest and lowest marks of students along
with student name.
=> create procedure sp_max()
Begin
select max(total_marks),min(total_marks) from student;
End;&
mysql> call sp_max;&
+------------------+------------------+
| max(total_marks) | min(total_marks) |
+------------------+------------------+
| 430 | 350 |
+------------------+------------------+
1 row in set (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

➢ Write a procedure that will display student name whose marks is between 20 and
25.
=> create procedure sp_mid()
Begin
select sname from Student where total_marks between 380 and 440;

End;&
mysql> call sp_mid;&
+---------+
| sname |
+---------+
| Rahul |
| Manthan |
| Dharmil |
| Janki |
+---------+
4 rows in set (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

5.
create table emp_master
(
eid int primary key auto_increment,
ename varchar(20) not null,
gender varchar(10) not null,
department varchar(20) not null,
contact bigint not null,
salary int not null,
city varchar(20)not null
);&

➢ Insert 10 records.
=> create procedure sp_record()
Begin
insert into emp_master values
(101,'Manthan','Male','IT',9898187393,50000,'Surat'),
(102,'Janki','Female','HR',9909145893,45000,'Surat'),

(103,'Yug','Male','Marketing',9173063168,40000,'Bardoli'),

(104,'Nupur','Female','IT',9638527410,35000,'Bardoli'),
(105,'Vishmay','Male','HR',8000516155,30000,'Surat'),

(106,'Vishwa','Female','Marketing',7419638520,35000,'Surat'),
(107,'Preet','Male','IT',8520741963,40000,'Vapi'),
(108,'Henny','Female','HR',7894561230,45000,'Vapi'),

(109,'Rahul','Male','Marketing',9517538426,50000,'Valsad'),
(110,'Isha','Female','IT',7538426120,45000,'Valsad');
End;&

➢ Retrieve all the records of employee.


=> create procedure display()
begin
select * from emp_master;
end;&
mysql> call display;&
+-----+---------+--------+------------+------------+--------+---------+
| eid | ename | gender | department | contact | salary | city |
+-----+---------+--------+------------+------------+--------+---------+
| 101 | Manthan | Male | IT | 9898187393 | 50000 | Surat |
| 102 | Janki | Female | HR | 9909145893 | 45000 | Surat |
| 103 | Yug | Male | Marketing | 9173063168 | 40000 | Bardoli |
| 104 | Nupur | Female | IT | 9638527410 | 35000 | Bardoli |
| 105 | Vishmay | Male | HR | 8000516155 | 30000 | Surat |
| 106 | Vishwa | Female | Marketing | 7419638520 | 35000 | Surat |
| 107 | Preet | Male | IT | 8520741963 | 40000 | Vapi |
| 108 | Henny | Female | HR | 7894561230 | 45000 | Vapi |
| 109 | Rahul | Male | Marketing | 9517538426 | 50000 | Valsad |
| 110 | Isha | Female | IT | 7538426120 | 45000 | Valsad |
+-----+---------+--------+------------+------------+--------+---------+
10 rows in set (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

➢ Manager wants to know the employee who are coming from Surat city
=> create procedure surat()
begin
select ename from emp_master where city='Surat';
end;&
mysql> call surat;&
+---------+
| ename |
+---------+
| Manthan |
| Janki |
| Vishmay |
| Vishwa |
+---------+
4 rows in set (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

➢ List all records of employee whose salary is grater than 30,000.


=> create procedure salary()
begin
select * from emp_master where salary > 30000;
end;&
mysql> call salary;&
+-----+---------+--------+------------+------------+--------+---------+
| eid | ename | gender | department | contact | salary | city |
+-----+---------+--------+------------+------------+--------+---------+
| 101 | Manthan | Male | IT | 9898187393 | 50000 | Surat |
| 102 | Janki | Female | HR | 9909145893 | 45000 | Surat |
| 103 | Yug | Male | Marketing | 9173063168 | 40000 | Bardoli |
| 104 | Nupur | Female | IT | 9638527410 | 35000 | Bardoli |
| 106 | Vishwa | Female | Marketing | 7419638520 | 35000 | Surat |
| 107 | Preet | Male | IT | 8520741963 | 40000 | Vapi |
| 108 | Henny | Female | HR | 7894561230 | 45000 | Vapi |
| 109 | Rahul | Male | Marketing | 9517538426 | 50000 | Valsad |
| 110 | Isha | Female | IT | 7538426120 | 45000 | Valsad |
+-----+---------+--------+------------+------------+--------+---------+
9 rows in set (0.00 sec)

Query OK, 0 rows affected (0.00 sec)


➢ Display total number of working employees.
=> create procedure work()
begin
select count(ename) from emp_master;
end;&
mysql> call work;&
+--------------+
| count(ename) |
+--------------+
| 10 |
+--------------+
1 row in set (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

➢ Display employee details with lowest salary from IT department.


=> create procedure IT()
begin
select min(salary) from emp_master where department='IT';
end;&
mysql> call IT;&
+-------------+
| min(salary) |
+-------------+
| 35000 |
+-------------+
1 row in set (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

➢ Display employee details with highest salary from Marketing department.


=> create procedure MD()
begin
select max(salary) from emp_master where department='Marketing';
end;&
mysql> call MD;&
+-------------+
| max(salary) |
+-------------+
| 50000 |
+-------------+
1 row in set (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

➢ Display total number of employees from each city.


=> create procedure total()
begin
select count(ename) as Surat from emp_master where city='Surat',
select count(ename) as Bardoli from emp_master where city='Bardoli',
select count(ename) as Vapi from emp_master where city='Vapi',
select count(ename) as Valsad from emp_master where city='Valsad';
end;&

You might also like