RecPgm4 10
RecPgm4 10
PROBLEM DEFINITION:
Program:
def calculate(d,n):
for i in range(1,n+1):
pf=(d[i]['basic']+d[i]['hra']+d[i]['da'])*0.1
net=d[i]['basic']+d[i]['hra']+d[i]['da'] -pf
d[i]['pf']=pf
d[i]['net']=net
d[i][‘annual’]=net*12
d={}
d1={}
d[i]=d1
calculate(d,n)
print("Name \t\t Basic \t\t HRA \t\t DA \t\t PF \t\t Netsalary\t\t”Annual Salary")
for i in d:
for j in d[i]:
print(d[i][j],"\t\t", end='')
print()
Output:
Record Program 5:
SQL EXERCISE -1
PROBLEM DEFINITION:
Create STATIONARY and CONSUMER table with the following specifications and insert the
given data.
i) Specification for tables:
Table name : STATIONARY
Attributes: S_ID of type String (Primary Key)
Table: STATIONARY
SQL COMMANDS:
Output:
distinct Company
ABC
XYZ
CAM
Output:
S_ID StationaryName Company Price
DP01 Dot Pen ABC 10
GP02 Gel Pen ABC 15
5. To display the ConsumerName, Address from table Consumer and Company and
Price from table Stationary with their corresponding matching S_ID
SQL EXERCISE -2
PROBLEM DEFINITION:
Consider the tables WORKER and PAYLEVEL and answer the following
questions:
(i) Write a SQL Command to display the details of all the WORKERS in
descending order of their DOJ.
(ii) Write a SQL Command to display the NAME and DESIG of those
WORKERS whose PLEVEL is either P001 or P002.
(iii) Write a SQL Command to display the number of workers whose
PAY+ALLOWANCE is more than 30000 for every PLEVEL.
(iv) Write a SQL Command to increase the ALLOWANCE by 1000 where
the pay is greater than 20000.
(v) Write a SQL Command to display the number of Workers designation
wise.
TABLE : WORKER
TABLE : PAYLEVEL
SQL COMMANDS:
1. To display the details of all the WORKERS in descending order of their DOJ.
Output:
2. To display the NAME and DESIG of those WORKERS whose PLEVEL is either P001 or
P002.
Output:
NAME DESIG
RadheShyam Supervisor
Ameen Ahmed Mechanic
Sanya Clerk
Sarsa Supervisor
3. To display the number of workers whose PAY+ALLOWANCE is more than 30000 for
every PLEVEL.
Output:
COUNT(*)
2
2
4. To increase the ALLOWANCE by 1000 where the pay is greater than 20000.
Output:
2 rows affected
COUNT(*) DESIG
2 Supervisor
2 Operator
1 Mechanic
1 Clerk
SQL EXERCISE -3
PROBLEM DEFINITION:
Consider the given tables COMPUTER and SALES and answer the questions that
follows:
Write SQL commands for the following statements:
i) Display the minimum price and maximum price from the table COMPUTER.
ii) Display the total number of records company wise, when the count is greater
than 1.
iii)Add a Constraint PRIMARY KEY to the column PROD_ID in the table
COMPUTER
iv)Display the product details and quantity sold for the first quarter using
natural join.
v)Increase the price of the products by 100 for input type products.
Table: COMPUTER
PROD_ID PROD_NAME PRICE COMPANY TYPEE
P001 Mouse 200 Logitech Input
P002 Laser Printer 4000 Canon Output
P003 Keyboard 500 Logitech Input
P004 Joystick 1000 Iball Input
P005 Speaker 1200 Creative Output
P006 Deskjet Printer 4300 Canon Output
Table : SALES
PROD_ID QTY_SOLD QUATER
P002 4 1
P003 2 2
P001 3 2
P004 2 1
SQL COMMANDS:
1. Display the minimum price and maximum price from the table
COMPUTER.
Output:
Max(PRICE) Min(PRICE)
4300 200
2. Display the total number of records company wise, when the count is
greater than 1.
Ans: select COMPANY, count(*) from COMPUTER group by COMPANY
having count(*)>1;
Output:
COMPANY Count(*)
Canon 2
Logitech 2
Output:
6 rows affected
4.Display the product details and quantity sold for the first quarter using
natural join.
Output:
Output:
3 rows affected.
PROBLEM DEFINITION:
Consider the given tables TOYSHOP and SUPPLIERS and W rite SQL commands for the
following statements:
i. To display the highest price of each type of toy.
ii. To display the toys which are supplied by Ritwij Rawat
iii. To display the total quantity of toys supplied by each supplier.
iv. To display the name of the supplier who supplies Tennis Raquets.
v. To add a new column, Date of Order of the type date to the table TOYSHOP
Table - TOYSHOP
Table - SUPPLIERS
SCODE SNAME
H222 Rohan Kumar
P101 Vijay Pal
G204 Garv Punaya
R402 Ritwij Rawat
SQL COMMANDS:
Output:
Max(PRICE) TYPE
1150 GF
990 HF
650 TF
Output:
Ans: select SNAME, sum(QUANTITY) from TOYSHOP natural join SUPPLIERS group by
SNAME;
Output:
SNAME Sum(QUANTITY)
Vijay Pal 60
Garv Punaya 40
Ritwij Rawat 65
4. To display the name of the supplier who supplies Tennis Raquet.
SNAME
Ritwij Rawat
5. To add a new column, Date of Order of the type date to the table TOYSHOP
PROBLEM DEFINITION:
Consider the given tables SPORTS and COACH and Write SQL commands for the following
statements:
(i) To display scode, the number of coaches for each scode from the table coach and display
scode in descending order.
(ii) To display details of those sports and coachname which are having Prizemoney more than
9000 and coachname ends with ‘n’.
(iii) To display the contents of the sports table with their coachname whose schedule date is in
the year 2012.
(iv) To display number of different participants from the table sports.
(v) Delete the column Prizemoney from the table SPORTS.
TABLE : SPORTS
TABLE : COACH
Code Name Scode
1 Ravi 101
2 Mohan 108
3 Sameer 101
4 Shikhar 103
SQL COMMANDS:
1. To display scode, the number of coaches for each scode from the table coach and
display scode in descending order.
Output:
Scode Count(*)
108 1
103 1
101 2
2. To display details of those sports and coachname which are having Prizemoney
more than 9000 and coachname ends with ‘n’.
Output:
Sportsname COACHNAME
Lawn Tennis Mohan
3. To display the contents of the sports table with their coachname whose schedule
date is in the year 2012.
Output:
Output:
distinct Participants
2
4
Output :
0 rows updated
Record Program No. 10
PROBLEM DEFINITION:
Write a Program to connect Python with MySQL using database connectivity and perform
the following operations on data in database: Insert, Fetch and Update the data.
i)Create a table Teacher with Teacher ID, Name, Age, Department, Doj, Salary, Gender,
Place.
TABLE - TEACHER
T_id Name Age Department Date_of_join Salary Gender
1 Jugal 34 Computer Science 10/01/2017 12000 M
2 Sharmila 31 History 24/03/2008 20000 F
3 Sandeep 32 Mathematics 12/12/2016 30000 M
4 Sangeeta 35 History 01/07/2015 40000 F
5 Rakesh 42 Mathematics 05/09/2007 25000 M
Program :
"%-15s"%"Salary","%-10s"%"Gender","%-15s"%"Place")
print("\n\t%-10s"%"Tid.","%-15s"%"Name","%-9s"%"Age","%-25s"%"Department",\
"%-20s"%"DateofJoin","%-15s"%"Salary","%-10s"%"Gender","%-15s"%"Place")
for row in data:
print("\t%-10s"%row[0],"%-15s"%row[1],"%-9s"%row[2],"%-25s"%row[3],\
"%-20s"%row[4],"%-15s"%row[5],"%-10s"%row[6],"%-15s"%row[7])
def Update():
try:
print(" Updation of the Salary")
cursor.execute("update Teacher set salary=salary+salary*0.05 \
where doj between '2017-01-01' and '2018-12-31' ")
cursor.execute("select * from Teacher")
data=cursor.fetchall()
print("\n\t%-10s"%"Tid.","%-15s"%"Name","%-9s"%"Age","%-25s"%"Department",\
"%-20s"%"DateofJoin","%-15s"%"Salary","%-10s"%"Gender","%-15s"%"Place")
for row in data:
print("\t%-10s"%row[0],"%-15s"%row[1],"%-9s"%row[2],"%-25s"%row[3],\
"%-20s"%row[4],"%-15s"%row[5],"%-10s"%row[6],"%-15s"%row[7])
except:
print("Updation not possible.")
def Select2():
cursor.execute("select max(salary), dept from Teacher group by dept")
data=cursor.fetchall()
print(" Highest Salary details in each Department")
print("\n\t%-15s"%"Max(Salary).","%-20s"%"Department")
for row in data:
print("\t%-15s"%row[0],"%-20s"%row[1])
#main program
import mysql.connector as sqltor
mycon=sqltor.connect(host="localhost",user="root",passwd="admin",database="s
ys")
if mycon.is_connected():
print("Successful")
cursor=mycon.cursor()
try:
cursor.execute("create table if not exists Teacher (T_id int primary key,Name varchar(20),\
Age int,dept varchar(20),doj date,salary int, gender char(1),place varchar(20))")
print(" 1.Table Created")
except:
print("Table already exists")
exit()
cursor.execute("insert into Teacher values(1,'Jugal',34,'Computer Science','2017-
01-10',12000,'M','Delhi')" )
cursor.execute("insert into Teacher values(2,'Sharmila',31,'History','2008-03-
24',20000,'F','Raipur')")
cursor.execute("insert into Teacher values(3,'Sandeep',32,'Mathematics','2016-12-
12',30000,'M', 'Delhi')")
cursor.execute("insert into Teacher values(4,'Sangeeta',35,'History','2015-07-
01',40000,'F','Chennai')")
cursor.execute("insert into Teacher values(5,'Rakesh',42,'Mathematics', '2007-09-
05',25000,'M','Delhi')")
print("2. 5 Records Inserted")
Display()
Select1()
Update()
Select2()
Output: