Ranjit H
Ranjit H
TEACHER
AIM:
To create table for teacher and execute the given commands using SQL.
TABLE: TEACHER
SQL Commands:
36
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
Shamathi 01/07/99
Department Count(*)
Computer 2
History 3
Maths 3
RESULT:
Thus the given program executed successfully.
37
DATE : PROGRAM NO : 19
STUDENT
AIM:
To create table for Student and execute the given commands using SQL.
TABLE: STUDENT
No Name Stipend Stream AvgMark Grade Class
1 Karan 400.00 Science 78.5 B 12B
2 Divakar 450.00 Science 89.2 A 11C
3 Divya 300.00 Commerce 68.6 C 12A
4 Arun 350.00 Commerce 73.1 B 12C
5 Robert 500.00 Humanities 90.6 A 11A
6 Sabina 400.00 Humanities 75.4 B 12A
7 Rubina 350.00 Science 64.4 C 12B
8 Vikas 450.00 Commerce 88.5 A 11B
9 John 500.00 Science 92.0 A 12A
10 Mohan 300.00 Commerce 67.5 C 12C
SQL Commands:
38
OUTPUT:
39
4. Select * from student student order by AvgMark Desc;
5. Select Name, Stipend from student where Name =’Mohan’ and Stipend > 200;
Name Stipend
Mohan 300.00
RESULT:
Thus the given program executed successfully.
40
DATE : PROGRAM NO : 20
COMPANY AND CUSTOMER
AIM:
To create two tables, company and customer and execute the given commands using SQL.
TABLE : COMPANY
TABLE : CUSTOMER
1. To display those company name which are having price less than 30000.
2. To display the name of the companies in reverse alphabetical order.
3. To increase the price by 1000 for those customer whose name starts with ‘S’
4. To add one more column totalprice with decimal (10,2) to the table customer
5. To display the details of company where productname as mobile.
41
SQL Commands:
CREATE TABLE CUSTOMER (custid int(3), name varchar(15), price int(10), qty int(3) , cid int(3));
INSERT INTO CUSTOMER VALUES (101, ‘ROHAN SHARMA’, 70000, 20, 222), (102, ‘DEEPAK
KUMAR’, 50000, 10, 666), (103, ‘MOHAN KUMAR’, 30000, 5, 111), (104, ‘SAHIL BANSAL’,
35000, 3, 333), (104, ‘NEHA SONI’, 25000, 7, 444), (106, ‘SONAL AGGARWAL’, 20000, 5, 333);
OUTPUT :
1. select name from company where company.cid=customer. cid and price < 30000;
NAME
NEHA SONI
NAME
SONY
ONIDA
NOKIA
DELL
BLACKBERRY
3. update customer set price = price + 1000 where name like ‘s%’; select * from customer;
42
103 MOHAN KUMAR 30000 5 111
RESULT:
Thus the given program executed successfully.
43
DATE : PROGRAM NO : 21
DOCTOR AND SALARY
AIM:
To create two tables for doctor and salary and execute the given commands using SQL.
TABLE:DOCTOR
TABLE: SALARY
i) Display NAME of all doctors who are in “MEDICINE” having more than 10 years
experience from table DOCTOR.
ii) Display the average salary of all doctors working in “ENT” department using the
tables DOCTOR and SALARY. (Salary=BASIC+ALLOWANCE)
iii) Display minimum ALLOWANCE of female doctors.
iv) Display DOCTOR.ID , NAME from the table DOCTOR and BASIC , ALLOWANCE from
the table SALARY with their corresponding matching ID.
v) To display distinct department from the table doctor.
44
SQL Commands:
CREATE TABLE DOCTOR(ID int NOT NULL PRIMARY KEY, NAME char(25) , DEPT char(25) ,
SEX char , EXPERIENCE int);
CREATE TABLE SALARY(ID int, BASIC int, ALLOWANCE int, CONSULTATION int);
INSERT INTO SALARY VLAUES (101, 12000,1000,300), (104, 23000,2300,500), (107, 32000,
4000, 500), (114, 12000, 5200, 100), (109,42000, 1700,200), (105, 18900, 1690, 300), (130, 21700,
2600, 300)
OUTPUT:
45
iv) select DOCTOR.ID, NAME, BASIC ,ALLOWANCE from DOCTOR,SALARY where
DOCTOR.ID=SALARY.ID;
RESULT:
Thus the given program executed successfully
46
DATE : PROGRAM NO : 22
Aim:
To write a program to perform creation and insertion operations with reference to table
“Employee” through MySQL – Python Connectivity.
Source code:
import mysql.connector as mysql
db1 = mysql.connect(host="localhost",user="root",passwd="Student@123",database="c12" )
cursor = db1.cursor()
try:
sql = "CREATE TABLE EMPSNEW(empno integer primary key,ename varchar(25) not null,salary
float);"
cursor.execute(sql)
print("Table created successfully")
except:
print("Table already exists")
sql = "INSERT INTO EMPSNEW VALUES (1,'Arun',80000),(2,'Vinu',75000),(3,'Neeru',81000);"
cursor.execute(sql)
print("Values inserted successfully")
try:
sql = "select * from EMPSNEW;"
cursor.execute(sql)
resultset = cursor.fetchall()
for row in resultset:
print (row)
db1.commit()
print("success")
except:
print("err")
db1.rollback()
db1.close()
47
OUTPUT:
48
DATE : PROGRAM NO : 23
SQL Connectivity – II ( Searching)
Aim:
To write a program to perform searching operations with reference to table “Employee”
through MySQL – Python Connectivity.
Source code:
Fetching all the records from EMPSNEW table having salary less than 80000.
49
Output:
50
DATE : PROGRAM NO : 24
SQL Connectivity – III ( Updating)
Aim:
To write a program to perform updating operations with reference to table “Employee”
through MySQL – Python Connectivity.
Source code:
except:
db1.rollback()
db1.close()
51
OUTPUT:
52
DATE : PROGRAM NO : 8
Write a Program to print all the line starting with letter ‘T’.
Aim:
To write a Program to print all the line starting with letter ‘T’.
Source Code:
53
Output:
54
DATE : PROGRAM NO : 9
Write a Program to count the number of occurrence of ‘is’ and ‘and’ in a text
file.
Aim:
To write a program to count the number of occurrence of ‘is’ and ‘and’ in a text file.
Source Code:
55
Output:
56
DATE : PROGRAM NO : 10
Write a Program to count vowels, consonants, digits, special character, lower
case & upper case letters present in text file.
Aim:
To write a program to count vowels, consonants, digits, special character, lower case &
upper case letters present in text file.
Source Code:
57
Output:
58
DATE : PROGRAM NO : 11
Write a Program to read a text file and create a new file after adding ‘ing’ to all words
ending with ‘s’ and ‘d’.
Aim:
To write a program to read a text file and create a new file after adding ‘ing’ to all
words ending with ‘s’ and ‘d’.
Source Code:
59
Output:
60
DATE : PROGRAM NO : 12
Write a Program to store and retrieving students information using CSV
file.
Aim:
To write a program to store and retrieving students information using CSV file.
Source Code:
61
Output:
62
DATE : PROGRAM NO : 13
Write a Program to copy the content of one CSV file to another by using different
delimiter.
Aim:
To write a program to copy the content of one CSV file to another by using different delimiter.
Source Code:
63
Output:
64
DATE : PROGRAM NO : 14
Write a Program to search and display using binary file.
Aim:
To write a program to search and display using binary file.
Source Code:
65
Output:
66
DATE : PROGRAM NO : 15
Write a menu driven program to write/read/update and append data onto a binary file.
Aim:
Write a menu driven program to write/read/update and append data onto a binary file.
Source Code:
67
Output:
68
DATE : PROGRAM NO : 16
Write a Python program to implement all basic operations of a stack, such
as adding element (PUSH operation), removing element (POP operation)
and displaying the stack elements (Traversal operation) using lists.
Aim:
Write a program to implement all basic operations of a stack, such as adding element (PUSH
operation), removing element (POP operation) and displaying the stack elements (Traversal
operation) using lists.
Source Code:
s=[]
c='y'
while(c=='y'):
print('1.PUSH')
print('2.POP')
print('3.DISPLAY')
choice=int(input('Enter your choice:'))
if(choice==1):
a=input('Enter any number')
s.append(a)
elif(choice==2):
if(s==[]):
print('Stack empty')
else:
print('Deleted element is:',s.pop())
elif(choice==3):
l=len(s)
for i in range(l-1,-1,-1):
print(s[i])
else:
print('Wrong input')
c=input('Do you want to continue or not?')
69
Output:
70
DATE : PROGRAM NO : 17
Aim:
Source Code:
71
Output:
72
DATE : PROGRAM NO : 18
SQL Connecting – I ( Creating & Insertion)
Aim:
To write a program to perform creation and insertion operations with reference to table
‘Employee’ through MySQL – Python connectivity.
Source Code:
import mysql.connector as mysql
db1 = mysql.connect(host="localhost",user="root",passwd="Student@123",database="NK"
) cursor = db1.cursor()
sql = "CREATE TABLE EMPS(empno integer primary key,ename varchar(25) not null,salary
float);"
cursor.execute(sql)
sql = "INSERT INTO EMPS VALUES(1,'Liyash PS',86000),(2,'PK Aishawaryamana
Pillai',86500),(3,'Chottavicky',90000),(4,'RAAMU',99000),(5,'KARTHIK PRINZZ',92000)" ;
try:
cursor.execute(sql)
db1.commit()
except:
db1.rollback()
db1.close()
73
Output:
74
DATE : PROGRAM NO : 19
SQL Connectivity – II (Searching Operation)
Aim:
To write a program to perform searching operation with reference to table ‘Employee’ through
MySQL-Python connectivity
Fetching all the records from EMPS table having salary more than 70000.
Source Code:
import mysql.connector as mysql
db1 = mysql.connect(host="localhost",user="root",passwd="Student@123",database="NK" )
cursor = db1.cursor()
sql = "SELECT * FROM EMPS WHERE SALARY > 70000;"
try:
cursor.execute(sql)
resultset = cursor.fetchall()
for row in resultset:
print (row)
except:
print ("Error: unable to fetch data")
db1.close()
75
Output:
76
DATE : PROGRAM NO : 20
SQL Connectivity – III ( Updating Operation)
Aim:
To write a program to perform updating operation with reference to table ‘Employee’ through
MySQL-Python connectivity
Source Code:
import mysql.connector as mysql
db1 = mysql.connect(host="localhost",user="root",passwd="Student@123",database="NK" )
cursor = db1.cursor()
sql = "UPDATE EMPS SET salary = salary +1000 WHERE salary<80000;"
try:
cursor.execute(sql)
db1.commit()
except:
db1.rollback()
db1.close()
77
OUTPUT :
78
DATE : PROGRAM NO : 21
SQL CONNECTIVITY – IV (Deletion operation)
Aim:
To write a program to perform deletion operation with reference to table ‘Employee’ through
MySQL-Python connectivity
79
OUTPUT:
80
DATE : PROGRAM NO : 22
Creating, inserting values, updating and display a table in many forms
using SQL Queries
AIM:
Creating, inserting values, updating and display a table in many forms using SQL Queries
Creating a table:
mysql> create table student(sid int(5), sname char(20), gender char(7), city char(30), phonenum
int(10));
Query OK, 0 rows affected (0.08 sec)
mysql> create table marks(sid int(5), Computer int, Physics int, Chemistry int, Biology int);
Query OK, 0 rows affected (0.08 sec)
81
Displaying a table:
82
mysql> select * from student where gender='Male' order by sname;
sid sname gender city phonenum
103 Karthick Male Krishnagiri 2389345
101 Sathya Male Krishnagiri 2467453
105 Vimal Male Dharmapuri 2356895
Aggregate Functions
mysql> select sum(Computer) from marks;
sum(Computer)
409
1 rows in set (0.03 sec)
83
The Update Command
mysql> update marks set Total = Computer + Physics + Chemistry + Biology, Average=Total/4;
Query OK, 5 row affected (0.06 sec)
Row matched : 5 Changed : 5 Warnings : 0
mysql> select * from marks;
sid Computer Physics Chemistry Biology Total Average
101 89 67 58 90 304 76
102 78 98 65 45 286 72
103 56 78 83 97 314 79
104 99 48 69 75 291 73
105 87 82 79 60 308 77
5 rows in set (0.00 sec)
Delete Command
Drop Command
84