Velammal Vidyalaya Practical Programs-Term2 Class XII-Computer Science 2022-2023

You might also like

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

Velammal Vidyalaya

Practical Programs-Term2
Class XII-Computer Science
2022-2023

Program 4-MYSQL 1

Aim: To create a table student and insert values into the table using mysql.

Query: Create table student(

Roll_no integer,

Stuname char(20),

Parent_name char(30),

Class integer,

Address varchar(40));

1. insert into student values(101,'Ram','Raj',12,'Nolambur,chennai')


2. insert into student values(102,'Priya','Veera',12,'AnnaNagar,chennai')
3. insert into student values(103,'Rajesh','Prasad',12,'West Mogappair,chennai')
4. insert into student values(104,'Riya','Palani',12,'East Mogappair,chennai')
5. insert into student values(105,'Bala','Natraj',12,'Perambur,chennai')
6. insert into student values(106,'John','Wilson',12,'Porur,chennai')

TABLE : Student

Roll_no Stuname Parent_name Class Address

101 Ram Raj 12 Nolambur,chennai

102 Priya Veera 12 Anna Nagar,chennai

103 Rajesh Prasad 12 West Mogappair,chennai

104 Riya Palani 12 East Mogappair,chennai

105 Bala Natraj 12 Perambur,chennai

106 John Wilson 12 Porur,chennai


Program 4-MYSQL 2

Aim: To create a table stud_data and insert values into the table using MYSQL.

Query: create table stud_data(

Roll_no integer,

Name char(20),

Marks float(6,2),

Grade char(1),

Section char(2));

1. insert into stud_data values(101,'Ram',76.80,'A','A');

2. insert into stud_data values(102,'Priya',71.20,'B','A');

3. insert into stud_data values(103,'Rajesh',81.20,'A','B');

4. insert into stud_data values(104,'Riya',61.20,'B','C');

5. insert into stud_data values(105,'Bala',51.60,'C','C');

6. insert into stud_data values(106,'Sam',83.60,'A','A');

TABLE : stud_data

Roll_no Name Marks Grade Section

101 Ram 76.80 A A

102 Priya 71.20 B A

103 Rajesh 81.20 A B

104 Riya 61.20 C C

105 Bala 51.60 C C

106 John 83.60 A A


Program 4-MYSQL 3

Aim: To execute the MYSQL commands for the following questions using the table student.

1. To add new columns into student table.


2. To modify any column datatype of a student table.
3. Drop any column of a student table.
4. Update values into a student table.
5. Display the marks of the student by descending order and section by
ascending order.

Query

1. a) Alter table student add Gender varchar(20);

b) Alter table student add Stud_id integer

Output

The new columns are added to the student table.

2. Alter table student modify Gender char(1);

Output: Gender datatype modified.

3. Alter table student drop column Stud_id;

Output: Stud_id column in student table dropped.

4. Update student set stuname='Sam' where Roll_no=106;

Output: Record updated.

5. select * from stud_data order by Section asc ,marks desc;


4. Output

Roll_no Stuname Parent_name Class Address Gender

101 Ram Raj 12 Nolambur,ch M


ennai

102 Priya Veera 12 Anna Nagar, F


chennai

103 Rajesh Prasad 12 West M


Mogappair,
chennai

104 Riya Palani 12 East M


Mogappair,
chennai

105 Bala Natraj 12 Perambur, M


chennai

106 Sam Wilson 12 Porur, M


chennai

5. Output

Roll_no Stuname Marks Grade Section

83.6 A A
106 Sam

76.8 A A
101 Ram

71.2 B A
102 Priya

81.2 A B
103 Rajesh

61.2 B C
104 Riya

51.6 C C
105 Bala
Program 4-MYSQL 4

Aim: To execute the MYSQL commands for the following questions using the table
stud_data.

1. To delete any records from the student table.


