Download as pdf or txt
Download as pdf or txt
You are on page 1of 36

2

CBSE LAB PRACTICAL PROGMAM

PART-A-PYTHON PROGRAMS
1. PROGRAM -1: Write the program to find the given string is
palindrome or not.

AIM:
To write the program to find the given string is palindrome or
not.
3
PROGRAM:

print("Palindrome checking program")


s=input("Enter the string :")
s1=s[::-1]
if s==s1:
print("The given string ",s,"is a palindrome")
else:
print("The given string ",s,"is not a palindrome")

OUTPUT1:

Palindrome checking program


Enter the string : madam
The given string madam is a palindrome

OUTPUT2:

Palindrome checking program


Enter the string :computer
The given string computer is not a palindrome

RESULT:
Thus the above program has been executed successfully and the
output is verified.

2. PROGRAM-2: Write a program to create a phone contact book and


also to do search,update and delete contact.

AIM:
To write a program to create a phone contact book and also to
do search,update and delete contact.
PROGRAM:

pb={}

def addcontact():
ans='y'
while ans=='y':
na=input("Enter name:")
no=int(input("Enter mobile number:"))
nc=str(no)
if len(nc)==10:
4
pb[na]=no
print("Contact saved successfully")
else:
print("Invalid number.Enter correct number")
continue
ans=input("Do you want to add another contact(y/n):")

def searchcontact():
na=input("Enter the name which you want to search:")
if na in pb:
print("Mobile number :",pb[na])
else:
print("Name not in contact book")

def updatecontact():
na=input("Enter the name which you want to update:")
if na in pb :
no=int(input("Enter the new number"))
nc=str(no)
if len(nc)==10:
pb[na]=no
print("Contact number updated")
else:
print("Invalid number.Contact not updated.")
else:
print("Name not in contact book")

