0% found this document useful (0 votes)
7 views

PRACTICAL TERM 2

Uploaded by

geetha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

PRACTICAL TERM 2

Uploaded by

geetha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

PRACTICAL-16

Stack Implementation
AIM- Write a program to do push and pop operations on a stack using a list.

PROGRAM:
s = []
def Peek(s):
if len(s)==0:
return "Underflow"
else:
top=len(s)-1
return s[top]
def push():
b_ID=int(input('Enter book no.'))
b_NAME=input('Enter book name')
b_PRICE=float(input('Enter price'))
data=b_ID,b_NAME,b_PRICE
s.append(data)
print("Book added to stack")
def pop():
if len(s)==0:
print("Underflow")
else:
dn = s.pop()
print("The deleted book details: ",dn)
def disp():
if len(s)==0:
print("Stack is empty")

else:
for i in range(len(s)-1,-1,-1):
print("Book Id :",s[i][0])
print("Book Name :",s[i][1])
print("Book Price :",s[i][2])
print()
while(1):
print('''Menu

1.Push
2.Pop
3.Peek
4.Display
5.Exit''')
ch=int(input('Enter choice:'))
if ch==1:
push()
elif ch==2:
pop()
elif ch==3:
val = Peek(s)
if val=="Underflow":
print("Stack is empty")
else:
print("Top Item :",val)
elif ch==4:
disp()
elif ch==5:
break
else:
print('Invalid Input')

OUTPUT:
Menu

1. Push
2. Pop
3. Peek
4. Display
5. Exit
Enter choice:1
Enter book no.1201
Enter book name Python Programming
Enter price580
Book added to stack
Menu

1. Push
2. Pop
3. Peek
4. Display
5. Exit
Enter choice:1
Enter book no.1205
Enter book nameStatistics
Enter price480
Book added to stack
Menu

1. Push
2. Pop
3. Peek
4. Display
5. Exit
Enter choice:4
Book Id : 1205
Book Name : Statistics
Book Price : 480.0

Book Id : 1201
Book Name : Python Programming
Book Price : 580.0

Menu

1. Push
2. Pop
3. Peek
4. Display
5. Exit
Enter choice:3
Top Item : (1205, 'Statistics', 480.0)
Menu

1. Push
2. Pop
3. Peek
4. Display
5. Exit
Enter choice:2
The deleted book details: (1205, 'Statistics', 480.0)
Menu

1. Push
2. Pop
3. Peek
4. Display
5. Exit
Enter choice:5

RESULT:
Thus the given program has been written and executed successfully.
SQL QUERIES
PRACTICAL-17

SIMPLE QUERIES

AIM- Write SQL commands to create a student table and insert


data. Implement the following SQL commands on the student
table:
ALTER table - to add new attributes / modify data type / drop
attribute

UPDATE- to modify data.

SELECT - To display data in ascending / descending order

DELETE - to remove tuple(s)

Use GROUP BY to find the min, max, sum, count and average functions

1. To create the given table


Create table stud_details( Rollno int(6) primary key, Sname varchar(25) not
null, Class char(5), Gender char(2), Dob date, address varchar(50), contact
int);
2. To insert the records
Insert into stud_details values(1201,’Arya’,’12B’,’M’,’2003-02-
10’,’Mylapore’,98404441),(1202,’Bavya’,’12B’,’F’,’2003-09-
15’,’T.Nagar’,984042410);
3. To display all the records from student details table
mysql>select * from stud_details;

4. To display Rollno, student name and their Date of Birth from the table
mysql>select Rollno,Sname,Dob from stud_details;

5. To display only girls record


mysql>select * from stud_details where Gender='F';

6. To display the location of the students without duplicate entries


mysql>select distinct address from stud_details;

7. To display the student’s name whose name start with ‘s’ letter
mysql>select sname from stud_details where sname like "s%";

8. To display the records order by roll number in descending


mysql>select * from stud_details order by rollno DESC;

9. To display number of records in a table


mysql>select count(*) Total from stud_details;

10. To add new attribute Aadhar in a student details table


mysql>Alter table stud_details add(Aadhar integer);

11. To update aadhar value of the students


mysql>Update stud_details set Aadhar=876874563455
where rollno=1201;

12. To count how many students are coming from particular area
mysql>Select address, count(sname) from stud_details
group by address;

13. To delete the column address


mysql>Alter table stud_details drop address;

14. To delete the student Sonia record from stud_details table


mysql>delete from stud_details where Sname=’Sonia’;

15. To change the datatype char to varchar of class attribute.


mysql>Alter table stud_details modify class varchar(5);
PRACTICAL-18

GROUPING TABLES AND JOINS

AIM- Write a program to implement the following SQL commands


using Employee table and Job_details table.

1. To display the department, number of employees in each department from


job_details.
Mysql>select department,count(*) from job_details group by
department;

2. To display the lowest salary of the employees department wise.


Mysql>select department, min(salary) from job_details group by
department;

3. To display the count of particular dept_id.


Mysql>select dept_id, count(*) from job_details group by
department having dept_id='102';
4. To display the job name and sum of the employee’s salary grouped by job
from job details.
Mysql>select job, sum(salary) from job_details group by job;

5. To display the total number of employee grouped by department having sum


of their salary >50000.
Mysql>select department, count(empno) from job_details group
by department having sum(salary)>50000;

6. To display employee number, name, job and salary from the tables
employee and job_details with order the rows by employee names.
Mysql> select employee.empno, employee.name, job_details.job,
job_details.salary from employee,job_details where
Employee.empno=job_details.empno order by name;

7. To display only for sales department employee details like number,