2. Display the total number of students in each section
3. Display the maximum, minimum and average marks of each section.
4. To display the names of students starting with ‘R’.
5. To display roll_no,name,class,address,marks,grade from student and stud_data table.

Query:

1. Delete from stud_data where Roll_no=102;

Output: 1 row is deleted.

2. select section,count(*) from stud_data group by Section;

Output:

section count(*)

A 3

B 1

C 2

3. select Section, max(Marks) as “Maximum”,min(Marks) as “Minimum”,avg(Marks) as “Average” from stud_data


group by Section;

Output:

Section Maximum Minimum Average

A 83.6 71.2 77.19

B 81.2 81.2 81.19

C 61.2 51.6 56.39

4. select Stuname from student where Stuname like ‘R%’;

Output:
Stuname

Rajesh

Riya

5. select Roll_No,Stuname,Class,Address,Marks,Grade from student,stud_data


where student.Roll_no=stud_data.Roll_no;

Output:

Roll_n Stuna Class Address Marks Grade


o me

101 Ram 12 Nolambur,ch 76.80 A


ennai

102 Priya 12 Anna Nagar, 71.20 B


chennai

103 Rajes 12 West 81.20 A


h Mogappair,c
hennai

104 Priya 12 East 61.20 C


Mogappair,c
hennai

105 Bala 12 Perambur,ch 51.60 C


ennai

106 Sam 12 Porur,chenn 83.60 A


ai
Program 5-MYSQL 1

Aim: To create table for teacher and execute the given commands using MYSQL.

Query:

create table teacher(

No integer(2),

Name varchar(15),

Age int(3) ,

Department varchar(15),

Dateofjoin varchar(15) ,

Salary int(7) ,

Sex char(1));

1. insert into teacher values(1,’jishnu’,34,’Computer’,’10/01/97’,12000,’M’);


2. insert into teacher values(2,’Sharmila’,31,’History’,’24/03/98’,20000,’F’);
3. insert into teacher values(3,’Santhosh’,32,’Maths’,’12/12/96’,30000,’M’);
4. insert into teacher values(4,’Sanmathi’,35,’History’,’1/07/99’,40000,’F’);
5. insert into teacher values(5,’Ragu’,42,’Maths’,’5/09/97’,25000,’M’);
6. insert into teacher values(6,’Shiva’,50,’History’,’27/02/97’,30000,’M’);
7. insert into teacher values(7,’Shakthi’,44,’Computer’,’25/02/97’,21000,’M’);
8. insert into teacher values(8,’Shriya’,33,’Maths’,’31/07/97’,20000,’M’);
TABLE: TEACHER
Program 5-MYSQL 2

Aim: To execute the MYSQL commands for the following questions using the table
Teacher.

1. To show all information about the teacher of history department.


2. To list the names of female teacher who are in Maths department.
3. To list names of all teachers with their date of joining in ascending

order.

4. To count the number of teachers with age>35.


5. To count the number of teachers department wise.

Query:

1. select * from teacher where Department=’History’;

Output:

No Name Age Department Dateofjoin Salary Sex

2 Sharmila 31 History 24/03/98 20000 F

4 Shanmathi 35 History 01/07/99 30000 F

6 Shiva 50 History 27/02/97 40000 M

2. select Name from teacher where Department=’Maths’ and Sex=’F’;

Output:

Name

Shriya

3.select Name,Dateofjoin from teacher order by Dateofjoin asc; ;

Output:

Name Dateofjoin

Santhosh 12/12/96
Jishnu 10/01/97

Shakthi 25/02/97

Shiva 27/02/97

Shriya 31/07/97

Ragu 05/09/97

Sharmila 24/03/98

Shanmathi 01/07/99

4.select count(*) from teacher where Age>35;

Output:

count(*)

5. select Department , count(*) from teacher group by department;

Output:

Department count(*)

Computer 2

History 3

Maths 3
Program 6

Aim: To integrate SQL with Python by importing the MySQL module to search an
employee using empno and delete the record.