def deletecontact():
na=input("Enter the name which you want to delete the contact:")
if na in pb:
del pb[na]
print("Contact deleted")
else:
print("Name not in contact book")
while True:
print("Phone contact book")
print("1-to add contact\n2-to search contact\n3-to update
contact\n4-to delete contact\n5-to quit")
ch=int(input("Enter your choice:"))
if ch==1:
addcontact()
elif ch==2:
searchcontact()
elif ch==3:
updatecontact()
5
elif ch==4:
deletecontact()
elif ch==5:
break
else:
print("Invalid choice.Give correct choice")
continue

OUTPUT:

Phone contact book


1-to add contact
2-to search contact
3-to update contact
4-to delete contact
5-to quit
Enter your choice:1
Enter name:anandh
Enter mobile number:7894561230
Contact saved successfully
Do you want to add another contact(y/n):y
Enter name:joy
Enter mobile number:9876543214
Contact saved successfully
Do you want to add another contact(y/n):n
Phone contact book
1- to add contact
2- to search contact
3-to update contact
4-to delete contact
5-to quit
Enter your choice:2
Enter the name which you want to search:joy
Mobile number : 9876543214
6
Phone contact book
1-to add contact
2-to search contact
3-to update contact
4-to delete contact
5-to quit
Enter your choice:3
Enter the name which you want to update:anandh
Enter the new number:9998567859
Contact number updated
Phone contact book
1- to add contact
2- to search contact
3-to update contact
4-to delete contact
5-to quit
Enter your choice:4
Enter the name which you want to delete the contact:joy
Contact deleted
Phone contact book
1-to add contact
2-to search contact
3-to update contact
4-to delete contact
5-to quit
Enter your choice:5
>>>

RESULT:
Thus the above program has been executed successfully and the
output is verified.

3. PROGRAM-3:Write a program to find the area of the shapes using


module.

AIM:
To write a program to find the area of the shapes using module.

PROGRAM:
7
MODULE CODE:MODULE NAME : AREA.PY

def square(a):
return a*a
def rectangle(l,b):
return l*b
def triangle(b,h):
return b*h
def circle(r):
return 3.14*r*r
def cube(a):
return 6*a**2
def cylinder(r,h):
return 2*3.14*r*(r+h)
def cone(r,h):
return 3.14*r*r+h
def sphere(r):
return 4*3.14*r**2

MAIN PROGRAM :

import area
print("Area of the shapes calculating program")
while True:
print("1-square\t2-rectangle\t3-triangle\t4-circle\n5-cube\t6-
cylinder\t7-cone\t8-sphere\t9-quit")
ch=int(input("Enter your choice:"))
if ch==1:
a=float(input("Enter the area value:"))
ar=area.square(a)
print("The area of the square is :",ar)
elif ch==2:
l=float(input("Enter the length value:"))
b=float(input("Enter the breadth value:"))
ar=area.rectangle(l,b)
print("The area of the rectangle is :",ar)
elif ch==3:
b=float(input("Enter the base value:"))
h=float(input("Enter the height value:"))
ar=area.triangle(b,h)
print("The area of the rectangle is :",ar)
elif ch==4:
a=float(input("Enter the radius value:"))
ar=area.circle(a)
print("The area of the circle is :",ar)
8
elif ch==5:
a=float(input("Enter the area value:"))
ar=area.cube(a)
print("The area of the cube is :",ar)
elif ch==6:
r=float(input("Enter the radius value:"))
h=float(input("Enter the height value:"))
ar=area.cylinder(r,h)
print("The area of the cylinder is :",ar)
elif ch==7:
r=float(input("Enter the radius value:"))
h=float(input("Enter the height value:"))
ar=area.cone(r,h)
print("The area of the cone is :",ar)
elif ch==8:
a=float(input("Enter the radius value:"))
ar=area.sphere(a)
print("The area of the sphere is :",ar)
elif ch==9:
break
else:
print("Invalid choice")
continue

OUTPUT:

Area of the shapes calculating program


1-square 2-rectangle 3-triangle 4-circle
5-cube 6-cylinder 7-cone 8-sphere 9-quit
Enter your choice:1
Enter the area value:7
The area of the square is : 49.0
1-square 2-rectangle 3-triangle 4-circle
5-cube 6-cylinder 7-cone 8-sphere 9-quit
Enter your choice:2
Enter the length value:8
Enter the breadth value:9
The area of the rectangle is : 72.0
1-square 2-rectangle 3-triangle 4-circle
5-cube 6-cylinder 7-cone 8-sphere 9-quit
Enter your choice:3
Enter the base value:5
Enter the height value:6
The area of the rectangle is : 30.0
1-square 2-rectangle 3-triangle 4-circle
9
5-cube 6-cylinder 7-cone 8-sphere 9-quit
Enter your choice:4
Enter the radius value:9
The area of the circle is : 254.34
1-square 2-rectangle 3-triangle 4-circle
5-cube 6-cylinder 7-cone 8-sphere 9-quit
Enter your choice:5
Enter the area value:8
The area of the cube is : 384.0
1-square 2-rectangle 3-triangle 4-circle
5-cube 6-cylinder 7-cone 8-sphere 9-quit
Enter your choice:9
>>>

RESULT:
Thus the above program has been executed successfully and the
output is verified.

4. PROGRAM -4 : Write a random number generator that generates


random numbers between 1 and 6 (simulates a dice).

AIM:
To write a random number generator that generates random
numbers between 1 and 6 (simulates a dice).

PROGRAM:

import random
print("Dice game ")
print("Game starts...")
ans='y'
while ans=='y':
print("Dice rolling ....... ")
s=random.randint(1,6)
print("You got:",s)
ans=input("Do you want to roll again the dice (y/n):")
print("See you again.. Bye ..... ")

OUTPUT:

Dice game
Game starts...
Dice rolling....
You got: 5
1
Do you want to roll again the dice0 (y/n):y
Dice rolling....
You got: 2
Do you want to roll again the dice (y/n):y
Dice rolling....
You got: 2
Do you want to roll again the dice (y/n):y
Dice rolling....
You got: 6
Do you want to roll again the dice (y/n):n
See you again.. Bye…

RESULT:
Thus the above program has been executed successfully and the
output is verified.

5. PROGRAM -5:Read a text file line by line and display each word
separated by a #.

AIM:
To read a text file line by line and display each word
separated by a #.

PROGRAM:

a=open('sample.txt','r')
b=a.readline()
while b:
c=b.split()
for i in range(len(c)-1):
print(c[i],'#',end='')
print(c[-1])
print()
b=a.readline()
a.close()

FILE CONTENT :

I am a computer science student.


I am learning python programming.
11
OUTPUT:

I #am #a #computer #science #student.

I #am #learning #python #programming.

RESULT:
Thus the above program has been executed successfully and the
output is verified.

6. PROGRAM-6 :Read a text file and display the number of


vowels/consonants/uppercase/lowercase characters in the file.

AIM:
To read a text file and display the number of
vowels/consonants/uppercase/lowercase characters in the file.

PROGRAM:

a=open('sample.txt','r')
b=a.read()
v=c=uc=lc=0
vowels=['a','e','i','o','u']
for i in b:
if i.lower() in vowels:
v+=1
else:
c+=1
if i.isupper():
uc+=1
elif i.islower():
lc+=1
print("The number of vowels in the file is :",v)
print("The number of consonants in the file is :",c)
print("The number of upper case letters in the file is :",uc)
print("The number of lower case letters in the file is :",lc)
a.close()

FILE CONTENT :

I am a computer science student.


I am learning python programming.
12
OUTPUT:

The number of vowels in the file is : 20


The number of consonants in the file is : 46
The number of upper case letters in the file is : 2
The number of lower case letters in the file is : 52

RESULT:
Thus the above program has been executed successfully and the
output is verified.

7. PROGRAM-7: Remove all the lines that contain the character 'a' in
a file and write it to another file.

AIM:
To remove all the lines that contain the character 'a' in a
file and write it to another file.

PROGRAM:

a=open('sample1.txt','r')
b=open('sample2.txt','w')
c=a.readlines()
for i in c:
if 'a' not in i:
b.write(i)
print()
a.close()
b.close()
print("File Content written into Sample2.txt which lines not
contains the letter 'a'")

OUTPUT:

File Content written into Sample2.txt which lines not contains the
letter 'a'

SAMPLE1.TXT FILE CONTENT:

I am a computer science student.


I am learning python programming.
Studying Python is very much useful in future.
13
SAMPLE2.TXT FILE CONTENT (Lines which not contains the letter ‘a’):
Studying Python is very much useful in future.

RESULT:
Thus the above program has been executed successfully and the
output is verified.

8. PROGRAM-8:Read a text file and count the number of occurrence of


the particular word in the file content.

AIM:
To read a text file and count the number of occurrence of the
particular word in the file content.

PROGRAM:

a=open("sample.txt",'r')
b=a.read()
c=b.split()
d=0
w=input("Enter the word which you want to count:")
for i in c:
if i.lower()==w:
d+=1
if d==0:
print("The given word",w,"not found in the file content")
else:
print("The given word",w,"found",d,"time(s) in the file
content")

OUTPUT:

Enter the word which you want to count:python


The given word python found 2 time(s) in the file content

Enter the word which you want to count:game


The given word game not found in the file content

RESULT:
Thus the above program has been executed successfully and the
output is verified.
14

9. PROGRAM -9 Create the binary file which should contains the


student details and to search the particular student based on roll
no and display the details.

AIM:
To create the binary file which should contains the student
details and to search the particular student based on roll no and
display the details.

PROGRAM:

import pickle

def addrec():
a=open("student.bin",'wb')
print("Add student details")
l=[]
ans='y'
while ans=='y':
r=int(input("Enter the roll no:"))
n=input("Enter name :")
m=float(input("Enter mark:"))
l.append([r,n,m])
ans=input("Do you want to add another record(y/n):")
pickle.dump(l,a)
print("Records stored successfully!.")

def searchrec():
a=open("student.bin",'rb')
r=int(input("Enter the roll no which you want to search:"))
b=pickle.load(a)
for i in b:
if r==i[0]:
print("Roll no:",i[0],"\nName:",i[1],"\nMark:",i[2])
break
else:
print("Entered Roll no not found in the file")

print("Student record search program")


addrec()
searchrec()
15
OUTPUT:

Student record search program


Add student details
Enter the roll no:1
Enter name :ramya
Enter mark:78
Do you want to add another record(y/n):y
Enter the roll no:2
Enter name :santhosh
Enter mark:89
Do you want to add another record(y/n):n
Records stored successfully!.
Enter the roll no which you want to search:2
Roll no: 2
Name: santhosh
Mark: 89.0

RESULT:
Thus the above program has been executed successfully and the
output is verified.

10. PROGRAM -10 Create the binary file which should contains the
student details and to update the particular student based on roll
no.

AIM:
To create the binary file which should contains the student
details and to update the particular student based on roll no.

PROGRAM:

import pickle

def addrec():
a=open("student.bin",'wb')
print("Add student details")
l=[]
ans='y'
while ans=='y':
r=int(input("Enter the roll no:"))
n=input("Enter name :")
m=float(input("Enter mark:"))
16
l.append([r,n,m])
ans=input("Do you want to add another record(y/n):")
pickle.dump(l,a)
print("Records stored successfully!.")

def updaterec():
a=open("student.bin",'rb')
r=int(input("Enter the roll no which you want to update:"))
b=pickle.load(a)
l=[]
for i in b:
l.append(i)
for i in l:
if r==i[0]:
nm=float(input("Enter new mark:"))
i[2]=nm
break
else:
print("Entered Roll no not found in the file")
a=open("student.bin",'wb')
pickle.dump(l,a)
print("Record updated successfully")
print("Student record update program")
addrec()
updaterec()

OUTPUT:

Student record update program


Add student details
Enter the roll no:1
Enter name :ashwathan
Enter mark:45
Do you want to add another record(y/n):y
Enter the roll no:2
Enter name :srinithi
Enter mark:87
Do you want to add another record(y/n):n
Records stored successfully!.
Enter the roll no which you want to update:1
Enter new mark:80
Record updated successfully
17
RESULT:
Thus the above program has been executed successfully and the
output is verified.

11. PROGRAM -11 Create the binary file which should contains the
student details and to delete the particular student based on roll
no.

AIM:
To create the binary file which should contains the student
details and to delete the particular student based on roll no.

PROGRAM:

import pickle

def addrec():
a=open("student.bin",'wb')
print("Add student details")
l=[]
ans='y'
while ans=='y':
r=int(input("Enter the roll no:"))
n=input("Enter name :")
m=float(input("Enter mark:"))
l.append([r,n,m])
ans=input("Do you want to add another record(y/n):")
pickle.dump(l,a)
print("Records stored successfully!.")

def deleterec():
a=open("student.bin",'rb')
r=int(input("Enter the roll no which you want to delete:"))
b=pickle.load(a)
l=[]
for i in b:
l.append(i)
for i in l:
if r==i[0]:
l.remove(i)
break
else:
print("Entered Roll no not found in the file")
18
a.close()
a=open("student.bin",'wb')
pickle.dump(l,a)
print("Roll no:",r," Record record successfully")
a.close()

print("Student record delete program")


addrec()
deleterec()

OUTPUT:

Student record delete program


Add student details
Enter the roll no:1
Enter name :manas
Enter mark:85
Do you want to add another record(y/n):y
Enter the roll no:2
Enter name :ram
Enter mark:56
Do you want to add another record(y/n):n
Records stored successfully!.
Enter the roll no which you want to delete:2
Roll no: 2 Record record successfully

RESULT:

Thus the above program has been executed successfully and the
output is verified.

12. PROGRAM -12 Create the csv file which should contains the
employee details and to search the particular employee based on emp
no and display the details.

AIM:
To create the csv file which should contains the employee
details and to search the particular employee based on emp no and
display the details.
19
PROGRAM:

import csv

def addrec():
with open('emp.csv','w',newline='') as a:
b=csv.writer(a)
l=[]
ans='y'
while ans=='y':
eno=int(input("Enter the emp no:"))
ename=input("Enter emp name :")
s=int(input("Enter emp salary:"))
l.append([eno,ename,s])
ans=input("Do you want to add another record(y/n):")
b.writerows(l)
print("record stored")

def searchrec():
l=[]
with open('emp.csv','r') as a:
b=csv.reader(a)
s=int(input('Enter the emp no which you want to search:'))
s=str(s)
for i in b:
l.append(i)
for i in l:
if i[0]==s:
print("Emp no:",i[0],'\nEmp name :',i[1],'\nEmp
salary:',i[2])
break
else:
print('no record found')

print("Employee details - search record program")


addrec()
searchrec()

OUTPUT:

Employee details - search record program


Enter the emp no:1001
Enter emp name :vasanth
Enter emp salary:10000
Do you want to add another record(y/n):y
20
Enter the emp no:1002
Enter emp name :sriram
Enter emp salary:20000
Do you want to add another record(y/n):y
Enter the emp no:1003
Enter emp name :keerthivasan
Enter emp salary:25000
Do you want to add another record(y/n):n
record stored
Enter the emp no which you want to search:1002
Emp no: 1002
Emp name : sriram
Emp salary: 20000

RESULT:

Thus the above program has been executed successfully and the
output is verified.

13. PROGRAM -13 Create the csv file which should contains the
employee details and to update their salary based on emp no .

AIM:
To create the csv file which should contains the employee
details and to update their salary based on emp no .

PROGRAM:

import csv

def addrec():
with open('emp.csv','w',newline='') as a:
b=csv.writer(a)
l=[]
ans='y'
while ans=='y':
eno=int(input("Enter the emp no:"))
ename=input("Enter emp name :")
s=int(input("Enter emp salary:"))
l.append([eno,ename,s])
ans=input("Do you want to add another record(y/n):")
b.writerows(l)
print("record stored")
21
def updaterec():
l=[]
with open('emp.csv','r') as a:
b=csv.reader(a)
for i in b:
l.append(i)
with open('emp.csv','w',newline='') as a:
b=csv.writer(a)
s=int(input('Enter the emp no which you want to update:'))
s=str(s)
for i in l:
if i[0]==s:
ns=int(input("Enter the revised salary:"))
i[2]=ns
b.writerows(l)
print("record updated")
break
else:
print('no record found')

print("Employee details - update record program")


addrec()
updaterec()

OUTPUT:

Employee details - update record program


Enter the emp no:1001
Enter emp name :vasanth
Enter emp salary:10000
Do you want to add another record(y/n):y
Enter the emp no:1002
Enter emp name :sriram
Enter emp salary:20000
Do you want to add another record(y/n):n
record stored
Enter the emp no which you want to update:1002
Enter the revised salary:25000
record updated

RESULT:
Thus the above program has been executed successfully and the
output is verified.
22
14. PROGRAM -14 Create the csv file which should contains the
employee details and to delete the particular record based on emp
no .

AIM:
To create the csv file which should contains the employee
details and to delete the particular record based on emp no .

PROGRAM:

import csv

def addrec():
with open('emp.csv','w',newline='') as a:
b=csv.writer(a)
l=[]
ans='y'
while ans=='y':
eno=int(input("Enter the emp no:"))
ename=input("Enter emp name :")
s=int(input("Enter emp salary:"))
l.append([eno,ename,s])
ans=input("Do you want to add another record(y/n):")
b.writerows(l)
print("record stored")

def deleterec():
l=[]
with open('emp.csv','r') as a:
b=csv.reader(a)
for i in b:
l.append(i)
with open('emp.csv','w',newline='') as a:
b=csv.writer(a)
s=int(input('Enter the emp no which you want to delete:'))
s=str(s)
for i in l:
if i[0]==s:
l.remove(i)
b.writerows(l)
print("record deleted")
break
else:
print('no record found')
23
print("Employee details - delete record program")
addrec()
deleterec()

OUTPUT:

Employee details - delete record program


Enter the emp no:1001
Enter emp name :karthick
Enter emp salary:20000
Do you want to add another record(y/n):y
Enter the emp no:1002
Enter emp name :santhosh
Enter emp salary:30000
Do you want to add another record(y/n):n
record stored
Enter the emp no which you want to delete:1002
record deleted

RESULT:
Thus the above program has been executed successfully and the
output is verified.

15. PROGRAM -15 Write a program to implement all stack operations


using list as stack.

AIM:
To write a program to implement all stack operations using list
as stack.

PROGRAM:

def PUSH(stk):
a=input('Enter a city:')
stk.append(a)

def POP(stk):
if len(stk)==0:
print('STACK UNDERFLOW')
else:
c=stk.pop()
print('The popped city is:',c)
24
def DISPLAY(stk):
if len(stk)==0:
print('STACK EMPTY')
else:
print(stk[-1],'<--TOP')
for i in range(len(stk)-2,-1,-1):
print(stk[i])

print('STACK IMPLEMENTATION PROGRAM')


stk=[]
ans='y'
while ans.lower()=='y':
print('1-PUSH\n2-POP\n3-DISPLAY\n4-EXIT')
ch=int(input('Enter your choice:'))
if ch==1:
PUSH(stk)
elif ch==2:
POP(stk)
elif ch==3:
DISPLAY(stk)
elif ch==4:
break
ans=input('Do you want to do any operation(y/n):')

OUTPUT:

STACK IMPLEMENTATION PROGRAM


1-PUSH
2- POP
3- DISPLAY
4- EXIT
Enter your choice:1
Enter a city:Banglore
Do you want to do any operation(y/n):y
1-PUSH
2- POP
3- DISPLAY
4- EXIT
Enter your choice:1
Enter a city:Delhi
Do you want to do any operation(y/n):y
1-PUSH
2- POP
3- DISPLAY
4- EXIT
24
Enter your choice:1
Enter a city:Chennai
Do you want to do any operation(y/n):y
1-PUSH
2- POP
3- DISPLAY
4- EXIT
Enter your choice:2
The popped city is: Chennai
Do you want to do any operation(y/n):y
1-PUSH
2- POP
3- DISPLAY
4- EXIT
Enter your choice:3
Delhi <--TOP
Banglore
Do you want to do any operation(y/n):y
1-PUSH
2- POP
3- DISPLAY
4- EXIT
Enter your choice:4
>>>

RESULT:
Thus the above program has been executed successfully and the
output is verified.

PART -B - MYSQL QUERIES

TABLE NAME : STUDENTS


EXAMNO NAME CLASS SEC MARK
1201 PAVINDHAN XII A1 489
1202 ESHWAR XII A1 465
1203 BHARKAVI XII A2 498
1204 HARIPRIYA XII A2 400
1205 JAI PRADHAP XII NULL 398
1206 KAVIPRIYA XII A2 NULL
25
SET- 1:

Q1.Write the query to create the above table and set the
constraints for the attributes.
EXAMNO-PRIMARY KEY,NAME- NOT NULL ,MARK CHECK-MARK SHOULD NOT
EXCEED 500.

QUERY:

CREATE TABLE STUDENTS (EXAMNO INT PRIMARY KEY,NAME VARCHAR(20) NOT


NULL,CLASS VARCHAR(5),SEC VARCHAR(3),MARK INT CHECK(MARK<=500));

Q2.Write the query to insert the mentioned records into the table.

QUERY:

INSERT INTO STUDENTS VALUES (1201,'PAVINDHAN','XII','A1',489),


(1202,'ESHWAR','XII','A1',465),
(1203,'BHARKAVI','XII','A2',498),(1204,'HARIPRIYA','XII','A2',400);

INSERT INTO STUDENTS (EXAMNO,NAME,CLASS,MARK)


VALUES(1205,'JAIPRADHAP','XII',398);

INSERT INTO STUDENTS (EXAMNO,NAME,CLASS,SEC)


VALUES(1206,'KAVIPRIYA','XII','A2');

Q3.Write the query to display all the student records who is


studying in A1 SECTION.

QUERY:

SELECT * FROM STUDENTS WHERE SEC='A1';

Q4.Write the query to display the records whose mark is not


assigned.

QUERY:

SELECT * FROM STUDENTS WHERE MARK IS NULL;

Q5.Write the query to display whose marks in the range 400 to 450
(both values are inclusive).

QUERY:
SELECT * FROM STUDENTS WHERE MARK BETWEEN 400 AND 450;
26
SET-2:

Q1.Write the query to display the student name,marks who secured


more than 400 and less than 487.

QUERY:

SELECT NAME,MARK FROM STUDENTS WHERE MARK>400 AND MARK<487;

Q2.Write the query to display the details whose name is starts with
‘P’ or ‘B’.

QUERY:

SELECT * FROM STUDENTS WHERE NAME LIKE 'P%' OR NAME LIKE 'B%';

Q3. Write the query to display the student name and section whose
name contain ‘priya’.

QUERY:

SELECT NAME,SEC FROM STUDENTS WHERE NAME LIKE '%PRIYA%';

Q4. Write the query to display all the details sort by name in
descending order.

QUERY:

SELECT * FROM STUDENTS ORDER BY NAME DESC;

Q5.Write the query to add 10 marks with the existing marks as


‘Updated marks’ and display the details.

QUERY:

SELECT EXAMNO,NAME,CLASS,SEC,MARK+10 AS 'Updated marks' FROM


STUDENTS;

SET - 3:

Q1.Write the query to add a new column named as CITY with the data
type VARCHAR(30) and apply the default constraint ‘NOT MENTIONED’
in the students table.
27
QUERY:

ALTER TABLE STUDENTS ADD COLUMN CITY VARCHAR(30) DEFAULT ("NOT


MENTIONED");

Q2.Write the query to change the order of the column in the


students table as :
EXAMNO,NAME,CITY,CLASS,SEC,MARK

QUERY:

ALTER TABLE STUDENTS MODIFY CITY VARCHAR(30) AFTER NAME;

Q3.Write the query to redefine the NAME field size into VARCHAR(40)
in the students table.

QUERY:

ALTER TABLE STUDENTS MODIFY NAME VARCHAR(40);

Q4.Write the query to update the mark as 350 whose marks is null
and update the section is A3 whose section is null.

QUERY:

UPDATE STUDENTS SET MARK=350 WHERE MARK IS NULL;

UPDATE STUDENTS SET SEC="A3" WHERE SEC IS NULL;

Q5. Write the query to update the city of all records with the
following cities
[CHENNAI,BENGALURU]

QUERY:

UPDATE STUDENTS SET CITY='CHENNAI' WHERE EXAMNO IN (1201,1202,1203);

UPDATE STUDENTS SET CITY='BENGALURU' WHERE EXAMNO IN


(1204,1205,1206) ;
28
TABLE NAME : DOCTOR
DOCID DNAME DEPT CITY SALARY
D01 ASHWATHAN ONCOLOGY CHENNAI 150000
D02 RAMYA GYNAECOLOGY COIMBATORE 140000
D03 SRIRAM DENTAL BHOPAL 180000
D04 SANJAY CARDIOLOGY HYDERABAD 160000
D05 SRINITHI PEDIATRICS DELHI 120000
D06 SANTHOSH PEDIATRICS BENGALURU 140000
D07 KARTHICK CARDIOLOGY JAIPUR 180000
D08 YASHIK GYNAECOLOGY AHMEDABAD 195000

TABLE NAME : PATIENT


PATID PNAME APMDATE GENDER AGE DOCID
P01 SATHISH 2022/08/19 MALE 45 D01
P02 SUPRIYA 2022/09/21 FEMALE 23 D02
P03 ARUNA 2022/10/20 FEMALE 43 D08
P04 RAMESH 2022/08/25 MALE 84 D04
P05 KUMAR 2022/09/23 MALE 12 D02
P06 PRIYA 2022/09/14 FEMALE 10 D06
P07 ROOPA 2022/08/13 FEMALE 66 D03
P08 CHARLES 2022/10/12 MALE 24 D07

SET-4:

Q1.Write the query to create the patient table and keep DOCID to be
the foreign key with update and delete cascade.

QUERY:

CREATE TABLE PATIENT(PATID VARCHAR(3) PRIMARY KEY,PNAME


VARCHAR(20),APMDATE VARCHAR(12),GENDER VARCHAR(7),AGE INT,DOCID
VARCHAR(3),FOREIGN KEY (DOCID) REFERENCES DOCTOR (DOCID) ON UPDATE
CASCADE ON DELETE CASCADE);

Q2.Write the query to display the count of male and female patients
in the age between 40 and 50.

QUERY:

SELECT GENDER,COUNT(GENDER) FROM PATIENT WHERE AGE BETWEEN 40 AND


50 GROUP BY GENDER;

Q3.Write the query to display the patient id,name and age who fixed
the appointment on September month.
29
QUERY:

SELECT PATID,PNAME,AGE FROM PATIENT WHERE MONTH(APMDATE)=09;

Q4.Write the query to display the number of doctors in each dept.

QUERY:

SELECT DEPT,COUNT(DOCID) FROM DOCTOR GROUP BY DEPT;

Q5.write the query to display the sum of the salary of the doctors
department wise.

QUERY:

SELECT DEPT,SUM(SALARY) FROM DOCTOR GROUP BY DEPT;

SET-5

Q1.Write the query to display patient name,doctor name,patient age


and their appointment date from the tables.

QUERY:

SELECT PNAME,DNAME,AGE,APMDATE FROM DOCTOR,PATIENT WHERE


DOCTOR.DOCID=PATIENT.DOCID;

Q2.Write the query to display the doctor id,doctor name and number
of patients need to visit.

QUERY:

SELECT D.DOCID,DNAME,COUNT(P.DOCID) AS 'NO.PATIENT' FROM DOCTOR


D,PATIENT P WHERE D.DOCID=P.DOCID GROUP BY P.DOCID;

Q3.Write the query to display the average salary of each dept from
doctor table.

QUERY:

SELECT DEPT,TRUNCATE(AVG(SALARY),2) AS 'AVERAGE SALARY' FROM DOCTOR


GROUP BY DEPT;
30
Q4.Write the query to display the maximum and minimum salary of
each department.

QUERY:

SELECT DEPT,MAX(SALARY),MIN(SALARY) FROM DOCTOR GROUP BY DEPT;

Q5.(i)Write the query to delete record of the doctor id ‘D08’ and


‘D06’ from the table .
(ii)write the output of the followings queries after executing
the above question query.
SELECT * FROM DOCTOR;
SELECT * FROM PATIENT;

QUERY:

i) DELETE FROM DOCTOR WHERE DOCID IN('D06','D08');

OUTPUT:
ii)
31

PART-C - PYTHON - MYSQL CONNECTIVITY PROGRAMS

1. PROGRAM 1-Write a python program to create the table and insert


the records into the table by getting the data from the user and to
store in the MySQL database.
DATABASE : MYSCHOOL
TABLE NAME : STUDENT
ATTRIBUTES : EXAMNO,NAME,CLASS,SECTION,MARK

AIM:
To write a python program to create the table and insert the
records into the table by getting the data from the user and to
store in the MySQL database.

PROGRAM :

import mysql.connector as my
a=my.connect(host='localhost',user='root',password='password',
database='MYSCHOOL')
if a.is_connected():
print('Database connected')
else:
print('Database not connected')
b=a.cursor()
q0='drop table if exists student'
b.execute(q0)
q="create table student(EXAMNO INT,NAME VARCHAR(20),CLASS
INT,SECTION CHAR(2),MARK INT)"
b.execute(q)
ans='y'
while ans.lower()=='y':
ex=int(input("ENTER EXAMNO:"))
na=input("ENTER NAME:")
cl=int(input("ENTER CLASS:"))
s=input("ENTER SECTION:")
m=int(input("ENTER MARK:"))
q1="insert into student values({},'{}',{},'{}',{})".format(ex,na,cl,s,m)
b.execute(q1)
print("Record successfully stored in the student table")
ans=input("Do you want to add another record(y/n):")
a.commit()
a.close()
32
OUTPUT:

Database connected
ENTER EXAMNO:101
ENTER NAME:SRINITHI
ENTER CLASS:12
ENTER SECTION:A1
ENTER MARK:77
Record successfully stored in the student table
Do you want to add another record(y/n):Y
ENTER EXAMNO:102
ENTER NAME:RAMYA
ENTER CLASS:12
ENTER SECTION:A2
ENTER MARK:78
Record successfully stored in the student table
Do you want to add another record(y/n):Y
ENTER EXAMNO:103
ENTER NAME:HEMA
ENTER CLASS:12
ENTER SECTION:A3
ENTER MARK:78
Record successfully stored in the student table
Do you want to add another record(y/n):Y
ENTER EXAMNO:104
ENTER NAME:THARANIYA
ENTER CLASS:12
ENTER SECTION:A1
ENTER MARK:76
Record successfully stored in the student table
Do you want to add another record(y/n):N
>>>

RESULT:
Thus the above program has been executed successfully and the
output is verified.

2. PROGRAM 2-Write a python program to search the records from the


table based on the examno and display the details of the student.
DATABASE : MYSCHOOL
TABLE NAME : STUDENT
33
AIM:
To write a python program to search the records from the table
based on the examno and display the details of the student.

PROGRAM :

import mysql.connector as my
a=my.connect(host='localhost',user='root',password='password',datab
ase='MYSCHOOL')
ans='y'
while ans.lower()=='y':
b=a.cursor()
r=int(input("ENTER THE EXAMNO YOU WANT TO SEARCH:"))
q1="select * from student where examno={}".format(r)
b.execute(q1)
e=b.fetchall()
d=b.rowcount
if d!=0:
for i in e:
print('EXAM NO:',i[0])
print('NAME :',i[1])
print('CLASS :',i[2])
print('SECTION :',i[3])
print('MARKS :',i[4])
else:
print('RECORD NOT FOUND')
ans=input('DO YOU WANT TO SEARCH ANOTHER RECORD (Y/N):')
a.close()

OUTPUT:

ENTER THE EXAMNO YOU WANT TO SEARCH:102


EXAM NO: 102
NAME : RAMYA
CLASS : 12
SECTION : A2
MARKS : 78
DO YOU WANT TO SEARCH ANOTHER RECORD (Y/N):y
ENTER THE EXAMNO YOU WANT TO SEARCH:105
RECORD NOT FOUND
DO YOU WANT TO SEARCH ANOTHER RECORD (Y/N):n
>>>
34
RESULT:
Thus the above program has been executed successfully and the
output is verified.

3. PROGRAM 3-Write a python program to update the student mark on


table based on the examno given by the user . If record not found
display the appropriate message.
DATABASE : MYSCHOOL
TABLE NAME : STUDENT

AIM:
To write a python program to update the student mark on table
based on the examno given by the user and if the record not found
display the appropriate message.

PROGRAM :

import mysql.connector as my
a=my.connect(host='localhost',user='root',password='password',datab
ase='MYSCHOOL')
ans='y'
while ans.lower()=='y':
b=a.cursor()
r=int(input("ENTER THE EXAMNO YOU WANT TO UPDATE:"))
q1="select * from student where examno={}".format(r)
b.execute(q1)
e=b.fetchall()
d=b.rowcount
if d!=0:
nm=int(input('ENTER THE NEW MARK:'))
q2='UPDATE STUDENT SET MARK={} WHERE EXAMNO={}'.format(nm,r)
b.execute(q2)
a.commit()
print('RECORD UPDATED SUCCESSFULLY WITH NEW MARK')
else:
print('RECORD NOT FOUND')
ans=input('DO YOU WANT TO UPDATE ANOTHER RECORD (Y/N):')
a.close()
35
OUTPUT:

ENTER THE EXAMNO YOU WANT TO UPDATE:102


ENTER THE NEW MARK:100
RECORD UPDATED SUCCESSFULLY WITH NEW MARK
DO YOU WANT TO UPDATE ANOTHER RECORD (Y/N):Y
ENTER THE EXAMNO YOU WANT TO UPDATE:105
RECORD NOT FOUND
DO YOU WANT TO UPDATE ANOTHER RECORD (Y/N):n
>>>

RESULT:
Thus the above program has been executed successfully and the
output is verified.

4. PROGRAM 4-Write a python program to delete the particular record


from the table based on the examno given by the user . If record
not found display the appropriate message.
DATABASE : MYSCHOOL
TABLE NAME : STUDENT

AIM:
To write a python program to delete the particular record from
the table based on the examno given by the user and if record not
found display the appropriate message.

PROGRAM :

import mysql.connector as my
a=my.connect(host='localhost',user='root',password='password',datab
ase='MYSCHOOL')
ans='y'
while ans.lower()=='y':
b=a.cursor()
r=int(input("ENTER THE EXAMNO YOU WANT TO DELETE:"))
q1="select * from student where examno={}".format(r)
b.execute(q1)
e=b.fetchall()
d=b.rowcount
if d!=0:
q2='DELETE FROM STUDENT WHERE EXAMNO={}'.format(r)
b.execute(q2)
a.commit()
36
print('RECORD DELETED SUCCESSFULLY')
else:
print('RECORD NOT FOUND')
ans=input('DO YOU WANT TO DELETE ANOTHER RECORD (Y/N):')
a.close()

OUTPUT:

ENTER THE EXAMNO YOU WANT TO DELETE:103


RECORD DELETED SUCCESSFULLY
DO YOU WANT TO DELETE ANOTHER RECORD (Y/N):Y
ENTER THE EXAMNO YOU WANT TO DELETE:103
RECORD NOT FOUND
DO YOU WANT TO DELETE ANOTHER RECORD (Y/N):N
>>>

RESULT:
Thus the above program has been executed successfully and the
output is verified.

ALL THE BEST!

You might also like