name, job, salary, department, department id.
Mysql> select employee.empno, employee.name, job.salary,
department,dept_id from employee,job_details where
Employee.empno=job_details.empno and department="Sales";

8. To display the details of the employee who are coming from


Adyar, Egmore and R.A.Puram.
Mysql> select employee.Name,Address,Job from
employee,job_details where employee.empno=job_details.empno
and address in('Adyar','Egmore','R.A.Puram');

9. To display the employee details sorted by their salary in descending


order.
Mysql>select employee.Name,Job,Department,Salary from
employee,job_details where employee.empno=job_details.empno
order by salary desc;

10. To display the name and department of the employee who have joined in
2006.
MySql> select employee.name,department from
employee,job_details where employee.empno =
job_details.empno and dateofapp between '2006-01-01' and
'2006-12-31';
PRACTICAL-19
DISPLAY STUDENT DETAILS

AIM- Write a program to Integrate SQL with Python by importing the


MySQL module and display the records of Student details.

Program

import mysql.connector
mycon=mysql.connector.connect(host="localhost",user="root",passwd="scott",
database="student")
if mycon.is_connected():
print("successfully connected")
cursor=mycon.cursor()
cursor.execute("Select * from student_details")
data=cursor.fetchall()
for row in data:
print(row)
mycon.close()

Output:

(1001,”Aswin”,’M’,”2007-08-13”,”Nerkundram”)
(1002,”Barath”,’M’,”2007-10-23”,”Madhuravoyal”)
(1003,”Janani”,’F’,”2007-11-05”,”Koyambedu”)
(1004,”Swathi”,’F’,”2008-06-30”,”Mogappair”)
(1005,”Yaswanth”,’F’,”2007-07-25”,”Nerkundram”)

Result:
Thus the given program has been executed successfully.
PRACTICAL- 20

SEARCHING A RECORD

Aim:

Program to connect with database and search employee number in


table employee and display record, if empno not found display
appropriate message.

Program

import mysql.connector as mycon


con =mycon.connect(host="localhost", user="root", passwd="scott",
database="student")
cur = con.cursor()
print("#"*40)
print("EMPLOYEE SEARCHING FORM")
print("#"*40)
print("\n\n")
ans='y'
while ans.lower()=='y':
eno=int(input("ENTER EMPNO TO SEARCH :"))
query="select * from employee_details where Eno={}".format(eno)
cur.execute(query)
result = cur.fetchall()
if cur.rowcount==0:
print("Sorry! Empno not found ")
else:
print("%10s"%"ENO", "%20s"%"ENAME","%15s"%"DEPARTMENT",
"%10s"%"SALARY")
for row in result:
print("%10s"%row[0],"%20s"%row[1],"%15s"%row[2],"%10s"%row[3])
ans=input("SEARCH MORE (Y) :")
Output:

########################################
EMPLOYEE SEARCHING FORM
########################################

ENTER EMPNO TO SEARCH :1


EMPNO NAME DEPARTMENT SALARY
1 AMIT SALES 9000

SEARCH MORE (Y) :y


ENTER EMPNO TO SEARCH :2
EMPNO NAME DEPARTMENT SALARY
2 NITIN IT 80000

SEARCH MORE (Y) :y


ENTER EMPNO TO SEARCH :4
Sorry! Empno not found SEARCH MORE (Y) :n

Result:
Thus the given program has been executed successfully.
PRACTICAL- 21
UPDATING THE RECORD

AIM- Write a program to Integrate SQL with Python by


importing the MySQL module to search a student using
roll number to delete the record.

PROGRAM:

import mysql.connector as mycon


con = mycon.connect (host="localhost", user="root", passwd="scott",
database="student")
cur = con.cursor()
roll=int(input("Enter roll number:"))
print("Before Updating record:")
query1='select * from stud_details'
cur.execute(query1)
record=cur.fetchall()
for row in record:
print(row)

print("Deleted attribute:")
query2='select * from stud_details where Rollno={}'.format(roll)
cur.execute(query2)
record=cur.fetchall()
for row in record:
print(row)
query3='delete from stud_details where Rollno={}'.format(roll)
cur.execute(query3)
con.commit()
record=cur.fetchone()

print("After Updating record:")


query4='select * from stud_details'
cur.execute(query4)
record=cur.fetchall()
for row in record:
print(row)

Output:

Enter roll number: 5


Before deleting record:
[(1,’Akil’,’XII’,’A’,’Science’,’Computer Science’,81.0), (2,’Satya’, ‘XII’,’C’,
‘Science’,’Biology’,87.0),(3,’Antony’,’XII’,’D’,’Commerce’,’Maths’,92.0), (4,
‘Vishal’, ‘XII’,’E’, ‘Humanities’,’NA’, 69.0), (6, ‘Brij’, ‘XII’, ‘B’, ‘Science’,
‘Computer Science’, 72.0), (5,’Deepak’, ‘XII’,’A’, ‘Science’, ‘Computer Science’,
85.0)]

Deleted attribute:
(5,’Deepak’, ‘XII’,’A’, ‘Science’, ‘Computer Science’, 85.0)
After deleting attribute:
[(1,’Akil’,’XII’,’A’,’Science’,’Computer Science’,81.0), (2,’Satya’, ‘XII’,’C’,
‘Science’,’Biology’,87.0),(3,’Antony’,’XII’,’D’,’Commerce’,’Maths’,92.0), (4,
‘Vishal’, ‘XII’,’E’, ‘Humanities’,’NA’, 69.0), (6, ‘Brij’, ‘XII’, ‘B’, ‘Science’,
‘Computer Science’, 72.0)]

Result:
Thus the given program has been executed successfully.

You might also like