Program Code:

import mysql.connector as mc
mycon=mc.connect(host='localhost',user='root',password='root',database='test')
if mycon.is_connected()==False:
print("Error in connection")
else:
print("Connected")
eno=int(input("Enter number:"))
cursor=mycon.cursor()
cursor.execute("select distinct(empno) from emp")
allrow=cursor.fetchall()
for row in allrow:
if row[0]==eno:
cursor.execute("delete from emp where empno={}".format(eno))
cursor.execute("select * from emp")
data=cursor.fetchall()
mycon.commit()
for row in data:
print(row)
mycon.close()

Output

Connected

Enter num:103

(101, 'Sai', 23, 23000)

(104, 'Priya', 24, 30000)

(105, 'Sruthi', 22, 33000)


Program 7

Aim: To integrate SQL with Python by importing the MySQL module to display name started with n.

Program Code:

import mysql.connector as sqltor

mycon=sqltor.connect(host="localhost",user="root",password="root",database="test”)

if mycon.is_connected()==False:

print("Error in connection")

else:

print("Connected")

cursor=mycon.cursor()

st="select * from item where description like 'L%'"

cursor.execute(st)

data=cursor.fetchall()

for i in data:

print(i)

mycon.close()

Output:

Connected

(50, 'Laptop', 1000)

(106, 'Laptop', 2000)

(107, 'Laptop', 2000)


Program 8

Aim: To integrate SQL with Python by importing the MySQL module and extracting

data from result set.


Program Code:

import mysql.connector as sqltor

mycon=sqltor.connect(host="localhost", user="root", password="root", database="test")

if mycon.is_connected( ) == False:

print("Error in connection")

else:

print("Connected")

cursor=mycon.cursor( )

cursor.execute("select * from item")

data=cursor.fetchall()

count=cursor.rowcount

for row in data:

print(row)

print("No.of records in the table:",count)

mycon.close( )

Output:

Connected

(50, 'Laptop', 1000)

(53, 'TV', 1000)

(51, 'TV', 2000)

(106, 'Laptop', 2000)

(107, 'Laptop', 2000)

No.of records in the table: 5


Program 9

Aim: To integrate SQL with Python by importing the MySQL module to display the number

of customers from each city.


Program Code:

import mysql.connector as sqltor

mycon=sqltor.connect(host="localhost", user="root", password="root", database="test")

if mycon.is_connected( ) == False:

print("Error in connection")

else:

print("Connected")

cursor=mycon.cursor( )

st="select city,count(*) from customer group by city"

cursor.execute(st)

str=cursor.fetchone()

while str is not None:

print(str)

str=cursor.fetchone()

print()

mycon.close()

Output

Connected

('Delhi', 3)

('Mumbai', 1)

('Bangalore', 1)
Program 10
Aim: To integrate SQL with Python by importing the MySQL module to to search a student

using roll no and update the record.

Program Code:

import mysql.connector as sqltor

mycon=sqltor.connect(host="localhost", user="root", password="root", database="test")

if mycon.is_connected( ) == False:

print("Error in connection")

else:

print("Connected")

cursor=mycon.cursor( )

rollno=int(input("Enter rollno:"))

perc=int(input("Enter percentage:"))

print("Before updating record")

st="select * from student where roll_no={}".format(rollno)

cursor.execute(st)

data=cursor.fetchone()

print(data)

st="update student set perc={} where roll_no={}".format(perc,rollno)

cursor.execute(st)

mycon.commit()

data=cursor.fetchone()

print("After updating record")

st="select * from student where roll_no={}".format(rollno)


cursor.execute(st)

data=cursor.fetchone()

print(data)

mycon.close()

Output

Connected

Enter rollno:106

Enter percentage:76

Before updating record

(106, 'John', 'Wilson', 12, 'Porur,chennai', 84.0)

After updating record

(106, 'John', 'Wilson', 12, 'Porur,chennai', 76.0)

You might also like